Knowledge sharing about Go functions

Knowledge sharing about Go functions

One, init function

1. Features: It will mainbe run by Go's running framework before the function is executed

package main 
import (
	"fmt"
)
//init在main前面运行,可以数据初始化
func init(){
    
    
   fmt.Println("init打印")  //类似与构造函数
}
func main(){
    
    
   fmt.Println("main打印")
}

The output results of the above code are:
Insert picture description here
2. Function: complete some initial assignment work
3. Note: the initfunction is executed after the execution of the global variable.
When main.gocalled util.go, it has global variables and the execution order of the init function.
Insert picture description here
2. Anonymous function

1. Definition: An anonymous function is a function without a name

2. How to use

Use method one of anonymous function (call the function only once)

func main(){
    
    
    res:=func (n1 int,n2 int) int {
    
    
          return n1+n2
    }(10,20)
    fmt.Println("res=",res)
}

The second way of using anonymous functions (assign the function to a variable, and use the variable to call the anonymous function)

func main(){
    
    
    a:=func (n1 int,n2 int) int {
    
    
          return n1+n2
    }
    res:=a(10,20)
    fmt.Println("res=",res)
}

3. Global anonymous function
Define the second way of using anonymous functions in global variables, that is, a is a global anonymous function, and all sub-functions in this package can call the anonymous function.

3. Function error handling
Background:
1. By default, if panican error occurs , the program will exit (crash). We hope that the error can be caught and the program can continue to run.
2, due to the pursuit of beauty Go language, so do not use the traditional try...catch..., so the introduction of defer, panic, , recoverGo when thrown in a panic, and then recover by capturing the exception defer, in a normal process.

func testError(){
    
       
   defer func(){
    
    
   	err:=recover() //内置函数捕获异常
   	if err!=nil{
    
    
   		fmt.Println("err=",err)
   		fmt.Println("发送信息给管理员")
   	}
   }()   //这里将错误信息发送信息
   num:=1
   num2:=0
   res:=num/num2    //0不能做分母,这个是必错的代码
   fmt.Println(res)
}

3. Custom error

erroes.New ("error description"); returns a value of erroe type, indicating an error, then panic receives, outputs the error message, and exits the program, the code after the error code does not run

//自定义错误
func readConfig(name string)error{
    
    
	    b:=strings.HasSuffix(name,"fig")
        if b==true{
    
    
			return nil
		}else{
    
    
		//返回自定义错误
			return errors.New("文件名不正确")
		}
}
func errRead(){
    
    
	result:=readConfig("mmp.fi")
	if result !=nil{
    
    
		panic(result)
	}else{
    
    
        fmt.Println("没错误,继续执行")
	}
}
func main(){
    
    
    // testError()
	// fmt.Println("发送邮件")  //莫尔尼情况下不能到这一行
	errRead()
    fmt.Println("下面代码不执行了")

}

If the reading is a little helpful to you, please like and support, thank you

Guess you like

Origin blog.csdn.net/yyq1102394156/article/details/113872830