GO study notes - map

Original link: http://www.cnblogs.com/sunylat/p/6385852.html

map is an advanced data type GO language, is characterized by a corresponding key and value, which is the same and Delphi in the Dictionary! map format statement: map [Key Data Type] value data type. Before use map, you must be created with make!

Example:

import "fmt"

func main() {

	/*
	Declare a map, m is a variable name of this map.
	map the key: [] in the map of this parameter is the key, the type to string.
	map of value: int type of final surface is the map of value.
	*/
	var m map[string]int
	// Create the map with the make
	m = make(map[string]int)

	// Add the key is "Answer" elements, while the corresponding values ​​assigned to it 42
	m["Answer"] = 42
	fmt.Println("The value:", m["Answer"])

	// Modify key is "Answer" value of the value 48
	m["Answer"] = 48
	fmt.Println("The value:", m["Answer"])

	/*
	Detection key is "Answer" element exists, returns a value of two.
	The first one: the current value of the value of the element, if the element is not present or 0
	Second: whether the current Boolean value of existing elements
	*/
	v1, ok1 := m["Answer"]
	fmt.Println("The value:", v1, "Present?", ok1)

	// delete key is "Answer" of map elements
	delete(m, "Answer")
	fmt.Println("The value:", m["Answer"])

	/*
	Detection key is "Answer" element exists, returns a value of two.
	The first one: the current value of the value of the element, if the element is not present or 0
	Second: whether the current Boolean value of existing elements
	*/
	v2, ok2 := m["Answer"]
	if ok2 == true {
		fmt.Println("Answer exits")
	} else {
		fmt.Println("Answer not exits")
	}

	fmt.Println("The value:", v2, "Present?", ok2)
}

  

Reproduced in: https: //www.cnblogs.com/sunylat/p/6385852.html

Guess you like

Origin blog.csdn.net/weixin_30319097/article/details/94787152