Share the learning go website:
Foreign websites: https://golang.org
go programming website: https://golang.google.cn/ (go packages and functions can be viewed here)
Rookie tutorial - Go: green hand
Yibai tutorial - Go: Yi Bai tutorial
W3c-Go: w3c
Geek Go: Geek
Go advanced programming
GO language library: https://studygolang.com/pkgdoc (common)
1.GO language notes
//Single-Line Comments /* multiline comment */
2. Escape characters of go language
\t : Alignment function \n: Newline character \": One character \r: enter
code:
package main import "fmt" func main() { fmt.Println("hello\tdadaw") fmt.Println("hello\ndadaw") fmt.Println("hello\rdadaw") fmt.Println("hello\"dadaw") }
effect:
3. Basic types of go language
3.1 digital type
Data type work is to divide the data into data with different memory sizes and make full use of them
- uint8 bit integer (0 to 255)
- uint16 16 bit integer (0 to 65535)
- uint32 32-bit integer (0 to 4294967295)
- uint64 64 bit integer (0 to 18446744073709551615)
- int8 8-bit integer (- 128 to 127)
- int16 16 bit integer (- 32768 to 32767)
- int32 32-bit integer (- 2147483648 to 2147483647)
- int64 64 64 bit integer (- 9223372036854775808 to 9223372036854775807)
- uintptr unsigned integer used to store a pointer
3.2 floating point
float32 32-bit floating point number
float64 64 bit floating point number
complex64 32-bit real and imaginary numbers
complex128 64 bit real and imaginary numbers
3.3 Boolean
(a)true (b)false
3.3 string type
Pre declared string
3.4 derived types
(a) Pointer type
(b) Array type
© structure type
(d) Union type
(e) Function type
(f) Slice type
(g) Function type
(h) Interface type
(i) Type
4.GO language variables
go is a statically typed language, so the declared variables must be explicitly typed, generally using the var keyword
Variable = variable name + variable value + type
Variable specification: variables are composed of letters, numbers and underscores, and cannot start with numbers
-Standard declaration variable format:
var Variable name variable type var Variable name,Variable name *Variable type
code:
var num int var num,num2 *int
-Batch declaration format:
var ( a int b string c []float32 d func() bool e struct { x int } )
- Short format: (variables that declare data types cannot be output in short format)
- Cannot provide data type
- Can only be used inside a function
- Defined variables, display initialization
name:= expression
code:
func main() { a:=100 c,d:=50,"huawei" }
Summary of common declaration assignment
package main import "fmt" //Globally declared variable type var ( name string //character string age int //Integer type salary float32 //float ) func main() { //The following are local variables // Short expression a, b, c := 100, 100.12345, "Hello" fmt.Println(a) fmt.Println(b) fmt.Println(c) //Declare multiple variable type expressions () var d, e, f = 18.5, "Xiao Wang", 4500 fmt.Println(d, e, f) //Here, the type declared by the global variable is used to use the output name = "huawei" age = 18 salary = 1000 fmt.Println(name) fmt.Println(age) fmt.Println(salary) } Output: 100 100.12345 Hello 18.5 Xiao Wang 4500 huawei 18 1000
5.GO language constants
After a const ant is generally defined, it will not be modified when the program is running, except for iota
- Format of constant definition: (variable type can be omitted, and the compiler can automatically infer the type when defining the value)
const defintion [type] = values
- Multiple constant definitions of the same type:
const defintion_1,defintion_2 = value01,value02
Case of variable:
package main import "fmt" //The defined variables are generally in the global scope const language string = "GO" const scholar = "caicai" func main() { //Use variables within local variables fmt.Println("this is const") fmt.Println("language:", language, "scholar:", scholar) } Run output: this is const language: GO scholar: caicai
Special constant iota
iota simple note: i/o is a constant that can be changed. When encountering a const keyword, it will be reset to 0, and the number in const will be automatically increased by 1. It is directly compared to a constant counter
iota's case:
package main import "fmt" func main() { fmt.Println("this is iota") const ( a = iota //a=iota start count, a=0 b //1 c //2 d //3 e //4 ) fmt.Println(a, b, c, d, e) const ( aa = iota //aa=iota start count, aa=0 bb = "linux" //bb gives a separate value, and iota counts as 1 (no counting number is displayed, only the given value is displayed) cc = "java" //cc is given a unique value, and iota counts as 2 (no counting number is displayed, only the given value is displayed) dd = "python" //dd is given a unique value, and iota counts 3 (no counting number is displayed, only the given value is displayed) ee = iota //ee has no separate value, and the recovery count is 4 (display count number) ) fmt.Println(aa, bb, cc, dd, ee) const ( aaa = iota bbb = "Romance of the Three Kingdoms" ccc = 500 ddd = 600 //ddd assigns a separate value, and iota counts 3 (no counting number is displayed, only the assigned value is displayed) eee //eee inherits the assigned value of ddd. At this time, iota counts 4 (no counting number is displayed, only the assigned value is displayed) fff //fff inherits the assigned value of eee. At this time, iota counts 5 (no counting number is displayed, only the assigned value is displayed) ggg = iota //Recovery count, ggg=6 ) fmt.Println(aaa, bbb, ccc, ddd, eee, fff, ggg) } Run output: this is iota 0 1 2 3 4 0 linux java python 4 0 Romance of the Three Kingdoms 500 600 6
Use of iota in kubernetes
For pod Component Success,Error,Unscheduleable,UnschedulableAndUnresolvable,Wait,Skip These six status fields The effect of expression is Success=0,Error=1 etc.
package main import "fmt" func main() { type Code int //Define an integer type of Code const ( // Success means that plugin ran correctly and found pod schedulable. // NOTE: A nil status is also considered as "Success". Success Code=iota //Start counting, Success=0 // Error is used for internal plugin errors, unexpected input, etc. Error //iota increases by 1, Error=1 // Unschedulable is used when a plugin finds a pod unschedulable. The scheduler might attempt to // preempt other pods to get this pod scheduled. Use UnschedulableAndUnresolvable to make the // scheduler skip preemption. // The accompanying status message should explain why the pod is unschedulable. Unschedulable //iota increases by 1, unscheduled = 2 // UnschedulableAndUnresolvable is used when a PreFilter plugin finds a pod unschedulable and // preemption would not change anything. Plugins should return Unschedulable if it is possible // that the pod can get scheduled with preemption. // The accompanying status message should explain why the pod is unschedulable. UnschedulableAndUnresolvable //iota increases by 1, unscheduleandunresolved = 3 // Wait is used when a Permit plugin finds a pod scheduling should wait. Wait //iota increases by 1, Wait=4 // Skip is used when a Bind plugin chooses to skip binding. Skip //iota increases by 1, Wait=5 ) fmt.Println(Success, Error, Unschedulable, UnschedulableAndUnresolvable, Wait, Skip) } Output display: [Running] go run "/root/workspace/goproject/src/go_code/project01/main/kubernet-iota.go" 0 1 2 3 4 5
6.GO language operator
6.1GO operation classification
Arithmetic operator
Relational operator
Logical operator
Bitwise Operators
Assignment Operators
Other Operators
6.2 arithmetic operation
operator describe expression + Add A + B - subtract A - B * Multiply A * B / be divided by A/ B % Seeking remainder A % B ++ Self increasing A++ -- Self subtraction A--
example
package main import "fmt" func main() { a := 20 b := 10 var c int c = a + b fmt.Println(c) c = a * b fmt.Println(c) c = a / b fmt.Println(c) c = a % b fmt.Println(c) a++ fmt.Println(a) b++ fmt.Println(b) } Run output: [Running] go run "/root/workspace/goproject/src/go_code/project01/main/arich.go" 30 200 2 0 21 11
6.3 relation operation
== Check whether the two values are equal, and return if they are equal True Otherwise return False. (A == B) by False != Check whether the two values are not equal. If not, return True Otherwise return False. (A != B) by True > Check whether the left value is greater than the right value. If yes, return True Otherwise return False. (A > B) by False < Check whether the left value is less than the right value. If yes, return True Otherwise return False. (A < B) by True >= Check whether the left value is greater than or equal to the right value. If yes, return True Otherwise return False. (A >= B) by False <= Check whether the left value is less than or equal to the right value. If yes, return True Otherwise return False. (A <= B) by True
6.4 logic operation
&& logic AND operator If both operands are True,Then condition True,Otherwise False || logic OR operator If both operands have one True,Then condition True,Otherwise False ! logic NOT operator If the condition is True,Then logic NOT condition False,Otherwise True
6.5 assignment operation
= Expression assignment, given to the left += Additive reassignment -= Subtraction reassignment *= Multiply and assign /= Divide and assign %= Assign value after remainder <<= Left shift assignment >>= Assignment after shift right &= Bitwise and post assignment ^= Assignment after bitwise XOR |= Bitwise or post assignment
6.6 bit operation
a b a&b a|b a^b 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1
6.7 other operations
& Return variable storage address * Pointer variable
7go conditional statement
if statement
if else statement
If else else nested statement
switch Statements
7.1if statement
if Boolean expression { /* Executes when the Boolean expression is true */ }
7.2if else statement
d := 400 e := 500 if d >= e { fmt.Println("d>=e") } else { fmt.Println("d<e") } Run output: d<e
7.3if nested statements
if Boolean expression noe { /* Executes when the Boolean expression one is true */ if Boolean expression two { /* Executes when the Boolean expression two is true */ } }
7.4 switch statement
switch statements are used to perform different actions based on different conditions. Each case branch is unique and tested one by one from top to bottom until it matches.
The switch statement executes from top to bottom until a match is found, and there is no need to add a break after the match.
By default, the switch contains a break statement at the end of the case. After successful matching, other cases will not be executed. If we need to execute the following cases, we can use fallthrough
package main import "fmt" func main() { var grade string = "A" var marks int = 90 switch marks { case 90: grade = "A" case 80: grade = "B" case 70: grade = "C" case 60: grade = "D" default: grade = "E" } switch { case grade == "A": fmt.Println("excellent\n") case grade == "B": fmt.Println("good\n") case grade == "C", grade == "D": fmt.Println("commonly\n") case grade == "E": fmt.Println("Very bad\n") } } Run output: excellent
8.go loop statement
8.1 for loop statement format:
for Variable initial condition; Cycle condition; Variable iteration{} for Cycle condition{} for {}
Output 5 times: days in February and places in March
package main import "fmt" func main() { for init := 1; init <= 5; init++ { fmt.Println("2 The day of March, the land of March") } } Run output: [Running] go run "/root/workspace/goproject/src/go_code/project01/main/for.go" 2 The day of March, the land of March 2 The day of March, the land of March 2 The day of March, the land of March 2 The day of March, the land of March 2 The day of March, the land of March
Output 30-60 digital
package main import "fmt" func main() { for i := 30; i <= 60; i++ { fmt.Println(i) } } Run output: [Running] go run "/root/workspace/goproject/src/go_code/project01/main/for.go" 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
8.2 loop control statement
break: interrupt for Loop, jump out switch sentence continue: Jump out of the current cycle and continue to the next cycle goto: You can unconditionally transfer to the line specified in the procedure
break conditionally aborts the output of the loop body
Output 30-60 numbers, but not 50-60
package main import "fmt" func main() { for i := 30; i <= 60; i++ { fmt.Println(i) if i > 50 { break //When the cycle reaches i=50, it will jump out of the cycle directly } } } Operating cycle: [Running] go run "/root/workspace/goproject/src/go_code/project01/main/for.go" 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Print 99 multiplication table
%d: The output width is also the output decimal integer printf: format output function
package main import "fmt" func main() { //Processing nine elements for y := 1; y <= 9; y++ { // Processing columns for rows of y for x := 1; x <= y; x++ { fmt.Printf("%d*%d=%d ", x, y, x*y) } // Generate carriage return manually fmt.Println() } } Run output: 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81