Go language learning missing finding Day1

Author: Regan Yue

Source: Hang Seng LIGHT cloud community

Go language learning missing finding Day1

Zero. Preface

Because I have a weak foundation, I often encounter a lot of confused problems when using Go language, so I am determined to check and fill in the gaps in Go language. This [Go language checking and filling in] series is mainly to help novice Gopher better understand the mistakes, key and difficult points of Go language. I hope you can like it, praise it and pay attention to it!

1, Execution order of multiple defer s

package main

import "fmt"

func main() {
    defer fmt.Println("The first step of genius")
    defer fmt.Println("Freund's diapers")
    defer fmt.Println("Ares first step")
    defer fmt.Println("Gaia diapers")

}

In actual project development, we often encounter the situation of using multiple defers for delay processing. At this time, it is very important to understand the execution order of multiple defers when they exist at the same time.

The result of this code is:

Gaia diapers
 Ares first step
 Freund's diapers
 The first step of genius

That is, when multiple defer statements are called together, they follow the last in first out order.

2, The order in which defer and return are executed

package main

import "fmt"

func main() {
    fmt.Println("Main function:", d())
}
func d() int {
    i:=0
    defer func() {
        i+=10
        fmt.Println("First defer sentence:", i)
    }()
    defer func() {
        i++
        fmt.Println("Late arrival defer sentence:", i)
    }()
    return i
}

Take a look at the execution results first:

Late arrival defer sentence: 1
 First defer sentence: 11
 Main function: 0

Do you understand the execution sequence?

Students who understand can skip here. Let me explain. As mentioned earlier, the execution order of defer statements is last to first out. Therefore, in the two defers, the deferer statement is output first, and then the deferer statement that comes first is output. However, the main function outputs 0, which means that the return value is 0, which means that you return first and then execute the defer statement. And note that the function ends after defer is completed.

3, for...range... Creates a copy of each element

package main

import "fmt"

func main() {
    slice := []int{0, 1, 2, 3, 4, 5}
    m := make(map[int]*int)
    for key, value := range slice {
        m[key] = &value
    }

    for k, v := range m {
        fmt.Println("key=",k,"value=",*v)
    }
}

Let's take a look at the operation results first, and then describe in detail:

key= 0 value= 5
key= 1 value= 5
key= 2 value= 5
key= 3 value= 5
key= 4 value= 5
key= 5 value= 5

We will find that the key is OK, but the value is the same. Do you understand anything? M [key] = & value gets the address of value, which means that for...range... Generates a copy of each element, not a reference to each element. As for why it is 5 instead of others, it is because value is finally assigned 5. All values point to this address, so all values output are the same.

We can add a line of code and we can achieve what we wanted to achieve.

package main

import "fmt"

func main() {
    slice := []int{0, 1, 2, 3, 4, 5}
    m := make(map[int]*int)
    for key, value := range slice {
        v := value
        m[key] = &v
    }

    for k, v := range m {
        fmt.Println("key=",k,"value=",*v)
    }
}

The running result of the modified program is:

key= 3 value= 3
key= 4 value= 4
key= 5 value= 5
key= 0 value= 0
key= 1 value= 1
key= 2 value= 2

4, A little thing to pay attention to when creating slice with make

package main

import "fmt"

func main() {
    s1 := make([]int,3)
    s2 := make([]int,0)
    s1 = append(s1, 8, 8, 8)
    s2 = append(s2, 8, 8, 8)
    fmt.Println("s1=>",s1)
    fmt.Println("s2=>",s2)
}

The operation result is:

s1=> [0 0 0 8 8 8]
s2=> [8 8 8]

We can see that after three eights are attached, s1 is preceded by three zeros, while s2 is not preceded by zero. This means that when you create a slice with make, n zeros will be automatically filled in.

Tags: Programming

Posted on Fri, 03 Dec 2021 06:45:34 -0500 by devinemke