This is a creation in Article, where the information may have evolved or changed.
- Value types: All types, such as int, float, bool, and string, are value types that use variables of these types to point directly to a value that exists in memory, and the value of a value type's variable is stored in the stack. When using equal sign = Assigning the value of a variable to another variable, such as J = i, is actually a copy of the value of I in memory. The memory address of the variable I can be obtained by &i
- Reference types: Complex data often requires multiple words, which are typically saved using reference types. A variable of reference type R1 stores the memory address (number) where the value of R1 resides, or the location of the first word in the memory address, which is called a pointer, and the pointer is actually in a different word.
- A local variable must be used in the same code block after it is declared, otherwise it will get a compilation error, and the global variable allows the declaration without using
- If you want to exchange values for two variables (declared and assigned), you can simply use a, B = b,a, which is called parallel or simultaneous assignment; _ is actually a write-only variable, and we can't get its value because all the declared local variables must be used in the go language, but sometimes we don't need to use all the return values from a function, and the parallel assignment is also used when a function returns multiple return values. For example here Val and error err is obtained by calling the FUNC1 function at the same time: Val,err = Func1 ()
- IotaSpecialconstants, which can be thought of as a constant that can be modified by the compiler. When each const keyword appears, it is reset to 0, and the value automatically increases by 1 each time a iota occurs before the next const appears.
Iota can be used as an enumeration valuepackage mainconst ( a = iota b = iota c = iota)func main() { println(a, b, c)//0 1 2}
The first iota equals 0, and whenever Iota is used in a new row, its value is automatically incremented by 1, so the a=0,b=1,c=2 can be shortened to the following form (not quite):const ( a = iota b c)
The result is the same as before.
Iota Usage:package mainimport "fmt"func main() { const ( a = iota //0 b //1 c //2 d = "ha" //独立值,iota += 1 e //"ha" iota += 1 f = 100 //iota +=1 g //100 iota +=1 h = iota //7,恢复计数 i //8 ) fmt.Println(a,b,c,d,e,f,g,h,i) // 0 1 2 ha ha 100 100 7 8}
<<: Left-shift operator to shift all bits of a number to the left by a number of bits
Iota instances:package mainimport "fmt"const ( i=1<<iota j=3<<iota k l)func main() { fmt.Println("i=",i) fmt.Println("j=",j) fmt.Println("k=",k) fmt.Println("l=",l)}
The results of the above example run as follows:i= 1j= 6k= 12l= 24
Iota indicates that starting from 0 automatically add 1, so i=1<<0,j=3<<1 (<< means to move left), namely: I=1,j=6, this is no problem, the key in the K and L, from the output of the results see,k=3<<2,l=3< <3.
Remember it's messy, just look at it.