Detailed explanation of how to use golang functions

PHPz
Release: 2023-03-30 09:28:28
Original
541 people have browsed it

Go language is a new programming language developed by Google. It is simple, efficient, easy to learn, etc., and is suitable for concurrent programming. In the Go language, functions are a very important programming element. This article will introduce how to use golang functions.

1. Definition and call of function

In Go language, the definition of function is as follows:

func function_name(parameter_list) (result_list) {
    // 函数代码
}
Copy after login

Among them, function_name is the function name;parameter_list is a parameter list, multiple parameters are separated by commas; result_list is a function return value list, multiple return values ​​are separated by commas; {} Inside is the code block where the function executes.

The syntax for function calls is as follows:

function_name(parameter_list)
Copy after login

For example, a simple function sum is defined below, which receives two parameters of type int , return their sum:

func sum(a int, b int) int {
    return a + b
}
Copy after login

The way to call the sum function is as follows:

x := sum(3, 5)
Copy after login

The above code will return the value 8 and assign it to the variable x.

2. Multiple return value functions

In Go language, functions can return multiple values. As shown in the example below, the function more_return returns two strings:

func more_return() (string, string) {
    return "hello", "world"
}
Copy after login

The calling method is as follows:

a, b := more_return()
Copy after login

The above code will return two strings, And assign values ​​to variables a and b.

3. Function as parameter

In Go language, a function can also be passed as a parameter to another function. For example, the following example demonstrates a function as a parameter:

func calc(a int, b int, f func(int, int) int) int {
    return f(a, b)
}

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

func main() {
    result := calc(3, 5, add)
    fmt.Println(result)  // 8
}
Copy after login

In the above code, the calc function receives three parameters: a, bAnd a function f, their types are int, int and func(int, int) int respectively. The add function is a function used to add two numbers. It meets the requirements of the func(int, int) int type. In the main function, the calc function is called to calculate the value of the addition of 3 and 5, while add The function is passed as a parameter to the calc function.

4. Recursive calling of functions

In the Go language, a function can also call itself recursively. For example, the following example demonstrates a recursively called function:

func fibonacci(n int) int {
    if n < 2 {
        return n
    }
    return fibonacci(n-1) + fibonacci(n-2)
}
Copy after login

In the above code, the fibonacci function receives an integer n as a parameter and it returns Fibonacci The value of the nth number in that sequence. When n is less than 2, the return value is n itself; in other cases, the fibonacci function is called recursively.

5. Anonymous functions

In Go language, functions can also be defined as anonymous functions. Anonymous functions can pass values, call and return like ordinary functions. The following example demonstrates the use of anonymous functions for sorting:

package main

import "fmt"

func main() {
    a := []int{5, 2, 6, 3, 1, 4}

    // 使用匿名函数进行排序
    sort.Slice(a, func(i, j int) bool {
        return a[i] < a[j]
    })

    fmt.Println(a)  // [1 2 3 4 5 6]
}
Copy after login

In the above code, we have used the sort.Slice function from the standard library to sort slices. An anonymous function needs to be passed in as a parameter. The parameter list of the anonymous function is i and j, which represent the two elements of the slice; the return value type is bool, which represents Which element is smaller.

6. Closure

A closure is a whole composed of a function and the external variables it references. In Go language, functions can become closures. External variables in a closure are referenced within the closure, but have a different lifetime than that of a closure, and they can survive after the closure call ends. The following example demonstrates how to use closures:

func accumulator() func(int) int {
    sum := 0
    return func(x int) int {
        sum += x
        return sum
    }
}

func main() {
    f := accumulator()
    fmt.Println(f(1))  // 1
    fmt.Println(f(2))  // 3
    fmt.Println(f(3))  // 6
}
Copy after login

In the above code, the accumulator function returns an anonymous function, and the sum variable is referenced in the anonymous function. After the closure call ends, the sum variable still exists and continues to hold its value. In the main function, we call the accumulator function to obtain a closure function f, and calculate the accumulation by calling the f function multiple times and.

7. Summary

Function in Go language is a very important programming element. They are simple, efficient, easy to learn, etc., and support functions as parameters, multiple return values, and recursion. Calls, anonymous functions, closures and many other features. Mastering the use of these features can help us better complete Go language development work.

The above is the detailed content of Detailed explanation of how to use golang functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!