Home  >  Article  >  Backend Development  >  Introduction to the basic concepts and usage of Go language functions

Introduction to the basic concepts and usage of Go language functions

PHPz
PHPzOriginal
2024-03-09 17:33:041190browse

Introduction to the basic concepts and usage of Go language functions

Go language is a fast, concise, safe and efficient programming language that has an increasing influence in the field of software development in recent years. In the Go language, function is a very important concept. It is the most basic unit of the program. This article will introduce the basic concepts and common usage of Go language functions, and attach specific code examples.

1. Definition of function

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

func 函数名(参数列表) 返回值列表 {
    // 函数体
}

Among them, func is the keyword to define the function , the function name can be any legal identifier, the parameter list is enclosed in parentheses, if there are multiple parameters, separated by commas, the return value list is after the parameter list, indicating the return value of the function. If the function does not return a value, the return value list can be omitted.

2. Function call

After defining the function, you can call it through the function name. The sample code is as follows:

package main

import "fmt"

// 定义一个简单的函数,实现两数相加
func add(x, y int) int {
    return x + y
}

func main() {
    sum := add(3, 5)
    fmt.Println("3 + 5 =", sum)
}

Run the above code, the output result is:

3 + 5 = 8

3. Multiple return values ​​of functions

Go language supports functions returning multiple values. The sample code is as follows:

package main

import "fmt"

// 定义一个函数,返回两个值
func divmod(a, b int) (int, int) {
    return a / b, a % b
}

func main() {
    quotient, remainder := divmod(10, 3)
    fmt.Printf("10 ÷ 3 = %d, 余 %d
", quotient, remainder)
}

Run the above code, the output result is:

10 ÷ 3 = 3, 余 1

4. Anonymous function

Anonymous function is a function without a function name. It can be directly assigned to a variable or passed as a parameter. The sample code is as follows:

package main

import "fmt"

func main() {
    add := func(x, y int) int {
        return x + y
    }

    sum := add(4, 6)
    fmt.Println("4 + 6 =", sum)
}

Run the above code and output The result is:

4 + 6 = 10

5. Closure

A closure is an entity composed of a function and its related reference environment. Closures can capture variables in external functions and operate on them. The sample code is as follows:

package main

import "fmt"

func outer() func() {
    count := 0
    return func() {
        count++
        fmt.Println("count:", count)
    }
}

func main() {
    f := outer()
    f() // count: 1
    f() // count: 2
}

Run the above code, the output result is:

count: 1
count: 2

Through the above introduction, we understand Go Basic concepts and common usage of language functions, including function definition, function calling, multiple return values, anonymous functions and closures. Functions are an important part of the Go language, and mastering the use of functions will help improve the readability and reusability of the code. I hope this article will be helpful to you in learning Go language functions.

The above is the detailed content of Introduction to the basic concepts and usage of Go language functions. 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