当前位置:网站首页>Go language, condition, loop, function

Go language, condition, loop, function

2022-04-23 15:41:00 The end of the world and you

1. Conditions

if/else

package main

import "fmt"

func main() {
    
	var a int = 521
	if a == 521 {
    
		fmt.Println(" I love you! ")
	} else {
    
		fmt.Println(" I do not love you ")
	}
}
--------------------------
 Output : I love you! 

switch-case

switch var1 {
    
    case val1:
        ...
    case val2:
        ...
    default:
        ...
}

select sentence

select Randomly execute a runnable case. without case Can run , It will block , Until there is case Can run . A default clause should always run .

select {
    
    case communication clause  :
       statement(s);      
    case communication clause  :
       statement(s);
    /*  You can define any number of  case */
    default : /*  Optional  */
       statement(s);
}

The following describes select Sentence syntax :

  • Every case It has to be a communication
  • all channel Expressions are evaluated
  • All expressions sent are evaluated
  • If any particular communication can be made , It will carry out , The rest is ignored .
  • If there are more than one case Can run ,Select One of them will be selected randomly and fairly . Others will not be executed .
    otherwise :
    If there is default Clause , Then execute the statement .
    without default Clause ,select Will block , Until some communication can run ;Go It won't be right again channel Or value to evaluate .

2. Loop statement

for Cycle is a cycle control structure , You can execute a specified number of cycles .

example 1: Calculation 1 To 10 The sum of the numbers

package main

import "fmt"

func main() {
    
	sum := 0
	for i := 0; i <= 10; i++ {
    
		sum += i
	}
	fmt.Println(sum)
}
------------------------------------
 Output :55

example 2: Infinite loop

package main

import "fmt"

func main() {
    
	sum := 0
	for {
    
		sum += 1
	}
	fmt.Println(sum)
}

To stop the infinite loop , You can press... In the command window ctrl-c .

example 3:For-each range loop

package main

import (
	"fmt"
)

func main() {
    
	strings := []string{
    "imustctf", "wode"}
	for i, s := range strings {
    
		fmt.Println(i, s)
	}
}
---------------------------------------
 Output :
0 imustctf
1 wode

example 4: Allied while loop , stay sum Less than 10 When the sum The value after self adding :

package main

import "fmt"

func main() {
    
	sum := 1
	for sum <= 10 {
    
		sum += sum
	}
	fmt.Println(sum)
}
-----------------------------
 Output :16

3. function

example 1: Function passes in two integer arguments num1 and num2, And return the maximum value of these two parameters

package main

import "fmt"

func max(num1, num2 int) int {
    
	//  Declare local variables 
	var result int
	if num1 > num2 {
    
		result = num1
	} else {
    
		result = num2
	}
	return result
}

func main() {
    
	var a int = 100
	var b int = 200
	res := max(a, b)
	fmt.Println(res)
}
-----------------------------
 Output :200

example 2: Function returns multiple values

package main

import "fmt"

func swap(x, y string) (string, string) {
    
	return y, x
}

func main() {
    
	a, b := swap("a", "b")
	fmt.Println(a, b)
}
--------------------------------
 Output :b a

example 3: Pass pointer parameter by reference

package main

import "fmt"

func swap(x *int, y *int) {
    
	var temp int
	temp = *x /*  preservation  x  The value on the address  */
	*x = *y   /*  take  y  Value is assigned to  x */
	*y = temp /*  take  temp  Value is assigned to  y */
}

func main() {
    
	a, b := 100, 200
	fmt.Println(a, b)
	swap(&a, &b)
	fmt.Println(a, b)
}
-----------------------------------
 Output :
100 200
200 100

Copyright notice : This tutorial is based on the rookie tutorial

版权声明
本文为[The end of the world and you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231533524059.html