if
•The if conditional judgment statement in Go does not require brackets. A variable is allowed to be declared in the conditional judgment statement. Any variable declared here can be used in all conditional branches.
if x := 11; x > 10 { ("x is greater than 10") } else { ("x is less than 10") }
goto
• Use goto to jump to the tag that must be defined in the current function, the tag name is case sensitive.
func myFunc() { i := 0 Here: //The first word in this line ends with a colon as the label println(i) i++ goto Here //Skip to Here}
for
•For is the only loop structure in go, and there are the following forms of use in go
//Classic initialization/condition/sequential form of for loopfor expression1; expression2; expression3 {} //With a single loop condition, i.e., expression1 and expression3 are ignored:sum := 1 for ; sum < 1000; { sum += sum } //There can also be omitted, and it will become the following code, which is the function of while.for sum < 1000 {} //The for loop without conditions will be executed until break or return is used in the loop body to break out of the loopfor { ("loop") break }
break and continue
•When the nesting is too deep, break can be used with the tag, that is, jump to the position specified by the tag. Break and continue can also be followed by the tag to jump to the outer loop in multiple loops.
For and range can be used to read data from slice, map and array
•range provides the index and value of each item in both array and slice. When we do not need the index, we use the null value definer_ to ignore it, because the compiler will report an error for variables that are "declared but not called". Sometimes we need this index.
•range iterates over key-value pairs in map
switch
//Classic initialization/condition/sequential form of for loopfor expression1; expression2; expression3 {} //With a single loop condition, i.e., expression1 and expression3 are ignored:sum := 1 for ; sum < 1000; { sum += sum } //There can also be omitted, and it will become the following code, which is the function of while.for sum < 1000 {} //The for loop without conditions will be executed until break or return is used in the loop body to break out of the loopfor { ("loop") break }
Summarize
The above is a detailed explanation of the go process control code introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!