Go study notes - map dictionary and for-range traversal

Go study notes—map, range


Thanks for watching, if you have any questions, please leave a message to correct me! Thanks


1、map

Map is a built-in associated data type in Go, also known as dictionary or hash, internally 散列表(hash)implemented by

map is an unordered based data structure that needs to be created key-valueusing built-in functions, and its syntax is:make()

//make(map[key-type]value-type)
make(map[string]int)
//Map: An empty map is allocated with enough space to hold the
//specified number of elements. The size may be omitted, in which case
//a small starting size is allocated.
//一个空map被分配足够的空间来存储指定数量的元素。空间的容量可以省略,在这种情况下,分配较小的其实空间。

Initialization and basic use of map:

func main(){
    
    
    m := make(map[string]int)
    m["安徽"] = 10
	m["江苏"] = 13
	fmt.Println("map:",m)
    
    //使用自动推导声明和初始化键值对
    n := map[int]int{
    
    1:10,2:100,3:1000}
    fmt.Println("map-n:",n)
}

//map: map[安徽:10 江苏:13]
//map-n: map[1:10 2:100 3:1000]

Use name[key]to get the value corresponding to a key:

func main(){
    
    
    v1 := m["安徽"]
    fmt.Println(v1)
}	

//10

Using the built-in function len(), you can get the number of key-value pairs:

func main(){
    
    
    fmt.Printf("包含键值对的数目为:%d",len(m))
}

//包含键值对的数目为:2

delete()A key-value pair can be removed from a map using the built-in function

func main(){
    
    
    delete(m,"江苏")
    fmt.Println("map:",m)
}

map: map[安徽:10]

When getting a value from the map, you can use value:ok := map[key]it to determine whether the value corresponding to the key is in the map. can disambiguate that a key does not exist and a key has a zero value, such as 0and""

func main(){
    
    
    _,prs := m["江苏"]
	fmt.Println("prs:",prs)
}

//prs: false  说明不存在指定的键

2. rangetraverse

rangeGenerally forused in combination with to provide the index and value of each item.

In ** slice**, in addition to using index traversal, you can use for...range...to traverse.

func main(){
    
    
	slice := []int{
    
    1,2,3,4,5,6,7,8,9,0}
	sum := 0	//返回切片存储值的和
	ind := 0	//返回切片存储元素的个数
	for index,value := range slice{
    
    
		ind += 1
		sum += value
		fmt.Printf("%d:%d\t",index,value)
	}
	fmt.Printf("\n")
	fmt.Printf("存储元素的个数为:%d\t",ind)
	fmt.Printf("存储元素的总和为:%d\t",sum)
}

//0:1 1:2 2:3 3:4 4:5 5:6 6:7 7:8 8:9 9:0	
//存储元素的个数为:10	存储元素的总和为:45	

It can be seen from the above output results that for...range...the index corresponding to the element can be output at the same time as the result of the traversal. If the index value is not required, an anonymous variable can be used_

for index,value := range slice	//使用索引值
for _,value := range slice	//不需要使用索引值

In ** array**, the method of use is slicesimilar to .

In ** map**, the usage is similar.

func main(){
    
    
    kvs := map[string]string{
    
    "a":"apple","b":"banana"}
	for key,value := range kvs{
    
    
		fmt.Printf("%s:%s\n",key,value)
	}
}

//a:apple
//b:banana

In ** string**, the first return value is the starting byte position of the character, and the second return value is the character itself.

func main(){
    
    
    for i,c := range "study"{
    
    
		fmt.Println(i,c)
	}
}

//0 115
//1 116
//2 117
//3 100
//4 121

for...range...You can also use channel( channel) to return only the value inside the channel.

Guess you like

Origin blog.csdn.net/weixin_46435420/article/details/119449731