K
K
kvaks2020-09-13 18:02:39
go
kvaks, 2020-09-13 18:02:39

How to display in GO in template struct?

For some reason, {{.time}} crashes with an error

executing "index.html" at <.time>: time is an unexported field of struct type main.tb

even though {{.}} pulls data out of the struct. Tell me how to display data from the structure separately?

type tb struct {
  time string
  booking bool
}

type Room struct {
  Name string
  Bookings []tb
}

var rooms = make(map[int]Room)

var TIME_CONST = []tb{
  tb{"8:00",false },tb{"8:30",false },
  tb{"9:00",false },tb{"9:30",false },}

func addRoom(name string)  {
  rooms[cns] = Room{ name,TIME_CONST}
  cns++
}


<ul class="list-group">
            {{range $key, $value := .}}
                    <li class="list-group-item">
                        <a href="/bookingDetails" > {{$key}}  {{.Name}} </a>
                    {{range .Bookings}}
                            {{.time}}
                    {{ end }}
                    </li>
                {{ end }}
        </ul>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2020-09-13
@kvaks

Structure fields must be exportable (Start with a capital letter), then there will be access to these fields from the template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question