Golang function function analysis Fibonacci recursive implementation

Regarding Fibonacci, the recursive implementation is directly uploaded to the code, and later some explanations will be given on the characteristics of golang's func

package main

import (
	"errors"
	"fmt"
)

func main() {
	if err := logAndFun(fblq_sum, -10, "debug");err != nil{
		fmt.Println("运行程序异常奔溃,请检查日志",err.Error())
	}else{
		fmt.Println("切面函数未发现异常")
	}

}
// 函数: 为演示函数是可以作为参数传递的,并能执行
func logAndFun(f func(int) int, n int, leve string) error {
	var err error
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("程序异常", r)
			// 事实证明 defer 干预不了 返回结果
			err = errors.New("异常啦啦")
		}
	}()
	fmt.Println(leve, "执行形参函数执行 start")
	fmt.Println("函数类参数执行结果=", f(n))
	fmt.Println(leve, "执行形参函数执行 end")
	return err
}
// 函数: 求斐波拉契值
func fblq_sum(n int) int {
	if n >= 2 {
		return fblq_sum(n-1) + fblq_sum(n-2)
	} else if n>=0 {
		return 1
	}else{
		panic("能好好玩不,负数不可")
	}
}

operation result:

debug 执行形参函数执行 start
程序异常 能好好玩不,负数不可
切面函数未发现异常

 

Golang function understanding notes

1. The exception of the function is managed by the error interface. You can manage the exception through the errors package. It is agreed to use the Error() method of the interface to output the exception string

2. Panic is an unpredictable runtime exception. If it is not caught, it will cause the entire program to crash, not just the function itself

3. The recover() function is used to capture panic exceptions to ensure that exceptions occur during function execution and will not cause the program to crash, as shown in the above sample code

4. The defer statement is equivalent to the finally statement in the java code, that is, the program proves that exceptions or panics will not cause the defer not to be executed, and it is used to close IO resources, various connection resources, preprocessing before continuous crashes, etc.

5. Although the anonymous function in the defer statement is executed at the end, it cannot interfere with the variable assignment in return, as shown in the above code execution results

6. Two particularly important features

        a. The function in golang is a data type, that is, a function variable var ff func(int,int) string can be defined

        b. Functions can be assigned to variables, and can be passed as function parameters, as long as they conform to the specified function parameter type

// 函数作为参数传递 f
func logAndFun(f func(int) int, n int, leve string) error 
// 函数fblq_sum作为参数传递,前提满足logAndFun函数中对函数参数的定义 func(int) int
logAndFun(fblq_sum, -10, "debug")

 7. For the above code, it can be understood that the function logAndFun is to execute some established business or aspect logic, and its formal parameter function is to leave a certain space for you to customize various execution logic, which is defined in the database Things, and the specific execution logic of things are realized by the programmers themselves according to the business, which is particularly obvious

8. The capitalization of functions, structures, variables, methods, variables, interfaces, and interface methods is particular. Uppercase means that they can be used across packages, otherwise they can only be used within this package

9. Take advantage of the above traits. What should I do if I want the variables in the structure (struct) not to be assigned arbitrarily?

        a. The structure is capitalized, and the variables in the structure are smaller. Such a structure can be used outside the package, but variables cannot be assigned arbitrarily

        b. The assignment of structure variables, we realize by defining the method, of course, must be capitalized, and then it can be written in the function for the restriction of assignment

type Car2 struct {
	title string
	name  string
}

func (c *Car2) SetTitle(title string) {
	if len(title) < 5 {
		return
	}
	c.title = title
}
func (c *Car2) SetName(name string) {
	if len(name) > 50 {
		c.name = name[:50]
	} else {
		c.name = name
	}
}

        

Guess you like

Origin blog.csdn.net/m0_37298500/article/details/125953890