map的value赋值
package main
import "fmt"
type Student struct {
Name string
}
var list map[string] Student
//map的Value赋值
func main() {
list = make(map[string] Student)
student := Student{"action"}
list["student"] = student
//list["student"].Name = "xd" //错误 cannot assign to struct field list["student"].Name in map
tempStudent := list["student"]
tempStudent.Name = "xd"
list["student"] = tempStudent
fmt.Println(list["student"])
}
map的遍历赋值
package main
import "fmt"
type People struct {
Name string
Age int
}
//map的遍历赋值
func main() {
//定义map
list := make(map[string] *People)
//定义student数组
peoples := []People{
{Name: "zhou", Age: 24},
{Name: "li", Age: 23},
{Name: "xing", Age: 22},
}
//遍历结构体数组,依次赋值给map
for i:=0;i<len(peoples);i++{
list[peoples[i].Name] = &peoples[i]
}
//打印map
for k,v := range list{
fmt.Println(k,"=>",v.Name)
}
}
action
本站未注明转载的文章均为原创,并采用
CC BY-NC-SA 4.0授权协议,
转载请注明来源,谢谢!如本站内容对你有所帮助的话,欢迎订阅关注
邢栋博客,唠嗑(分享)每日的折腾经历。
已有 0 条评论