This is a creation in Article, where the information may have evolved or changed.
Go programming Language: Support concurrent, garbage collection of compiled system-level programming language! This paper is mainly based on the "Go Programming Basics" Open source video to learn and record notes.
First, function functions
1. Basic Concepts
A function is a basic block of code that performs a task.
Go 语言最少有个 main() 函数
。
函数声明告诉了编译器函数的名称,返回类型,和参数
。
The Go language standard library provides a variety of built-in functions that are available for use. For example, the Len () function can accept different type parameters and return the length of that type. If we pass in a string, the length of the string is returned, and if an array is passed in, the number of functions contained in the array is returned.
2. Function definition
The function definition format is as follows:
func function_name( [parameter list] ) [return_types] { 函数体}
Function definition Resolution:
func
: function starts declaration by Func
function_name
: function names, function names, and parameter lists together form a functional signature.
parameter list
: Parameter list, 参数就像一个占位符
when the function is called, you can pass the value to the parameter, which is called the actual parameter. The parameter list specifies the 参数类型、顺序、及参数个数
. Arguments are 可选
, that is, the function can also contain no arguments.
return_types
: return type, function returns a column value. Return_types is the data type of the column value. Some features do not require a return value, in which case the return_types is not required.
函数体
: A collection of code for function definitions.
Example:
/* 函数返回两个数的最大值 */func max(num1, num2 int) int { /* 声明局部变量 */ var result int if (num1 > num2) { result = num1 } else { result = num2 } return result }
3. Function characteristics
- Go function
不支持
nesting, overloading, and default parameters
- The Go function
支持
does not need to declare prototypes, variable length arguments, multiple return values, named return value parameters, anonymous functions, closures
- The definition function uses
func
the keyword, and the opening brace cannot be another line
函数也可以作为一种类型使用
ackage mainimport "fmt" func main() { A(1, 2, 3, 4 ,5)}// ... 不定长变参func A(a ...int) { fmt.Println(a) }
Output:
➜ myfirstgo go run func.go[1 2 3 4 5]
Variable parameter characteristics of indefinite length:
1, you can not add other parameters behind the variable length parameter func b(a ...int, b string)
, this is 错误
the
2, variable length parameters can be placed behind other parametersfunc b(b string, a ...int)
4. Anonymous function
func main() { // 将一个函数赋值一个变量,该变量是函数类型 a := func(){ fmt.Println("匿名函数")} // 调用 a()}
5. Closed Package
/*** 闭包函数** 该闭包函数接收一个int型参数,其返回值是函数类型**/func closure(x int) func(int) int { fmt.Println("%p\n", &x) return func (y int) int { fmt.Println("%p\n", &x) fmt.Println(x) fmt.Println(y) return x + y }}func main() { f := closure(10); fmt.Println(f(1)) fmt.Println(f(2))}
Printing results:
➜ myfirstgo go run func.go%p 0xc42000e228%p 0xc42000e22810111%p 0xc42000e22810212➜ myfirstgo
6, defer
- Defer are executed in a manner similar to those in other languages
析构函数
, and are executed one after the other at the end of the function body execution, in order of invocation 相反顺序
- Even if a function occurs,
严重错误
it is executed
- Calls that support anonymous functions
- Commonly used in
资源清理、文件关闭、解锁以及记录时间
such operations
- The function evaluates the result after return by cooperating with an anonymous function
修改
- If a variable in the function body is an argument to an anonymous function when defer, the copy is obtained when the defer is defined, or the address of a variable is referenced.
- Go has no exception mechanism, but there are
panic/recover
patterns to handle errors
Panic
Can be thrown anywhere, but recover
only in functions called by defer
A simple test:
func main() { fmt.Println("a") defer fmt.Println("b") defer fmt.Println("c")}
What results will be printed on the above execution? Will print: A b c?
Let's actually do this:
myfirstgo go run func.goacb
The result of the actual printing is: a C b
Defer are executed in a manner similar to those in other languages 析构函数
, and are executed one after the other at the end of the function body execution, in order of invocation 相反顺序
Using closures
func main() { for i := 0; i < 3; i++ { // defer 普通调用 // defer fmt.Println(i) // 打印 2 1 0 // 使用闭包,引用局部变量 defer func () { fmt.Println(i) }() } }
Printing results:
➜ myfirstgo go run func.go333
Panic Use example :
func main() { A() B() C()}func A() { fmt.Println("FUNC A")}func B() { // 匿名函数,如果没有参数,则末尾需要使用括号 defer func() { if err := recover(); err != nil { fmt.Println("Recover is B") } }() panic("B panic")}func C() { fmt.Println("FUNC C")}
Printing results:
➜ myfirstgo go run func.goFUNC ARecover is BFUNC C➜ myfirstgo