为了账号安全,请及时绑定邮箱和手机立即绑定

GoLang:在 Anynomous 结构中使用地图

GoLang:在 Anynomous 结构中使用地图

Go
繁花如伊 2022-11-23 20:04:39
我试图了解如何在匿名结构中使用地图。我的代码如下places := struct {      Country map[string][]string    }{      make(map[string][]string)["india"] :=  []string{"Chennai", "Hyderabad", "Kolkata" }    }我尝试使用new()初始化但没有成功。是否可以在匿名结构中使用映射?
查看完整描述

2 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

使用复合文字:


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"}


// or


places := struct { Country map[string][]string }

places.Country = make(map[string][]string)

places.Country["india"] = []string{"Chennai", "Hyderabad", "Kolkata"}


查看完整回答
反对 回复 2022-11-23
?
慕娘9325324

TA贡献1783条经验 获得超4个赞

这应该有效:https ://goplay.space/#gfSDLS79AHB

package main


import (

    "fmt"

)


func main() {


    places := struct {

        Country map[string][]string

    }{

        Country: map[string][]string{"india": {"Chennai", "Hyderabad", "Kolkata"}},

    }


    fmt.Println("places =", places)

}


查看完整回答
反对 回复 2022-11-23
  • 2 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信