go语言基础数据结构学习–> 字典(map)
go 语言中的字典和python 中的字典特性差不多
相同: 键值对, 无序集合, 每个键都是唯一的, 对一个键多次赋值会更新当前键的值;
不同: go语言的字典里面的类型是定好的, 不可变更, python可以随意写类型.
package main
import "fmt"
type Fei struct {
id int
name string
}
type Dic map[string]int
func main() {
dict := make(map[string]string)
dicts := map[string]int{ "age": 10, "丙总": 2}
result := make(map[int]*Fei)
result[0] = &Fei{ id: 6300, name: "刀妹"}
letter := []string{"a", "b", "c", "d", "e", "f", "g", "h"}
dict["name"] = "薇恩"
dict["age"] = "18"
dict["label"] = "小飞"
ss := result[0]
ss.id = 4800
name := dict["name"]
res := dict["res"]
fmt.Println(name)
fmt.Println(res)
fmt.Println("lens", len(dict))
delete(dict, "label")
delete(dict, "ask")
for k := range dict{
delete(dict, k)
}
for k,v := range letter{
fmt.Println("还可以", k, v)
dicts[v] = k
}
fmt.Println(dict)
fmt.Println(dicts)
fmt.Println(result[0])
respon := make(map[string]Dic)
DicNum := make(Dic)
DicNum["id"] = 1
DicNum["age"] = 2
respon["ids"] = DicNum
respon["type"] = Dic{"id": 11, "age": 22}
fmt.Println(respon)
}