Variable than any one language which is the most basic.
If the metaphor of a memory cell of a grid storage boxes neatly arranged, then the variable is to identify each storage box, we accessed through the computer's memory variables. The program is not variable terrible for humans, we need to use digital memory location to locate the grid, Human extremely good at such a thing. This is like a year and a half or so children have not learned many nouns, the object can only be pointing to express their preferences by hand.
Variables let the program logic there are plenty of forms of expression.
Three ways defined variables
Go language variables defined in many forms, we look at the most complicated form
package main
import "fmt"
func main() {
var s int = 42
fmt.Println(s)
}
输出-----42
Note that we use the var keyword, which is used to explicitly define variables.
Also noted that after the variable name s declaration of a variable shaping type int, then give it an initial value assigned on 42. The above variables can be defined to simplify the type removed because the compiler will automatically derive the variable type, the effect is the same, as follows
package main
import "fmt"
func main() {
var s = 42
fmt.Println(s)
}
---------------42
Still further, the above definition of the variables again can be simplified, removing var keyword.
package main
import "fmt"
func main() {
s := 42
fmt.Println(s)
}
---------------42
Notice of assignment equal sign becomes :=,它表示变量的「自动类型推导 + 赋值」
.
These three variables defined in ways that are possible, each with its advantages and disadvantages. Readability of the strongest is the first, to write the most convenient is the third, and the second is between form between the two.
The type of a variable is a symbol of status, if a variable does not care so much about their own identity, in the form that can be casually. var means that tells the reader, "I am important, you should pay attention": = mean to tell the reader, "I am free, do not take me seriously." var then put explicit type information for the convenience of the reader to quickly identify the identity of the variable.
If a variable is very important, it is recommended to use the first explicit statement to define the type of way, such as the definition of global variables would prefer the first definition mode. If you want to use a less important local variables, you can use a third. Such loop index variable
for i:=0; i<10; i++ {
doSomething()
}
The second way that can not be used in the above cycle index in it, the answer is no, you can not be directly written into the var keyword cycling conditions in the initialization statement, and variables must be declared in advance, like this, this when it is significantly better than the abbreviated form
var i = 0
for ; i<10; i++ {
doSomething()
}
If at first you declare a variable when no initial value, the compiler will automatically give the appropriate type of "zero value" zero value of different types, such as strings of zero value is not nil, but the empty string the value is 0, the zero value of zero plastic Boolean type is false.
package main
import "fmt"
func main() {
var i int
fmt.Println(i)
}
-----------0
Global and local variables
The variables we write in the code example above is a local variable, which is defined inside a function, the function call ended it disappeared. Corresponding to global variables, during running, it has always existed, it is defined outside of the function.
package main
import "fmt"
var globali int = 24
func main() {
var locali int = 42
fmt.Println(globali, locali
)}
---------------24 42
If the first letter capitalized global variable, then it is open global variables. If the first letter lowercase global variables, then it is an internal global variables. Internal global variables only within the current package can access the code, the code is not visible outside of the package.
Learned C language students might ask, Go language, there is no static variables? The answer is no.
Variables and Constants
Go language also provides a constant keyword const, used to define constants. Constants can be global constants can also be a local constants. You may not modify constants, otherwise the compiler will complain. Constants must be initialized, because it is not a secondary assignment. Global variables and constants capitalization rules are consistent.
package main
import "fmt"
const globali int = 24
func main() {
const locali int = 42
fmt.Println(globali, locali)
}
Pointer types
Go language is called C language of the Internet age, it continues to use the pointer type C language.
package main
import "fmt"
func main() {
var value int = 42
var pointer *int = &value
fmt.Println(pointer, *pointer)
}
--------------0xc4200160a0 42
We saw a long absence, the symbol * pointers and address operators & take on the function and use almost exactly the same with the C language. With C language, pointers also supports two pointer, three pointer, but in everyday applications, rarely encountered.
package main
import "fmt"
func main() {
var value int = 42
var p1 *int = &value
var p2 **int = &p1
var p3 ***int = &p2
fmt.Println(p1, p2, p3)
fmt.Println(*p1, **p2, ***p3)
}
----------0xc4200160a0 0xc42000c028 0xc42000c03042 42 42
It is essentially a pointer variable integer variable, the value of another variable which is stored in the memory address. And the symbol * & just syntactic sugar it is easy to use and understand the form of a pointer. * Operator exists in two memory read, obtain the value of the first pointer variable, i.e. a memory address, and then get another variable contents of this memory address is located.
If the common variable is a storage box, so that is another pointer variable storage box, the storage box to store the keys where the common variable storage box. Read pointer by a multistage variable values like playing a puzzle game.
Go language foundation types Daquan
Go language defines a very rich foundation type, below I have listed all the basic data types.
package main
import "fmt"
func main() {
// 有符号整数,可以表示正负
var a int8 = 1 // 1 字节
var b int16 = 2 // 2 字节
var c int32 = 3 // 4 字节
var d int64 = 4 // 8 字节
fmt.Println(a, b, c, d) // 无符号整数,只能表示非负数
var ua uint8 = 1
var ub uint16 = 2
var uc uint32 = 3
var ud uint64 = 4
fmt.Println(ua, ub, uc, ud) // int 类型,在32位机器上占4个字节,在64位机器上占8个字节
var e int = 5
var ue uint = 5
fmt.Println(e, ue) // bool 类型
var f bool = true
fmt.Println(f) // 字节类型
var j byte = 'a'
fmt.Println(j) // 字符串类型
var g string = "abcdefg"
fmt.Println(g) // 浮点数
var h float32 = 3.14
var i float64 = 3.141592653
fmt.Println(h, i)
}
-------------1 2 3 41 2 3 45 5trueabcdefg3.14 3.14159265397
Reproduced in: https: //www.jianshu.com/p/62e24569e2aa