SoFunction
Updated on 2025-03-03

Detailed explanation of the control statements of GO language include the pointer syntax of GO language

Control statements for GO language

Judgment structure: if-else

Like most programming languages, if-else is basically the same. Just take an example of GO.

package main
import (
    "fmt"
)
func main(
    var str string = "A"
    if str=="A"{
        ("Match Successfully")
    }else{
        ("Match failed")
    }
)

The output is:Match successfullyIf changedstrThe value ofMatch failed, This is the simplest judgment statement, and there are some complex ones that are discussed in a later program (the complete if-else may have to be discussed in a big chapter)

switch structure

Match a variable of certain characteristics to the corresponding value, omitting the structure of if-else, and displaying it in a clearer and more readable way. A complete example

package main
import(
    "fmt"
)
func main(
    var num int = 20
    switch num{
        case 10:
            ("num = 10")
        case 20:
            ("num = 20")
        case 30:
            ("num = 30")
        default:
            ("default = 0")
    }
)

Output:

num = 20

Additional Notes:

In GO language, no need to usebreakTo end the current execution block, the program will automatically match and end after complete completion;
So the question is, what if you don’t want to end the match? In GO, usefallthroughKeywords to continue matching such as:

    var num int = 20
    switch num{
        case 10:
            ("num = 10")
        case 20:
            ("num = 20")
            fallthrough
        case 30:
            ("num = 30")
        default:
            ("default = 0")
    }

The program will output:

num = 20
num = 30

Thinking: When to use swethc and when to use if-else? You can write a little DEMO yourself to test the structure of these two judgment statements

Circular control structure for

In GO, loops onlyforThis keyword implements multiple loop structures, eliminating other languages ​​such as:while,do-while,foreach,soforMore flexible functions

1. The first form of for

Like most other languages, the basic form is:

for Initialization statement; Conditional statements; Modification statement {}

A complete example

package main
import(
    "fmt"
)
func main(){
    var num int = 5
    for i:=0;i<num;i++{
        ("num index id %d \n", i)
    }
}

The output is:

num index id 0 
num index id 1 
num index id 2 
num index id 3 
num index id 4 

Supplement: The for loop in GO language does not need to be added to the judgment part()Come wrap it up
Exercise questions: Use GO to print the following format

G
GG
GGG
GGGG
GGGGG

Code:

package main
import(
    "fmt"
)
func main(){
    for i := 0; i < 5; i++ {
        G := ""
        for j := 0; j < i; j++ {
            G += "G"
        }
        (G + "G")
    }
}

2. The second form of for

This format is to some extent: it is actually from other languagesdo-whileLoop, but GO uses all this loopforCome to achieve

for Conditional statements {}

A complete example:

package main
import(
    "fmt"
)
func main(){
    var num_2 int = 5
    for num_2 > 0 {
        ("num_is is %d \n", num_2)
        num_2--
    }
}

The output is:

num_is is 5 
num_is is 4 
num_is is 3 
num_is is 2 
num_is is 1

3. The third form of for

I usually call this form infinite loop. When writing this form, you must pay attention to using it.break,returnIf you are not careful, you will write it as a dead loop

for { } or for ;; { }or for true { }

A complete example

package main
import(
    "fmt"
)
func main(){
var num_3 int = 5
    for {
        if num_3 &lt; 0 {
            break //Show this line to comment        }
        ("num_3 is %d \n", num_3)
        num_3--
    }
}

The output is:

num_3 is 5 
num_3 is 4 
num_3 is 3 
num_3 is 2 
num_3 is 1 
num_3 is 0

4. Four Forms of For

This form is similar to other languagesforeachKeywords, multi-layer loop

for ix, val := range coll { }

A complete example

package main
import(
    "fmt"
)
func main(){
    strs := "Hello World! Example"
    for ins, char := range strs {
        ("str is index %d,value is %c \n", ins, char)
    }
}

Output:

str is index 0,value is H 
str is index 1,value is e 
str is index 2,value is l 
str is index 3,value is l 
str is index 4,value is o 
str is index 5,value is   
str is index 6,value is W 
str is index 7,value is o 
str is index 8,value is r 
str is index 9,value is l 
str is index 10,value is d 
str is index 11,value is ! 
str is index 12,value is example 
str is index 15,value is son 

Yes, you read that right, GO will automatically recognize Chinese. Commonly used English letters, the number is 1 byte, and Chinese or other characters occupy 2-3 bytes.

Common keywords for control statements

break

End the current judgment or loop and execute the following code

continue

End the judgment or cycle of the next judgment or cycle

return

Return to the current function, the following code is not executed

Introduction to tags and goto, I'm introducing them when I have time (in fact, I don't encourage everyone to use this, because if you accidentally read the scope wrong, it will lead to a dead loop and keep calling it in a certain tag)

pointer

  • In the GO language, the ability to control pointers of data structures is provided, but you cannot perform pointer arithmetics;
  • In GO language,*Keywords to declare a variable as a pointer variable; (Example: var p *int)
  • In GO language,&Before putting the keywords on the variable, return the memory address of the variable; (Example: p = & variable)
  • In GO, the format identifier is%p(Example: ("%P",p))
  • A complete example
package main
import(
    "fmt"
)
func main(
    s := "good bye"
    var p *string = &amp;s
    *p = "ciao"
    ("Pointer Address: %p\n", p)
    ("The value of pointer P is: %s\n", *p)
    ("The value of variable S is: %s\n", s)
)

The output is

Pointer address: 0x2540820(Memory value may change)
pointerPThe value is: ciao
variableSThe value is: ciao

Why is the value of variable S the same as the value of pointer P?

Answer: Because P is a pointer and a memory address, when the pointer P is reassigned, the value of the memory address corresponding to P changes, and the memory block where the value of variable S is located is exactly the memory corresponding to pointer P.

This article mainly explains the control statements of GO language, including the pointer syntax of GO language, the switch structure of GO language, and the four structures of GO language for. For more information about the control statement syntax of GO language, please see the related links below.