Home  >  Article  >  What is fn in go language?

What is fn in go language?

小老鼠
小老鼠Original
2023-04-17 11:46:512248browse

In the Go language, fn specifically refers to the keyword func. The basic components of a function are: keyword func, function name, parameter list, return value, function body and return statement. Each program contains many Function, a function is a basic block of code. A function is an organized, reusable code segment used to implement a single or related function.

What is fn in go language?

Operating system for this tutorial: Windows 10 system, GO version 1.20, Dell G3 computer

Go language function declaration (function definition )

Functions are organized, reusable code segments used to implement single or related functions, which can improve application modularity and code reuse. Go language supports ordinary functions, anonymous functions and closures. The functions are optimized and improved from the design to make the functions more convenient to use.

Functions constitute the logical structure of code execution. In Go language, the basic components of functions are: keyword func, function name, parameter list, return value, function body and return statement. Each program contains There are many functions, and functions are basic blocks of code.

Because the Go language is a compiled language, the order in which the functions are written is irrelevant. In view of the readability requirements, it is best to write the main() function at the front of the file, and other functions follow the Write in a certain logical order (such as the order in which functions are called).

The main purpose of writing multiple functions is to decompose a complex problem that requires many lines of code into a series of simple tasks to solve. Moreover, the same task (function) can be called multiple times, which helps Code reuse (in fact, good programs pay great attention to the DRY principle, that is, Don't Repeat Yourself (Don't Repeat Yourself), which means that the code that performs a specific task can only appear once in the program).

The function will exit when it reaches the last line of the code block or before the return statement. The return statement can have zero or more parameters. These parameters will be used as return values ​​for the caller. Simple The return statement can also be used to end the infinite loop of for, or end a coroutine (goroutine).

Go language functions do not need to be declared

Go language functions are first-class citizens. Like variables, they can be assigned to a certain variable (the reason for the emergence of anonymous functions)

Since functions are first-class citizens, all the formats used to define variables in the past can be used to define variables of function type

    var 变量名称 函数类型
    var 变量名称 函数类型 = 匿名函数
    var 变量名称  = 匿名函数
    变量名称  := 匿名函数
    var(
        var 变量名称  = 匿名函数
    )
fn = func (a,b int){
    return a + b
}
fn()

The way to define function variables in Go language is different from that in C language

Format: func function name (parameter list) return value list{}

The function return value in Go language can be more than one

func test(a,b int) (a,b int){
    return a,b
}

Go language function return value can be only Write type, you can also write variable type

Write-only type: func test(a,b int) (int){} Note: Write-only type, the parentheses on both sides of the return value type int can be omitted

Variable plus type: func test(a,b int) (a,b int){}
Note: When writing the variable type form, return does not need to add a return value, and it will automatically find a in the code block If the type of Go language formal parameter list or return value list is the same as b

func test(a,b int) (a,b int){
      a = 100
      b = 99
      return //自动返回100,99}

, then we can continuously define
as follows: two functions represent the same method

func test(a,b int) (a,b int){return a,b}
func test2(a int,b int) (int,int){return a,b}

Since the Go language function There can be multiple return values, so when the external function receives multiple return values, it needs to use multiple variables to receive them. However, if variables are not used in Go language, an error will be reported, so if we have a return value that does not need to be used, we will use _ to receive, _ has a specific meaning in the Go language, so it cannot be used as an identifier for a variable

Anonymous function

Anonymous function is a function without a name. It needs to be used immediately after being defined otherwise an error will be reported

Anonymous functions are usually only used once. Since they are first-class citizens, they can be used as formal parameters of the function or as the return value of the function

As a function Formal parameters

fn  := func (a,b int) int {
  return a + b
}

//此时调用test函数用来计算a+b可以这么写
res := test(a,b,fn)

func test(a,b int,method func(int,int) int) func() {
  return method(a,b)
}

Further evolution

Since fn and are anonymous function assignments, we can directly pass the anonymous function into test

//此时调用test函数用来计算a+b可以这么写
res := test(a,b,func (a,b int) int {
  return a + b
})

func test(a,b int,method func(int,int) int) func() {
  return method(a,b)
}

as the return value of the function

fn := test()
fn()//打印匿名函数

func test() func() {
    return func() {
        fmt.Println("匿名函数")
    }
}

Closure (special anonymous function)

If external variables are used in the returned anonymous function, it is called a closure

Characteristics of closure: "People are in "As long as external variables are used in the returned anonymous function, if the anonymous function is still used after the function call, the function stack will not be released. Once it is no longer used, the function stack will be released.

fn := test()//test函数也被称为迭代器
fn()  //2
fn()  //3
fn()  //4

func test() func(){
     x := 1
     return func (){
            x++
            println(x)
        }
}

Memory performance of anonymous functions

What is fn in go language?

Illustration

  1. The code area stores the codes of three functions, corresponding to one address

  2. Define the variable fn and pass the address of the anonymous function to fn

  3. ##Call the function test to open up storage space

  4. Open storage space for num, value, method, res, where method saves the address of the function passed to it

  5. Execute the function, method saves the address of the anonymous function, When the method is executed, the storage space is opened and the values ​​​​of a and b are stored. After the function ends, the value is returned to res and the storage space of the method is released.

  6. Res is printed, the test function is also released, and fn is also released after use

The above is the detailed content of What is fn in go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn