To create a map with a value of type pointer in Go, you can use the make
function to create an empty map with the desired key and value types, and then set the values of the map to pointers to the desired type.
Here's an example:
main.go615 chars22 lines
In this example, we create a map people
with keys of type string
, and values of type *Person
(a pointer to a Person
struct). We then create two new Person
objects, and store pointers to them in the map using their names as keys. Finally, we manipulate the Age
and Name
fields of the Person
objects directly via their pointers stored in the map, and then print the updated values.
gistlibby LogSnag