“This is the 19th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”.

Go + ing + Day1

First, the order in which the multiple deferred defer are executed

package main
​
import "fmt"func main(a) {
    defer fmt.Println("Genius's First steps.")
    defer fmt.Println("Finch's Diapers.")
    defer fmt.Println("God of War in the First Step.")
    defer fmt.Println("Gaia diapers.")}Copy the code

In actual project development, it is often the case that multiple deferments are used for delay processing, and it is important to understand the order in which they are executed when they exist at the same time.

The result of this code is:

Gaia diaper god first step Finch diaper genius first stepCopy the code

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

What is the sequence of defer and return

package main
​
import "fmt"func main(a) {
    fmt.Println("Principal function:", d())
}
func d(a) int {
    i:=0
    defer func(a) {
        i+=10
        fmt.Println("The first defer statement :", i)
    }()
    defer func(a) {
        i++
        fmt.Println("The deferred statement :", i)
    }()
    return i
}
​
​
Copy the code

Let’s take a look at the results:

After thedeferStatement:1Here firstdeferStatement:11Main function:0Copy the code

Do you understand the order of execution?

Those who understand can skip this and let me explain. Previously, we said that the execution order of the defer statement is last to first out. So the two deferred statements are output first, then the defer statements, and then the first defer statements. But the main function prints 0, which means that the return value is 0, which means that you return first and then execute the defer statement. Also note that the function ends after defer has finished.

Three, for… range… You create a copy of each element

package main
​
import "fmt"func main(a) {
    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)
    }
}
​
Copy the code

Let’s take a look at the results and then go into details:

key= 0 value= 5
key= 1 value= 5
key= 2 value= 5
key= 3 value= 5
key= 4 value= 5
key= 5 value= 5
Copy the code

We’ll see that the key is fine, but the value is the same. Is that clear? M [key] = &value range… A copy of each element is generated, not a reference to each element. And the reason why it’s always 5 and not something else is because value is assigned 5 at the end. And all the values point to this address, so all the values are the same.

We can add a line of code, and we can do what we want.

package main
​
import "fmt"func main(a) {
    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)
    }
}
​
Copy the code

The modified program results are as follows:

key= 3 value= 3
key= 4 value= 4
key= 5 value= 5
key= 0 value= 0
key= 1 value= 1
key= 2 value= 2
Copy the code

Create a slice with make

package main
​
import "fmt"func main(a) {
    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)
}
​
Copy the code

The results are as follows:

s1=> [0 0 0 8 8 8]
s2=> [8 8 8]
Copy the code

We can see that after append has three 8s, S1 has three zeros in front of it, and S2 has none. This means that using make to create slice will automatically fill n zeros.