当前位置:网站首页>Go language functions

Go language functions

2022-06-11 21:05:00 Just number six Z

function

Define format

Functions form the logical structure of code execution . stay Go In language , The basic composition of a function is : keyword func、 Function name 、 parameter list 、 Return value 、 Function bodies and return statements .

Go The language function definition format is as follows :

func FuncName(/* parameter list */) (o1 type1, o2 type2/* Return type */) {
    
    // The body of the function 

    return v1, v2 // Return multiple values 
}

Function definition description :

  • func: Function by keyword func Start statement
  • FuncName: The name of the function , According to the contract , Function names start with lowercase letters private, Capitalized means public
  • parameter list : Functions can have 0 Two or more parameters , The parameter format is : Variable name type , If multiple parameters are separated by commas , Default parameters... Are not supported
  • Return type :
    ①  The above return value declares two variable names o1 and o2( Name return parameters ), This is not necessary , You can only type without variable name
    ②  If there is only one return value and the return value variable is not declared , Then you can omit , Parentheses including return values
    ③  If there is no return value , Then omit the last return information directly
    ④  If there is a return value , So you have to add... Inside the function return sentence

Custom function

No parameter no return value

func Test() {
     // No parameter no return value function definition 
    fmt.Println("this is a test func")
}

func main() {
    
    Test() // No parameter no return value function call 
}

Return value with parameter or not

Common parameter list

func Test01(v1 int, v2 int) {
     // The way 1
    fmt.Printf("v1 = %d, v2 = %d\n", v1, v2)
}

func Test02(v1, v2 int) {
     // The way 2, v1, v2 All are int type 
    fmt.Printf("v1 = %d, v2 = %d\n", v1, v2)
}

func main() {
    
    Test01(10, 20) // Function call 
    Test02(11, 22) // Function call 
}

Indefinite parameter list

  1. Indefinite parameter type
    Indefinite parameter means that the number of parameters passed into a function is indefinite . To do that , First, you need to define the function to accept indefinite parameter types :
// Form like ...type The type of the format can only exist as the parameter type of the function , And it has to be the last parameter 
func Test(args ...int) {
    
    for _, n := range args {
     // Traversal parameter list 
        fmt.Println(n)
    }
}

func main() {
    
    // Function call , Biography 0 To multiple parameters 
    Test()
    Test(1)
    Test(1, 2, 3, 4)
}

  1. The transfer of indefinite parameters
func MyFunc01(args ...int) {
    
    fmt.Println("MyFunc01")
    for _, n := range args {
     // Traversal parameter list 
        fmt.Println(n)
    }
}

func MyFunc02(args ...int) {
    
    fmt.Println("MyFunc02")
    for _, n := range args {
     // Traversal parameter list 
        fmt.Println(n)
    }
}

func Test(args ...int) {
    
    MyFunc01(args...)     // Deliver as is , Test() The parameters of are passed to MyFunc01
    MyFunc02(args[1:]...) //Test() In the parameter list , The first 1 Parameters and later parameters are passed to MyFunc02
}

func main() {
    
    Test(1, 2, 3) // Function call 
}

No parameter return value

Function with return value , There must be a clear termination statement , Otherwise, it will cause compilation errors .

A return value

func Test01() int {
     // The way 1
    return 250
}

// The official advice : It's best to name the return value , Because the return value is unnamed ,
// Although it makes the code more concise , But it will cause poor readability of the generated documents 
func Test02() (value int) {
     // The way 2,  Name the return value 
    value = 250
    return value
}

func Test03() (value int) {
     // The way 3,  Name the return value 
    value = 250
    return
}

func main() {
    
    v1 := Test01() // Function call 
    v2 := Test02() // Function call 
    v3 := Test03() // Function call 
    fmt.Printf("v1 = %d, v2 = %d, v3 = %d\n", v1, v2, v3)
}

Multiple return values

func Test01() (int, string) {
     // The way 1
    return 250, "sb"
}

func Test02() (a int, str string) {
     // The way 2,  Name the return value 
    a = 250
    str = "sb"
    return
}

func main() {
    
    v1, v2 := Test01() // Function call 
    _, v3 := Test02()  // Function call ,  The first return value is discarded 
    v4, _ := Test02()  // Function call ,  The second return value is discarded 
    fmt.Printf("v1 = %d, v2 = %s, v3 = %s, v4 = %d\n", v1, v2, v3, v4)
}

With parameter and return value

// seek 2 Minimum and maximum number 
func MinAndMax(num1 int, num2 int) (min int, max int) {
    
    if num1 > num2 {
     // If num1  Greater than  num2
        min = num2
        max = num1
    } else {
    
        max = num2
        min = num1
    }

    return
}

func main() {
    
    min, max := MinAndMax(33, 22)
    fmt.Printf("min = %d, max = %d\n", min, max) //min = 22, max = 33
}
原网站

版权声明
本文为[Just number six Z]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112054044079.html