Notes on go multivariable assignment

Time:2021-2-11

When it comes to multivariable assignment, first calculate all relevant values, and then assign values from left to right, but this rule does not apply to python
Let’s take an example

package main

import "fmt"

func main() {
    Data, I: = [3] string {"Qiao Bangzhu", "Murong Fu", "Jiu Mozhi"}, 0
    i. Data [i] = 2, "master Kurong"
    fmt.Println(i, data)
}

Output results:

2 [Kurong master Murong Fujiu Mozhi]

Some friends will think that the result should not be like this(But the output of Python is as follows)?

2 [Master Murong Fu Kurong, leader of Qiao Gang]

As a matter of fact, let’s look at the understanding of assignment order

1 data, I: = [3] string {"Qiao Bangzhu", "Murong Fu", "Jiu Mozhi"}, 0
2 I, data [i] = 2, master Kurong // Note: first calculate all the relevant values, and then assign values from left to right
3 // here, the order of variable I is (I = 0, because the variable I in the previous line is 0) - > (then I = 2), (data [i] takes data [0] instead of data [2], that is, data [0] = Kurong)
four fmt.Println (I, data) // so the final output here is I = 2

The same multivariable assignment doesn’t work with Python

Data, I = ["Qiao Bangzhu", "Murong Fu", "Jiu Mozhi"], 0
i. Data [i] = 2, "master Kurong" # note that data [i] is already data [2], that is, data [2] = "master Kurong"
Print (I, data) # output 2 ['qiao Bangzhu ',' Murong Fu ',' Kurong master ']

In addition, we should pay attention to the difference between the re assignment and the definition of a new variable with the same name

package main

func main() {
    Name: "gang leader Qiao"
    println(&name)
    Name, age: "jiumozhi", 30 // reassignment: the same level code block as the previous name, and a new variable is defined.
    Println (& name, age) // usually, the return value err of the function will be reused.
    {
        Name, weight: = "qingfengyang", 50 // define a new variable with the same name: not in the same level code block.
        println(&name, weight)
    }
}

Output:

0xc00002bf78
0xc00002bf78 30
0xc00002bf68 50

Note: due to different personal machines, the memory reference address returned by you may not be the same as mine, but this step is the key. Here’s the point
The memory address will not change with the assignment of the same variable at the same level. The assignment of the same variable at different levels actually defines a new variable with the same name, that is, the memory address of the third line has changed.
Next, let’s look at an interesting piece of code

package main

func main() {
    Name: "gang leader Qiao"
    println(&name)
    Name, age: "jiumozhi", 30 // reassignment: the same level code block as the previous name, and a new variable is defined.
    Println (& name, age) // usually, the return value err of the function will be reused.

    Name, weight: = 100, 50 // define a new variable with the same name: not in the same level code block.
    println(&name, weight, age)

}

Output:

cannot use 100 (type int) as type string in assignment

The reason is very obvious, because the above: Name: “Qiao Bangzhu” has implicitly declared that name is a string, which is equivalent to var name string. At the same level, assign 100 to shape again. This is not allowed,

But: Here’s the point. Let’s change it a little bit

package main

func main() {
    Name: "gang leader Qiao"
    println(&name)
    Name, age: "jiumozhi", 30 // reassignment: the same level code block as the previous name, and a new variable is defined.
    Println (& name, age) // usually, the return value err of the function will be reused.
    {
        Name, weight: = 100, 50 // define a new variable with the same name: not in the same level code block.
        println(&name, weight, age)
    }
}

The difference is that the hierarchy has changed, because the name in {} is already a new variable.
Well, that’s the end of the introduction. Blog friends about the use of golang variables encountered in a variety of strange “pit”, please leave valuable footprints, welcome to brick message

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]