Go language (basic)-map

1 Introduction

  • One of the most useful data structures in computer science is the hash table. Different hash table implementations provide many unique features. But basically it includes element query, addition and deletion. Go provides a built-in type map, which implements the basic functions of a hash table.
  • So if you want to use hash table in Go language, then use map.

2. Basic use of map

2.1, declaration and initialization

2.1.1, statement
var m map[string]int //此时 m 还没有被分配内存空间,是个 nil
  • If space is not allocated to the map and data is inserted into it, there will be no intelligence error.
var m map[string]int
m["中国"] = 1
//报错:panic: assignment to entry in nil map

note! When declaring the map, the data type of the key has requirements, it must be a comparable type, and for the value, it can be any data type. There are many comparable types, which is troublesome to remember, but there are only three non-comparable types: slice, map, and func .

2.1.2, initialization
  • Initializing the map is actually allocating space for the map. Next, there are three ways to initialize the map.
  • method one
var m = map[string]int{
    
    } //不要笑看了这一对大括号,这样就算给 map 分配空间了
m["中国"] = 1
  • Way two
var m map[string]int
m = make(map[string]int, 2)
  • Way three
var m = map[string]int{
    
    
	"中国" : 1,
	"美国" : 2,
}

2.2, add, delete, modify and check

var m = map[string]int{
    
    }
//1.增
m["中国"] = 1
m["美国"] = 2 //m->map[中国:1 美国:2]
//2.删
delete(m, "美国") //m->map[中国:1]
//3.改
m["中国"] = 100 //m->map[中国:0]
//4.查
val, ok := m["中国"] //val=100, ok=true
  • You can also check it like this, that is, you don't need to ok to receive the presence or absence of a certain key-value pair.
val := m["中国"] //val=100
  • When deleting a key-value pair in the map above, if the corresponding key does not exist in the map, no changes will be made; when a key-value pair does not exist in the query map, if it does not exist, the value will be returned The zero value of the data type, for example, the data type of value is int, and the zero value of int is 0.

2.3, traverse the map

func Traverse() {
    
    
	m := map[string]int{
    
    
		"china" : 1,
		"america" : 2,
		"england" : 3,
		"japan" : 4,
		"russia" : 5,
		"germany" : 6,
		"india" : 7,
	}
	for k, v := range m {
    
    
		fmt.Printf("number %d is %s\n", v, k)
	}
}
number 5 is russia
number 6 is germany
number 7 is india
number 1 is china
number 2 is america
number 3 is england
number 4 is japan
  • Seeing that the display order of the traversal results does not match the order in which I inserted the data, what should I do if I just want to traverse in an orderly manner?
func SortTraverse() {
    
    
	m := map[string]int{
    
    
		"china" : 1,
		"america" : 2,
		"england" : 3,
		"japan" : 4,
		"russia" : 5,
		"germany" : 6,
		"india" : 7,
	}
	keys := []string{
    
    }
	for k := range m {
    
    
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, k := range keys {
    
    
		fmt.Printf("number %d is %s\n", m[k], k)
	}
}
number 2 is america
number 1 is china
number 3 is england
number 6 is germany
number 7 is india
number 4 is japan
number 5 is russia

2.4, function parameter transfer

  • As a function parameter, map is actually passed the address value of map. If you don't believe it, let's take a look.
func ChangeMap(m map[string]int) {
    
    
	fmt.Printf("对 m 未做修改前:m 的地址是 %p\n", m)
	m["美国"] = 2
	fmt.Printf("对 m 插入1条数据后:m 的地址是 %p\n", m)
	m["俄罗斯"] = 3
	fmt.Printf("对 m 插入2条数据后:m 的地址是 %p\n", m)
	for i := 0; i < 9999998; i++ {
    
    
		m["国家" + strconv.Itoa(i)] = i
	}
	fmt.Printf("对 m 插入10000000条数据后:m 的地址是 %p\n", m)
}

func main() {
    
    
	m := make(map[string]int, 2)
	m["中国"] = 1
	//m["美国"] = 2
	fmt.Printf("main.传参前,m 的地址是 %p\n", m)
	Map.ChangeMap(m)
	fmt.Println(m["国家123456"])
}
main.传参前,m 的地址是 0xc0000b6330
ChangeMap.对 m 未做修改前:m 的地址是 0xc0000b6330
ChangeMap.对 m 插入1条数据后:m 的地址是 0xc0000b6330
ChangeMap.对 m 插入2条数据后:m 的地址是 0xc0000b6330
ChangeMap.对 m 插入10000000条数据后:m 的地址是 0xc0000b6330
main. 123456

Guess you like

Origin blog.csdn.net/wxy_csdn_world/article/details/115126650