This is a creation in Article, where the information may have evolved or changed.
4.1 Defining variables
4.1.1 Var name type | Define a variable
4.1.2 var name1,name2 type | Define multiple variables
4.1.3 var name1 type = value | Defining variables and initializing values
4.1.4 var n1,n2,n3 type = v1, v2, v3 | Simultaneous initialization of multiple variables
4.1.5 var n1,n2,n3 = v1, v2, v3 | Short statement first edition, suitable for global variables
4.1.6 n1,n2,n3: = V1,v2,v3 | Short declaration of the final version, only suitable for internal functions, external cannot be compiled through
4.1.7 _,b:=34,35 | The underscore is a special variable name, and any value given to it will be discarded
4.2 Constants
4.2.1 Const NAME = value | Define a constant
4.2.2 Const Pi float32 = 3.1415926 | Defines a constant and specifies the type.
4.3 Built-in base type
4.3.1 bool type with a value of true or False
4.3.2 int and unit types, also some specified types: Rune, int8, Int16, Int64 and Byte, Unit8, UInt16, Uint32,uint64, where Rune is the nickname for Int32, and Byte is the nickname for Unit8. These types of variables are not allowed to be assigned to each other, and errors are encountered at compile time.
4.3.3 string string type, defined with a pair of "" or "
For example S1, s2, s3: = "A", "B", "C"
String values can not be directly modified, such as "Hello", Want to modify "H" for "C", there are two ways: 1, first turn to byte array after change back to string 2, using slice s2: = "D" + s1[1:]
4.3.4 error type, specifically for handling errors
4.3.4 Iota Enumeration, default is starting from 0
Const (
deleted = Iota
Checked
Invalid = 3
)
4.3.4 Go Programming Rules
Variables that begin with capital letters are exportable and are common variables
A variable that starts with a lowercase letter is non-exportable and is a private variable
A function that starts with a capital letter, is a common function
A function that starts with a lowercase letter, is a private function
4.3.5 Array
var arr [N]type | Define an array
arr:=[3]int{1,2,3} | Brief statement
arr:=[10]int{1,2,3} | Short statement, other defaults to 0
Arr:=[...] int{4,5,6} | Automatically calculates length based on the number of elements
doublearr:=[2][4]int{[4]int{1,2,3,4},[4]int{5,6,7,8}} | Declaring a two-dimensional array
doublearr:=[2][4]int{{1,2,3,4},{5,6,7,8}} | If the inner element is the same as the outer one, simplify the wording
An array is an immutable length.
The assignment between arrays is the assignment of a value, that is, when an array is passed as a parameter to the function transports, it is actually a copy of the array, not its pointer.
4.3.6 slice "Dynamic array": Not a dynamic array in real sense, but a reference type
var Fslice []int | Define a slice
Slice:=[]byte{' A ', ' B ', ' C '} | Define and initialize
b = Ar[5:10] | Take 5-10 fragments of data
4.3.7 Map Dictionary
var numbers map[string]int | Define a dictionary
Numbers: = Make (Map[string]int) |
Rating: = map[string]float32{"C": 5, "Go": 4.5, "Python": 4.5, "C + +": 2} | Initialization
Delete (Map,key) | Delete key
Maps are unordered, and each time you print out a map, it will be different.
The length of the map is not fixed, as is the case with slice, which is a reference type
4.3.8 make memory allocations for built-in types (map, slice, channel)
NEW: Initializes the object, returning the first pointer to the object. Can be used to initialize any type
Make: Returns an initialized instance, not a pointer.
4.4 If Else
A variable is allowed to be declared in a conditional judgment statement, which acts on the logical block of the condition and does not work elsewhere.
if x: = 1; X > 0 {
Fmt. Print ("X is true")
}
4.5 goto
Used to jump to a label defined within the current function
Func Main () {
I: = 0
Here:
println (i)
i++
If I < 10 {
Goto HERE
}
Fmt. Println ("Finish")
}
4.6 for
For exp1;exp2;exp3{
}
EXP1 and EXP3 are variable declarations or function call return values. EXP2 is used to determine the condition.
Func Main () {
Sum: = 0
For index: = 0; Index < 10; index++ {
Sum + = Index
}
Fmt. Println ("Sum is equal to", sum)
}
Func Main () {
MP: = Make (Map[string]int, 10)
Mp["a"] = 1
Mp["B"] = 2
For k, V: = Range MP {
Fmt. PRINTLN ("Map ' s key:", K)
Fmt. PRINTLN ("Map ' s value:", V)
}
}
4.7 Switch
Fallthrough can enforce the next case
5. Functions
5.1 Defining functions
Func funcName (input1 type1, Input2 type2) (output1 type1, Output2 type2) {
Here is the process logic code
Return multiple values
return value1, value2
}
5.2 Defining a variable parameter function
Func myfunc (arg ... int) {}
5.3 Pass-through value and pass-through pointer
The pointer uses multiple functions to manipulate the same object
The pointer is relatively lightweight (8bytes), just pass the memory address, when the large structure of the time, you can save (memory and time)
5.4 Defer
Deferred statement, which executes on the last side of the function
Remember: Defer is advanced after out, when a function defines multiple defer, the last one executes first.
5.5 functions as values, types
Type typeName func (input1 inputType1 [, Input2 inputType2 [, ...]) (Result1 ResultType1
[, ...])
5.6 Panic and recover
Func Main () {
Fmt. Println ("Hello")
Defer func () {
If err: = Recover (); Err! = Nil {
Fmt. PRINTLN (ERR)
}
}()
Panic ("I am Error")
}
5.6 Main and INIT functions
6. Import
7. struct Structure body