An emotionless teajar machine came again. On the first day of this holiday, I learned Go at home and also produced this article. Well, no more nonsense, let’s start talking about today’s topic — mapping, all for the big teajar.

preface

Mapping, as the name implies, means that through a point, another thing can be quickly found, that is, through a Key, another value can be quickly found, and in Go, such a thing is a data type — Map.

Unlike arrays and slicing, a Map’s key is not just a number, it can be any data type, so it is particularly free to use.

Declare a Map

To declare a Map, you must first specify what types of keys and values are in the Map

    a := map[string]int{
            "test":1."haha":2,}Copy the code

Similarly, it can be pre-allocated just like slicing, with make taking one or two parameters: the data type of the Map and the size of the Map.

    ints := make(map[string]int) 
    
    i := make(map[string]int.3)
Copy the code

In a make Map, the initial length is 0.

Pay attention to the little things

Access a key that does not exist

What happens if we access a key that doesn’t exist in the Map

    a := map[string]int{
            "test": 1,
    }
    fmt.Print(a["test"]) / / 1
    fmt.Print(a["hahah"]) / / 0
Copy the code

When we access a nonexistent key, it returns the corresponding zero value as the result (in this case, the return type is Int).

So in order to know if the key we are accessing exists, we provide a second argument when we are accessing it.

    a := map[string]int{
            "test": 1,}if i,ok := a["test"]; ok {
            fmt.Print(i)
    }
Copy the code

This second parameter, OK, tells us if the key you are accessing exists in the Map. It is a Boolean value, true if it exists, and false if it does.

The Map will not be copied

We know that when you pass an array, it gets copied, but maps don’t, and they share a common underlying data as they pass.

That is, if you change one, the other changes too

func changeMap(a map[string]int)  {
	for k,_ := range a{
		a[k] = 100}}func main(a) {
	a := map[string]int{
		"test": 1,
	}
	changeMap(a)
	fmt.Print(a) // map[test:100]
}
Copy the code

Here I iterate through the Map through the range method, changing the value inside it, and then we see that the Map passed in from the outer layer is also changed, which proves that the Map is not copied.

Map is used with slicing

When we use Map, we can also complete some functions with slices, such as grouping the contents of slices.

Here’s a simple example of grouping the numbers in a slice by ten digits

func group(a []int) map[float64] []int {
	ints := make(map[float64] []int)
	for _,v := range a{
		g :=  math.Trunc( float64(v) / 10) * 10
		ints[g] = append(ints[g],v)
	}
	return ints
}

func main(a) {
	a := []int {11.12.22.23.33.34.44.45}
	ints := group(a)
	fmt.Print(ints) 
        // map[10:[11 12] 20:[22 23] 30:[33 34] 40:[44 45]]
}
Copy the code

In addition to the small function of grouping slices, we know that many languages have a data structure called sets, and within sets there is a function for de-duplicating and sorting.

For example, in javascript, if you want to deduplicate an array, you can do that by putting the array into a collection, but in Go, there’s no collection data structure, However, we can also complete the work of deduplicating a slice through slicing and mapping.

func set(a []int) []int {
	ints := make(map[int]bool)
	list := make([]int.len(ints))
	for _,v := range a{
		ints[v] = true
	}
	for k,v := range ints {
		if v == true {
			list = append(list, k)
		}
	}
	return list
}

func main(a) {
	a := []int {11.11.22.22.33.33.44.33}
	ints := set(a)
	fmt.Print(ints)
        // [11 22 33 44]
Copy the code

From the two simple examples above, we can see the omnipotence of Map, which can do a lot of unexpected things.

The last

As a proper teajar machine, it is really unexpected how to end, so…..

I wish you all a happy holiday. I’m going out.