Going straight to the point, let's look at the following code:
package main import ( "encoding/json" "fmt") func main() { //The first statement var language map[string]string language = make(map[string]string, 10) //Before using map, you need to make first. The purpose of make is to allocate data space to the map. language["1"] = "C#" language["2"] = "go" language["3"] = "python" (language) //map[1:C# 2:go 3:python] //The second declaration Compared with the first one above, there is less var declaration language2 := make(map[string]string) language2["1"] = "C#" language2["2"] = "go" language2["3"] = "python" (language2) //map[1:C# 2:go 3:python] //The third declaration is direct initialization language3 := map[string]string{ "1": "C#", "2": "go", "3": "python", } (language3) //map[1:C# 2:go 3:python] language4 := make(map[string]map[string]string) // Here is the key-value pair: string => map[string]string, simple understanding is [string][string]=>string language4["python"] = make(map[string]string, 2) language4["python"]["id"] = "1" language4["python"]["desc"] = "python is the best language in the world" language4["go"] = make(map[string]string, 2) language4["go"]["id"] = "2" language4["go"]["desc"] = "go is the best language in the world" (language4) //map[go:map[desc:go is the best language in the world id:2] python:map[desc:python is the best language in the world id:1]] //Addition and deletion, modification and search val, key := language4["java"] // Find out if there is a child element of java if key { (val) } else { ("Key not found") } //language4["go"]["id"] = "3" //Modify the id value of the go child element. Note that it is a modification here, do not understand it as an append element //language4["go"]["name"] = "golang best performance" //Increase the name value in the go element //delete(language4, "python") //Delete python child element //(language4) mjson, _ := (language4) mString := string(mjson) ("json String:%s", mString) }
The above is a comprehensive example. Many times, you need to remove certain elements from the traversal object or add elements to the traversal object. At this time, you need to be careful.
I have made some summary and examples of some notes in Go language and left some notes. Let's continue to give a few examples:
Traversal slices
Remove elements when traversing slices, error example:
package main import ( "fmt") func main() { arr := []int{1, 2, 3, 4} for i := range arr { if arr[i] == 3 { // That is, the subscript is 2 println(len(arr)) //arr = append(arr[:i], arr[i+1:]...) //Because the range of range of i has been determined during iteration to be [0,len(arr)) and the left-closed and right-open interval [0,3). //When arr[i] == 3 is satisfied, arr is modified to shorten the length of arr. At this time, len(arr)=3, the maximum subscript is 2, so an error will be reported when arr[3] is executed. Because of overflow } (arr) } //panic: runtime error: index out of range [3] with length 3
So how do you correctly delete the specified slice element? Let's change it slightly:
Remove elements when traversing slices, and there will be no errors, but not recommended writing methods:
package main import ( "fmt") func main() { arr := []int{1, 2, 3, 4} for i, v := range arr { if v == 3 { arr = append(arr[:i], arr[i+1:]...) // arr[:i] is arr[:2]=> []int{1, 2}, arr[i+1:] is: arr[3:] =>[]int{4} } } (arr) } // Output [1 2 4]
explain:
Go back to the usage of range. When executing the for loop, the traversal element value of (i,v) has been determined. Arr is modified in time during the loop, and the (i,v) value to be traversed by for will not be changed.
You can modify the above code and see that when changing the arr value in the loop, the (i, v) that is traversed later will not change with the change of arr. Continue to read:
Remove elements when traversing slices. The recommended way to write them:
package main import ( "fmt") func main() { arr := []int{1, 2, 3, 4} for i := 0; i < len(arr); i++ { (i, arr[i]) if arr[i] == 3 { ("i--Before=", i) arr = append(arr[:i], arr[i+1:]...) //arr[:2],arr[3:] i-- ("i--after=", i) } } (arr) }
Output:
0 11 22 3i--before= 2i--after= 12 4[1 2 4]
explain:
This scheme only modifies the value of i and performs i-- when deleting elements, which can ensure that there is no problem traversing arr, and there is no problem getting the slice value through arr[i] every time.
Of course, you can also add elements during traversal using this method, as long as i also changes accordingly, there is no problem.
Summarize:
There are some pitfalls when operating during slice traversal.
There are relatively few pitfalls when traversing maps, but when traversing maps, the value of maps must be of pointer type, which is worth remembering.
This is the article about the definition of Go maps and modification techniques. For more relevant content on Go maps, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!