This is a creation in Article, where the information may have evolved or changed.
1. For range structure
In the go language, the loop structure has only one keyword for, but its loop mode is still diversified.
For init;condition;post{
This is the standard for loop structure in C, with initial values, conditions, and steps.
For condition{
This is the analog C while loop, which needs to change the loop variable in the loop body
for{
This is the infinite loop, which is equivalent to the for in C (;;)
The go language also has a specific structure, for range
Package Mainimport "FMT" Func Main () { language:=[]string{"C", "C + +", "Python", "Go", "Java"} for key,value:= Range language{ FMT. Printf ("%d%s\n", Key,value) }}
The For range structure returns the index and value, and the result is as follows:
2. VAR () define Variables
You can use Var to define different types of variables in the Go language
Package Mainimport "FMT" var ( a int = ten b float32 = 3.1415926 c bool = True d []int = []int{1,2,3,4,5}) Fun C Main () { fmt. Printf ("%T%v\n", a,a) FMT. Printf ("%T%v\n", b,b) FMT. Printf ("%T%v\n", c,c) FMT. Printf ("%T%v\n", d,d) }
Notice how the array is defined,%t the type of the output variable,%v the value of the output variable, and run the result as follows:
For variables that do not have an assignment, the go language is initialized to 0,false, or an empty string.
3. Type conversion
Package Mainimport "FMT" Func Main () { var a int = 5 var b float32 = 9.9 var c float32 = float32 (a) var d int = Int (b) FMT. Printf ("%v%v\n", C,d)}
The T (v) statement converts the type of the V variable to type T, and the result is as follows:
This syntax is similar to the type in Python, and the decimal place is removed when floating-point type is rounded.
4, the definition of constants
Package Mainimport "FMT" Func Main () { const A = 10}
defined by the Const keyword and cannot be defined by: =