I. Create the first go language program: print Hello world!
Package Mainimport "fmt" func main () { Fmt. Println ("Hello world")}
Run the Go language:
Go Run main.go
Summary:
Switch to the directory where Main.go is located, go run main.go runs the MAIN.GO program and outputs "Hello world". The Go Run command is followed by the files to be compiled and run, and multiple files are separated by spaces, and go run compiles the Go program file into an executable program and places it under a temporary directory. If the program has an error, the go compiler will indicate where the error occurred.
The Import keyword allows the code contained in the other package to be used in the current program, such as the import "fmt" of main.go, where the FMT package implements formatted input and output, and the import must be enclosed in "" when including Packages.
Comment A line of code in Go using//, the commented code go compiler ignores, comments just improve the readability of the code, if you need to comment on multiple lines of code, go provides another way:/* ... the code between/* and * * is ignored by the go compiler.
Two: Declaration and initialization of Variables:
The variables in the Go language introduce the Var keyword, and type information is placed after the variable
Eg1: declaring and initializing a variable
Package Mainimport "fmt" func main () { var m int = Ten Fmt. Println (m)}
Output Results 10
Eg2.1: declares initialization of multiple variables:
Package Mainimport "fmt" func main () { var a,b,c = 1,3,8 Fmt. Println (a,b,c)}
Output Results: 1 3 8
eg2.2 Anonymous variables
Package Mainimport "fmt" func main () { var a,_,c = 1,3,8 Fmt. Println (a,c)}
Output is 1 8
Explanation: This situation is mainly used when the function has multiple return values, to remove one or more of the values we need to use, if not in _, direct Fmt. PRINTLN (a,c), will be error
Another use of the Eg3:var keyword is that you can put several variables that need to be declared together, lest the programmer need to write the var keyword repeatedly, as follows:
Package Mainimport "fmt" var ( v1 int v2 String) func main () { v1 = Ten v2 = "subao" Fmt. Println (v1,v2)}
Output Information: Ten Subao
Eg4: the var keyword can be preserved for scenarios that require initialization when declaring variables, but are no longer necessary elements, as Follows:
Package Mainimport "fmt" func main () { var v1 int = var v2 = V3: = . Println (v1,v2,v3)}
Output Results 88 88 88
The effect of the above three usages is exactly the Same.
Note: the left-hand variable should not have been declared, otherwise it will cause an error.
Constants: in the Go language, constants are values that are known and immutable during Compilation. Constants can be types of numeric values (including integer, float, boolean, string, etc.)
Two. values: Value Types
The go language has the following basic types built in
Boolean type: bool
Integral type: int8,byte,int16,int,int16,uint,uintptr, etc.
Floating-point types: float32, float64
Plural type: complex64, complex128
String Type: string
Character Type: Rune
Fault Type: Error
In addition, the Go language supports complex types:
Pointer (pointer)
Arrays (array)
Slicing (slice)
Dictionary (map)
Channel (chan)
struct (struct)
Interface (interface)
Boolean type:
Package Mainimport "fmt"//boolean func main () { var v1 bool V1 = True Fmt. Println (v1)} Boolean cannot accept other types of assignment and does not support automatic or forced type conversions, which is wrong
Func main () { var b bool B = 1 Fmt. Println (b)}
Output: true
The following error will be error:
./hello.go:14:cannot use 1 (type int) as type bool in assignment
Integer: integer is the most basic data type in all languages, go supports the integer type shown
I: Representation of the Type:
The first thing to say is that int and int32 are considered to be two different types in the Go language, and the compiler will not help you do the type conversion automatically, giving an example of the error
Package Mainimport "fmt" func main () { var value2 int32 value1: = value2 = value1 Fmt. Println (value2)}
Will Appear:
./hello.go:24:cannot use value1 (type Int) as type int32 in Assignment
If you do encounter such errors in the future, you need to cast the type, the correct way is:
Package Mainimport "fmt"//integer func main () { var value2 int32 value1: = value2 = Int32 (value1) (cast) fmt . Println (value2)}
Correct output: 64
Ii: numerical operations: Give an example of addition:
Package Mainimport "fmt"//numeric operation Func main () { var v1,v2,v3 int v1 = V2 = V3 = V1 + v2 Fmt. Println (v3)}
Output Results: 96
Iii: comparison operation, as in most other languages, several more common operators:>, <, = =, >=, <=,! =
Package Mainimport "fmt"//comparison operation Func main () { var v1,v2 int v1 = V2 = + If v1 >= v2 {fmt. Println ("v1 greater than V2") }}
Output: v1 greater than V2
Note: two different types of integers cannot be directly compared, such as the number of int8 types and the number of int cannot be directly compared, but all types of integer variables can be directly compared with literal constants (literal)
Iv: Bitwise operations
Floating point Type:
I. Representation of floating-point numbers:
Package Mainimport "fmt" func main () { var f1 float32 f1 = F2 : =12.0 If f1 = F2 { Fmt. Println ("equal") }}
Error:./hello.go:9: invalid OPERATION:F1 = = F2 (mismatched types float32 and Float64)
Cause: in the example above, the type is automatically pushed to the f2, it is important to note that its type is automatically set to float64, regardless of whether the number given to him is expressed in 32 length. So we have to force the type conversions to be compared:
Package Mainimport "fmt" func main () { var f1 float32 f1 = F2 : =12.0 If f1 = float32 (f2) { Fmt. Println ("equal") }}
Output: equal
Ii: floating-point comparison
Because the floating-point number is not a precise expression, so it is not feasible to use = = To determine the integer type, (the example above is a special case), which leads to unstable results.
Plural type:
I. The representation of the Plural:
Package Mainimport "fmt" func main () {var value1 complex64value1 = 3.2 + 12ivalue2: = 3.2 + 12ivalue3: = Complex (3.2,12) val Ue4: = Real (value3) value5: = imag (value3) Fmt. Println (value1,value2,value3,value4,value5)}
Output: (3.2+12i) (3.2+12i) (3.2+12i) 3.2 12
Ii. Real and imaginary parts
For a complex value3: =complex (3.2,12), You can get the real number of the complex by using the Go language built-in function real (z), that is, 3.2, by Imag (z) to obtain the imaginary number of the complex number.
String:
I: String manipulation:
Package Mainimport "fmt" func main () { var str string str = ' Hello world ' ch: = str[0] Fmt. Printf ("the length of \"%s\ "is%d \ n", str, len (str)) Fmt. Printf ("the first character of \"%s\ "is%c.\n", str,ch)}
Output information:
The length of ' Hello World ' is 11
The first character of ' Hello World ' is H.
String traversal:
A. Traversing a string (bytes)
Package Mainimport "fmt" func main () { var str2 = "hello" str3: = "world" //string merge str4: = str2 + "" + str3
//traversal string (bytes) for i:=0;i<len (str4); i++ { Fmt. Printf ("%d:%d\n", i, str4[i]) }}
Output result:
0:104
1:101
2:108
3:108
4:111
5:32
6:228
7:184
8:150
9:231
10:149
11:140
You can see that a Chinese character is three bytes, and the concatenation of strings is also available.
B. Characters traversing a string:
Package Mainimport "fmt" func main () { var str2 = "hello" str3: = "world" //string merge str4: = str2 + "" + str 3 //traversal string (character) for k,v: = range (str4) { Fmt. Printf ("%d:%x:%c\n", k, v, v) }}
Output:
0:68:h
1:65:e
2:6c:l
3:6c:l
4:6f:o
5:20:
6:4e16: the World
9:754c: bounded
Array: An array is one of the most common data structures in Go programming, as the name implies, an array is a collection of a series of Figures. Each data contained in an array is called an array element, and the number of elements contained in an array is called the length of the Array.
Array must know: in the go language, the length of an array cannot be changed after it is defined, and the length can be a constant or a constant expression when declared (a constant expression is an expression that evaluates the result at compile time). The length of the array is a built-in constant for the array type, which can be obtained using the Go Language's built-in function len (). Method:
Arrlength: =len (arr)
I: How arrays are created:
Package Mainimport "fmt" func main () { var a [10]int var b = [10]int{0,1,2,3,4,5,6,7,8,9} var c = [...] int{0,1,2,3,4,5,6,7,8,9} var d = [2][2]int{[2]int{1,1},[2]int{2,2}} var e = [2][2]int{{1,1},{2,2}} Arrlength: =len (a) Fmt. Println (a,b,c,d,e,arrlength)}
Output information: [0 0 0 0 0 0 0 0 0 0] [0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9] [[1 1] [2 2]] [[1 1] [2 2]] 10
Explanation: A is not assigned, the default is 0;b and C implementation of the function is the same, take the integer less than 10, d,e is a two-dimensional array, which represents a double-row, two-column integer array, A total of four elements; arrlength is the length of the obtained function
Ii: access to Elements:
You can use an array subscript to access the elements in the array, the array subscript starts at 0, and Len (array)-1, which indicates the subscript of the last Element. Here's How to iterate through an array of integers and print the contents of the Elements individually:
Package Mainimport "fmt" func main () { var c = [...] int{0,1,2,3,4,5,6,7,9} for I: = 0; i < len (c); i++ { Fmt. Println ("Element", i, "of C is", c[i]) }}/*func main () { var c = [...] int{0,1,2,3,4,5,6,7,9} for i,v: = range C { Fmt. Println ("c element[", i, "]=", v) }}*/
The above two functions can traverse an integer array, and the output is:
C element[0]= 0 Element 0 of c is 0
C element[1]= 1 element 1 of C is 1
C element[2]= 2 element 2 of C is 2
C element[3]= 3 element 3 of C is 3
C element[4]= 4 Element 4 of C is 4
C element[5]= 5 element 5 of C is 5
C element[6]= 6 Element 6 of C is 6
C element[7]= 7 Element 7 of C is 7
C element[8]= 9 Element 8 of C is 9
And the above example shows that C has two return values, the first return value is an array subscript, and the second return value is the value of the Element.
III Value Type:
Not to be continued ...
Initial Go language