First, declare variables
was a int was b esterification was c [] float32 was d func () bool was e struct { x int }
- Line 1, declare a variable of type integer, integer values can be saved.
- Line 2, declare a variable of type String.
- Line 3, declaring a 32-bit floating-point variable slice type, floating-point data structure represented by a plurality of slice floating point types.
- Line 4, declare a Boolean return value of a function of variables, this form is generally used for the callback function, the function is about to be preserved in the form of variables, when needed again call this function.
- Line 5, declare a variable of type structure, this structure has a field integer x.
var begins with the keyword, variable name to be declared in the middle, and its type on the back.
Go language variable declaration format is:
var variable name variable type
In addition you can also batch import
var ( a int b string c []float32 d func() bool e struct { x int } )
Second, variable initialization
var variable name = type expression
E.g:
was credits int = 100
The above code, 100 and for the same type int int, int can be considered redundant information can be further simplified wording initialized.
On the basis of the standard format, is omitted after the int, the compiler attempts hp variable type derived according to the expression on the right of the equal sign.
was credits = 100
for example:
var B = 40 var C = 20 is var D float32 = 0.17 // if the floating-point variables will not declare default float64 var E = float32 (BC) * D // , it is converted to a floating point type int and float points are calculated, otherwise reported int types and float32 the Mismatched FUNC main () { fmt.Print (E) }
Use structure:
package main import ( "fmt" ) type boby struct { id int name string age int } var b = boby{0, "jan", 25} func main() { fmt.Printf("The boy's name is %s", b.name) }
Also can write
package main import ( "fmt" ) type boby struct { id int name string age int } //var b = boby{0, "jan", 25} var b boby func main() { b.id=0 b.name="jan" b.age=25 fmt.Printf("The boy's name is %s", b.name) }