go process control code detail
- 2020-06-23 00:38:12
- OfStack
if
The & # 8226; In Go, parentheses are not required in the if conditional statement, which allows the declaration of 1 variable. Any variable declared here can be used in all conditional branches.
if x := 11; x > 10 {
fmt.Println("x is greater than 10")
} else {
fmt.Println("x is less than 10")
}
goto
The & # 8226; Use goto to jump to the tag that must be defined within the current function. The tag name is case-sensitive.
func myFunc() {
i := 0
Here: // The lines of the first 1 ", ending with a colon as the label
println(i)
i++
goto Here // Jump to Here Go to the
}
for
The & # 8226; for is the only cyclic structure in go and can be used in the following ways
// Classic initialization / conditions / Subsequent form for cycle
for expression1; expression2; expression3 {}
// With a single cycle condition, which is ignored expression1 and expression3 :
sum := 1
for ; sum < 1000; {
sum += sum
}
// Among them ; You can also omit it, so it becomes the following code, and here it is while The function.
for sum < 1000 {}
// No conditions for Loop will 1 Do this until it is used in the circulation break or return Out of the loop
for {
fmt.Println("loop")
break
}
break and continue
The & # 8226; When nesting is too deep, break can be used with the tag to jump to the location specified by the tag, and break and continue can also be used with the tag to jump to the outer loop of multiple loops
for with range can be used to read slice, map and array data
The & # 8226; range provides indexes and values for each item in both arrays and slice. We use the null value definer _ to ignore the index when we don't need it, because the compiler will report an error for "declared but not called" variables. Sometimes we need this index.
The & # 8226; range iterates over key-value pairs in map
switch
// Classic initialization / conditions / Subsequent form for cycle
for expression1; expression2; expression3 {}
// With a single cycle condition, which is ignored expression1 and expression3 :
sum := 1
for ; sum < 1000; {
sum += sum
}
// Among them ; You can also omit it, so it becomes the following code, and here it is while The function.
for sum < 1000 {}
// No conditions for Loop will 1 Do this until it is used in the circulation break or return Out of the loop
for {
fmt.Println("loop")
break
}
conclusion
Above is the site to introduce go process control code detailed, I hope to help you, if you have any questions welcome to leave a message, this site will reply you in time!