头图

Golang-based function usage (1)

帽儿山的枪手
中文

Hello everyone, today I will sort out the usage of Go language functions and share them with you. Please advise, thank you.

This "Go Language Function Usage" is divided into three chapters, and this article is the first chapter.

  • Golang-based function usage (1)
  • Golang-based function usage (2)
  • Golang-based function usage (3)

Contents of this chapter

  • Function introduction
  • Function characteristics
  • function declaration
  • Parameters
  • Indefinite parameter pass by value
  • Indefinite parameter of any parameter type
  • return value

Function introduction

A collection of program instructions (statements) to accomplish a function is called a function.

Go language functions can be divided into: custom functions and system functions. The biggest difference between Go language functions and other language functions is that functions in Go language can support returning any number of values, while functions in other languages generally only support returning one value.

For example, the return value of a C language function can only be of one type.

Functions in the Go language also support ordinary functions, anonymous functions and closures .

Function characteristics

  • No need to declare prototype
  • Support indefinite varargs
  • Support multiple return values
  • Support for named return parameters
  • Supports anonymous functions and closures (higher-order functions that change direction)
  • A function is also a type, a function can be assigned to a variable
  • Nested is not supported. A package cannot have two functions with the same name
  • Overload is not supported
  • Default parameter is not supported

function declaration

The function declaration includes the function name, the formal parameter list, the return value list (optional), and the function body.

 func name(parameter-list) (result-list) {   // 声明函数名
  // 函数体
}

If the function returns an unnamed variable or has no return value, the parentheses around the return value list can be omitted. If a function declaration does not include a list of return values, then after the function body is executed, no value is returned.

Parameters

pass by value

Refers to copying the actual parameters and passing them to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected.

 func num(x, y int) int {
       // 处理逻辑
}

pass by reference

It means that the address of the actual parameter is passed to the function when the function is called, then the modification of the parameter in the function will affect the actual parameter.

 func swap(x, y *int) {
    var temp int

    temp = *x /* 保存 x 的值 */
    *x = *y   /* 将 y 值赋给 x */
    *y = temp /* 将 temp 值赋给 y*/
}

Whether it is pass-by-value or pass-by-reference, what is passed to the function is a copy of the variable, but pass-by-value is a copy of the value.

Pass-by-reference is a copy of the address, and in general, the copy of the address is more efficient. The value copy depends on the size of the copied object, the larger the object, the lower the performance.

map, slice, chan, pointer, interface are passed by reference by default.

Indefinite parameter pass by value

The parameters of the function are not fixed, and the following types are fixed. (variable parameter)

Golang variable parameters are essentially slices.

When assigning parameters, you don't need to assign them one by one. You can directly pass an array or slice. Note that is added after the parameter.

 func myfunc(args ...int) {    //0个或多个参数
}

func add(a int, args ...int) int {    //1个或多个参数
}

func add(a int, b int, args ...int) int {    //2个或多个参数
}
Where args is a slice type, we can access all parameters in turn by arg[index] len(arg) , and judge the number of passed parameters by ---920629c9d1a47c4b7bc253d907c0c4ad---.

When using a slice object as a variable parameter, it must be expanded.

 package main

import (
    "fmt"
)

func sum(s string, n ...int) string {
    var x int
    for _, i := range n {
        x += i
    }
    return fmt.Sprintf(s, x) // 字符串拼接
}

func main() {
    s := []int{1, 2, 3}
    res := sum("sum: %d", s...)    // slice... 展开slice
    println(res)
}

output result

 sum: 6

indefinite parameter of any type

The parameters of the function and the type of each parameter are not fixed.

Passing any type of data with interface{} is conventional in Go, and the type of interface{} is safe.

 func myfunc(args ...interface{}){ // 示例用法
  ...
}

return value

_ Identifier, used to ignore a certain return value of the function.

Go's return values can be named, and the name of the return value should have some meaning.

A return statement with no arguments returns the current value of each return variable. This usage is called a "naked" return.
 func myfunc(arg int) (arg int) { // 示例用法
   return arg
}

Technical articles are continuously updated, please pay more attention~~

Search WeChat public account【The Gunslinger of Maoer Mountain】, follow me

阅读 418

Golang从小白到大神
分享Golang语言技术交流
43 声望
11 粉丝
0 条评论
43 声望
11 粉丝
文章目录
宣传栏