This is a creation in Article, where the information may have evolved or changed.
Package indicates the packages where the source files are located
Func is used to define functions
Go is used in addition to the for-loop initialization, and most of the others do not need
There is no while loop in go, but you can use the for condition to simulate while
Compile:
Go Builde compiled file name
Define a variable, such as a string
var s string = ""
Go also automatically derives the type
var s = "";
You can also s:= ""
: = used in value declaration and initialization
Package main07 Import ( "OS", " flag" /command line option Parser10 ) var Omitnewline = flag. Bool ("n", False, "Don t print final newline") + const ( Space = "" newline = "\ n")- func ma In () { flag. Parse () //Scans the ARG list and sets up Flags21 var s string = "", for I: = 0; I < flag. Narg (); i++ { If i > 0 {+ s + = Space25 }26 s + = flag. ARG (i) }28 if!*omitnewline { s + = Newline30 }31 os. Stdout.writestring (s) + }
The go language is the same size as int, float, and int8,int32,int32, but unlike int, byte is the same size as int8, and the same type
S:= "string" is somewhat similar to the C-language char *str = "string", so that the contents of the string is not allowed to be modified, only the point of STR is changed
If you want to create a string that you can modify, you must use an array
declares an array s:= []int{1,2,3}
var arrayofint [3] int{1,2,3}, defines an array, but arrayofint cannot degenerate into a pointer
a:= [2][2]int{[2]int{1,2},[2]int{3,4}}
Slice
s:= [3]int{1,2,3};
SL = S[1:2];
Fmt. Printf ("%d\n", Sl[0])
Output is 2
The map in go uses make to create
You can use range to iterate through an array, Map,slice
For _;days = Range Monthdays {
Fmt. Printf ("%d\n", days);
}
Function:
def func (xs []int)
This function is passed in a splite instead of an array, when using this function: Fun (array[:]) instead of func (array), it will prompt that the array cannot be converted to []int