在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原问地址https://studygolang.com/articles/31024 修改方法说明方法1package test1 import ( "fmt" "testing" ) type Person struct{ Name string Age int Gender string } func TestMap1(t *testing.T){ m1 := map[uint]Person{ 1: {"whw", 12, "male"}, 2: {"naruto", 22, "male"}, } fmt.Println("m1: ", m1) // TODO 传入一个临时结构体 修改map中的结构体然后再赋值即可 tempObj := m1[1] tempObj.Name = "www" tempObj.Age = 21 tempObj.Gender = "MALE" m1[1] = tempObj fmt.Println("m1: ", m1) } 方法2package test1 import ( "fmt" "testing" ) type Person struct{ Name string Age int Gender string } func TestMap2(t *testing.T){ // 定义阶段使用结构体指针 m2 := map[uint]*Person{ 3: {"sasuke", 22, "male"}, 4: {"sakurua", 21, "female"}, } fmt.Println("m2: ", m2) fmt.Println("name: ", m2[3].Name) m2[3].Name = "SASUKE" m2[3].Age = 25 m2[3].Gender = "MALE" fmt.Println("m2: ", m2) fmt.Println("name: ", m2[3].Name) } ~~~
|
请发表评论