To create a map with a value of type struct in Go, first you need to define the struct type. Let's define a simple struct type called Person
:
main.go52 chars5 lines
Now, to create a map with a value of type Person
, you can do the following:
main.go119 chars5 lines
Here, we are creating a map with string keys and Person
values. We are initializing the map with two Person
values, one for the key "Alice" and one for the key "Bob".
You can also add values to the map later using the same syntax:
main.go53 chars2 lines
Now, the map will have three keys ("Alice", "Bob", and "Charlie") with corresponding Person
values.
gistlibby LogSnag