Go golang Foundation--func Function

Posted by Japet on Fri, 21 Feb 2020 17:09:02 +0100

Function function

  • The Go function does not support nesting, overloading, and default parameters
  • The following features are supported:

    No prototype required, variable length parameters, multiple return values, named return value parameters, anonymous functions, closures

  • The definition function uses the keyword func and the left curly brace cannot start a new line

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        a, b := 1, 2
        D(a, b) //Pass variable length variable, slice index cannot be modified, slice value cannot be changed radically
        s1 := []int{1, 2, 3}
        D1(s1) //Passing slice as a parameter modifies the index value to modify the original slice
        x := 1
        D2(&x) //Memory address to pass x
    }
     //Parameter Return Value
    func A(a int, b string) int {
        return a
    }
    
    //Multiple Return Values
    func B() (a, b, c int) { //No parameters,
        a, b, c = 1, 2, 3 //Since a,b,c already occupy the inner layer in the return value, assign the value directly after
        return a, b, c    //You can omit a,b,c
    }
    
    //Variable length variable, that is, the int parameter can be multiple, and a slice type is passed in
    func C(b string, a ...int) { 
        fmt.Println(a) //Output [1,2,3,4,5]
    }
    
    /Variable length parameter passed in, variable passed in
    func D(s ...int) { 
        fmt.Println(s)
        s[0] = 3
        s[1] = 4
        fmt.Println(s)
    }
    
    //Pass slice as parameter, copy slice's memory address
    func D1(s []int) { 
        s[0] = 4
        s[1] = 5
        fmt.Println(s)
    }
    
    //Pointer type transfer for modifying variable values
    func D2(a *int) { 
        *a = 2 //The value of memory a changes to 2
        fmt.Println(*a)
    
    }
    
    /*output
    D---->      [1 2]  //slice
                [3 4]  //slice
                 1 2    //int
    D1---->     [4 5 3]
    
    D2---->     2       //A pointer parameter modifies the value of a variable
    */
  • Functions can also be used as a type

    package main
    import "fmt"
    
    func main() {
        a := A //Functions are used as types
        a()
    
    }
    
    func A() {
        fmt.Println("Func A")
    }
    
    //Pointer type transfer for modifying variable values
    
    /*output
    a()---->    Func A
    */

    The function name can be understood as the number of the memory address and can be used for assignment operations.

  • Anonymous function, different from python language related keywords lamada, go anonymous function explicit more straightforward, no function name, just func keyword can be used
    Simply build an anonymous function as follows

    // Anonymous function
    package main
    import "fmt"
    
    func main() {
        a := func() { //Assign directly to a variable without a function name
            fmt.Println("Func A")
        }
        a() //Calling anonymous functions
    }
  • Closure of a function: Also known as a nested function, the type returned is a function, then assigns the function to a variable, and re-passes in the parameter to execute

    package main
    import "fmt"
    func main() {
        f := closure(10)
        fmt.Println(f(3))
        fmt.Println(f(4))
    }
    
    func closure(x int) func(int) int { // Function as return type
        return func(y int) int {
            return x + y
        }
    }
    /*output
    f(3)--->    13
    f(4)--->    14
    */

Topics: Go Python