//Variable declaration
I: = 10; Declare and assign values directly
J: = 100;
Value of I,j=j,i//transform I,j
Fmt. Printf ("i=%d j=%d\n", i,j)//format lost
Fmt. Println ("i=", I);//cannot use%d etc.
var a int
var b [10]int
var c []int//array slice, equivalent to vector variable array
var s string
var p *int//pointer
var m map[string]int//key is a string type, value is int type
var fun func (a int) int//function
var st struct (//struct
f int
STR string
)
Or you can declare it together.
VAR (
v1 int
S1 string
)
//Anonymous variable
Func GetName () (Firstname,lastname,nickname string) {
Return "may", "Chan", "Chibi Maruko"
}
_,lastname,_: = GetName ()//Just want to take lastName value, others do not want, you can use _ to represent anonymous variables, you can mask off!
//Constant declaration
Const Pi float64 = 3.1415926
Const ZEOR = 0.0
Const (
Size Int64 = 1024
EOF =-1
)
Const AA,BB float64 = 1.0,2.0
Const A,B,C =, "foo"//Declaration and assignment together
Predefined constants: True false Iota
Iota is special, it is reset to 0 when Const appears, and then each time iota occurs before the next const appears, the number represented automatically increases by 1
For example:
Const
Sunday = Iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Numberofdays//This constant is not exported
)
Two methods of string traversal
str: = "Hello, World"
N: = Len (str)
For I: =0;i<n;i++{
CH: = Str[i];
Fmt. Println (I,ch)
}
Fmt. Println ("")
For i,ch: = Range str{//range has two return values, the first return value is an array subscript for the element, and the second return value is the value of the element
Fmt. Println (I,ch)
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Go Language Learning Notes