1, variable declaration
1) Standard format:
var variable name variable type
When a variable is declared, it is automatically given the value of zero types: int is 0, float to 0.0, bool is false, string is an empty string, the pointer is nil and the like. All memory is initialized in Go's.
2) batch format
var (
variable name 1 1 Variable Type
Variable Name Type 2 2 Variable
Variable Name Variable Type 3 3
)
3) short format
Variable name: = expression
Limit short form: define a variable, while explicitly initialized. Not provide data types. It can only be used within the function.
note:
Single variable declaration and the short assignment, since a: =, = not assigned, thus deriving a statement written left value of variable is not defined to be variable. If the definition is too, it will compile error.
In a number of short variable declarations and assignments, at least a new variable declaration appears in the left value, even if the other variable names may be repeated declarations, the compiler will not complain.
Example declaration:
package main import "fmt" Depending function1 () { var a, int b fmt.Printf ( "standard format: a =% v, b =% v \ n", a, b) } Function2 function () { where ( a int b float64 c [] int ) fmt.Printf ( "batch format: a =% v, b =% v, c =% v \ n", a, b, c) } func function3() { a, b := 1, "hello" fmt.Printf ( "short format: a =% v, b =% v \ n", a, b) } func main() { function1() function2() function3() }
2, variable initialization
1) a standard format
var variable name = type expression
2) type format compiler derived
var variable name = expression
On the basis of the standard format, will be omitted after the type, the compiler attempts type variable derived according to the expression on the right of the equal sign.
Initialization Example:
package main import "fmt" func main() { var x int = 100 // standard format var y = 998 // derived type format compiler fmt.Printf("x = %v, y = %v\n", x, y) }
3, multiple assignment
When multiple assignment, the left and right values of variables assigned from left to right.
Example:
package main import "fmt" func main() { x, y: = 100, 200 x, y = y, x fmt.Printf("x = %v, y = %v\n", x, y) // x = 200, y = 100 }
4, the anonymous variable
Anonymous variable: There is no variable name, type or method.
Anonymous variable is an underline "_", "_" itself is a special identifier, known as the blank identifier. As it can be used like any other identifier of a variable declaration or assignment (of any type can be assigned to it), but any value of the identifier assigned will be discarded, and therefore these values can not be used in the subsequent code, also this identifier may not be used as a variable assignment or calculation of other variables. When using anonymous variable, local variable declaration need only use underline replace.
Example:
package main import "fmt" func function()(int, int) { return 100, 200 } func main() { x, _ := function() _, y := function() fmt.Printf("x = %v, y = %v\n", x, y) // x = 100, y = 200 }
5, variable scope
Go language variables into three types, namely, local and global variables and formal parameters.
1) local variable
In vivo variable called local variables declared function, function in vivo scope thereof, only the parameters and return values of the variables are local variables.
Example:
package main import "fmt" func main () {// a, b, c are local variables There are a = 100 var b = 200 var c = a + b fmt.Printf("a = %v, b = %v, c = %v\n", a, b, c) }
2) Global Variables
In vitro function declaration variables called global variables, global variables can (after being exported) in outer cladding even the entire package.
Go language program in a global variable and local variable names are the same, but the function of the body's local variables will be considered first.
Example:
package main import "fmt" var x = 100 var y = 9.9 func main() { var y = 998 fmt.Printf("x = %v, y = %v\n", x, y) // x = 100, y = 998 }
3) in the form of parameters
It will be used as a parameter in the form of a local variable of the function.
Example:
package main import "fmt" func sum(a, b int) int { fmt.Printf("a = %v, b = %v\n", a, b) return a + b } func main() { sum(3, 4) }