go map, key is string, value is pointer to a struct -
type country struct { code string name string } var store = map[string]*country{}
in go code piece, key string, value pointer struct. what's benefit use pointer of contry here? can remove "*" , achieve same behaviour? such as:
var store = map[string]country
thanks.
you can achieve same behavior using either pointer or value.
package main import ( "fmt" ) type country struct { code string name string } func main() { var store = make(map[string]*country) var store2 = make(map[string]country) c1 := country{"us", "united states"} store["country1"] = &c1 store2["country1"] = c1 fmt.println(store["country1"].name) // prints "united states" fmt.println(store2["country1"].name) // prints "united states" }
using pointer store address of struct in map instead of copy of entire struct. small structs in example won't make of difference. larger structs might impact performance.
Comments
Post a Comment