GoLang:在 Anynomous 结构中使用地图

我试图了解如何在匿名结构中使用地图。


我的代码如下


places := struct {

      Country map[string][]string

    }{

      make(map[string][]string)["india"] :=  []string{"Chennai", "Hyderabad", "Kolkata" }

    }

我尝试使用new()初始化但没有成功。


是否可以在匿名结构中使用映射?


繁花如伊
浏览 95回答 2
2回答

大话西游666

使用复合文字:places := struct {    Country map[string][]string}{    Country: map[string][]string{"india": {"Chennai", "Hyderabad", "Kolkata"}},}或者,如果您想使用make,您可以使用多个语句来实现:places := struct {    Country map[string][]string}{    Country: make(map[string][]string),}places.Country["india"] = []string{"Chennai", "Hyderabad", "Kolkata"}// orplaces := struct { Country map[string][]string }places.Country = make(map[string][]string)places.Country["india"] = []string{"Chennai", "Hyderabad", "Kolkata"}

慕娘9325324

这应该有效:https ://goplay.space/#gfSDLS79AHBpackage mainimport (    "fmt")func main() {    places := struct {        Country map[string][]string    }{        Country: map[string][]string{"india": {"Chennai", "Hyderabad", "Kolkata"}},    }    fmt.Println("places =", places)}
打开App,查看更多内容
随时随地看视频慕课网APP