Go study notes - function

Go study notes - function


Thanks for watching, if you have any questions, please leave a message to correct me! Thanks


function

Functions are at the heart of the Go language.

First, look at this example:

func plus(a,b int) int{
    
    
    return a+b
}

Through the example we can understand that the function consists of the following four parts:

  • Function name: composed of letters, numbers, and underscores. But the first letter of the function name cannot be a number. In the same package, function names cannot be duplicated (see the following for the concept of packages).
  • Parameters: Parameters are composed of parameter variables and the types of parameter variables, and multiple parameters are ,separated.
  • Return value: The return value is composed of the return value variable and its variable type. You can also only write the type of the return value. Multiple return values ​​must be wrapped ()and ,separated by .
  • Function body: the code block that implements the specified function

The method of calling a function is that 函数名()a function with parameters needs to add parameters when calling.

func main(){
    
    
    sum := plus(1,2)
    fmt.Println("sum:",sum)
}

Variable parameter : When the parameter of the function is not fixed, it can be identified by adding it after the parameter name ....

func intSum(x...int)int{
    
    
    fmt.Printf("x:%T\n",x)
    fmt.Println(x)
    return 0
}

//intSum(10,10) 主函数调用
//x:[]int	//x是切片类型
//[10 10]

When variable parameters and fixed parameters are used at the same time, they should be placed after the fixed parameters.

multiple return values

When a Go function uses multiple return values, ()all return values ​​must be wrapped.

func calc(x,y int)(int,int){
    
    
    sum := x+y
    sub := x-y
    return sum,sub
}

//calc(4,7)
//11 -3

If only part of the return value is required, an anonymous variable can be used _.

anonymous function

An anonymous function is a function without a function name, which can be used as a return value in the function. The definition format is as follows:

func (参数)(返回值){
    
    
    函数体
}

Since the anonymous function has no function name, it cannot be called directly like a normal function, so the anonymous function needs to be stored in a variable or used as an immediate execution function.

func main(){
    
    
    //将匿名函数保存在变量中执行
    add := func(x,y int){
    
    
        fmt.Println(x+y)
    }
    add(10,20) //通过变量来调用匿名函数
    
    //匿名函数定义后立即执行
    func(x,y int){
    
    
        fmt.Println(x+y)
    }(10,20)
}

//30
//30

Closure

Closure = function + reference environment.

func intSeq() func() int{
    
    
	i := 0
	return func() int {
    
    
		i += 1
		return i
	}
}

func main(){
    
    
    //定义变量来引用函数,形成闭包,每次调用都会更新i
	nextInt := intSeq()
	fmt.Println(nextInt())
	fmt.Println(nextInt())
	fmt.Println(nextInt())
	
    //定义新的变量来引用函数,形成闭包,每次调用都会更新i
	newInts := intSeq()
	fmt.Println(newInts())
	
    //两次闭包调用对i的更新值是不同的
}

recursion

See this article for recursive features.

https://blog.csdn.net/weixin_46435420/article/details/119411637?spm=1001.2014.3001.5501

Guess you like

Origin blog.csdn.net/weixin_46435420/article/details/119458084