"nil slice" vs "nil map"

Slice and map are all reference types in Go, and their default values are nil: package main import "fmt" func main() { var ( s []int m map[int]bool ) if s == nil { fmt.Println("The value of s is nil") } if m == nil { fmt.Println("The value of m is nil") } } The result is like this: The value of s is nil The value of m is nil When a slice’s value is nil, you could also do operations on it, such as append: [Read More]

Accessing Map

Map is a reference type which points to a hash table, and you can use it to construct a “key-value” database which is very handy in practice programming. E.g., the following code will calculate the count of every element in a slice: package main import ( "fmt" ) func main() { s := []int{1, 1, 2, 2, 3, 3, 3} m := make(map[int]int) for _, v := range s { m[v]++ } for key, value := range m { fmt. [Read More]
map