SoFunction
Updated on 2025-03-05

Detailed explanation of the basic function usage (parameter value transfer) examples of Golang

Function introduction

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

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

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

Go language functions also supportNormal functions, anonymous functions and closuresThree forms.

Function Features

  • No need to declare prototypes
  • Supports uncertain variable parameters
  • Supports multiple return values
  • Support naming and return parameters
  • Support anonymous functions and closures (higher-order functions that change direction)
  • Functions are also a type, and a function can be assigned to a variable
  • Nested (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

Function declarations include function names, formal parameter lists, return value lists (omitted), and function body.

func name(parameter-list) (result-list) {   // Declare the function name  // Function body}

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

Transfer method

Value pass

It refers to copying a copy of the actual parameters when calling a function and passing it into the function. In this way, if the parameters are modified in the function, the actual parameters will not be affected.

func num(x, y int) int {
       // Processing logic}

Reference pass

It means that when calling a function, the address of the actual parameter is passed to the function, so the modifications made to the parameters in the function will affect the actual parameters.

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

    temp = *x /* Save the value of x */
    *x = *y   /* Assign y value to x */
    *y = temp /* Assign the temp value to y*/
}

Whether it is value passing or reference passing, the function is a copy of the variable, but value passing is a copy of the value.

Reference transfer is a copy of the address. Generally speaking, address copying is more efficient. The value copy depends on the size of the copy object. The larger the object, the lower the performance.

Map, slice, chan, pointer, and interface are passed in reference form by default.

Undetermined parameter transfer value

The parameters of the function are not fixed, the following types are fixed. (Variable parameters)

golang variable parameters are essentially slices.

When assigning parameters, you can do not need to assign one by one. You can directly pass an array or slice. Pay attention to adding the parameters afterJust do it.

func myfunc(args ...int) {    //0 or more parameters}
func add(a int, args ...int) int {    //1 or more parameters}
func add(a int, b int, args ...int) int {    //2 or more parameters}

where args is a slice type, we can usearg[index]Access all parameters in turn, throughlen(arg)To determine the number of passed parameters.

When using slice objects as variable parameters, 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 (s, x) // String stitching}
func main() {
    s := []int{1, 2, 3}
    res := sum("sum: %d", s...)    // slice... Expand slice    println(res)
}

Output result

sum: 6

Indefinite parameters of any type

Neither the parameters of the function nor the type of each parameter are fixed.

useinterface{}Passing arbitrary types of data is a conventional usage of Go, andinterface{}The type is safe.

func myfunc(args ...interface{}){ // Example usage  ...
}

Return value

_Identifier, used to ignore a return value of a function.

The return value of Go can be named, and the name of the return value should have certain meanings.

The return statement without parameters returns the current value of each return variable, and this usage is called "naked" return.

func myfunc(arg int) (arg int) { // Example usage   return arg
}

The above is the detailed explanation of the basic function usage (parameter value transfer) example of Golang. For more information about the parameter value transfer of Golang function, please pay attention to my other related articles!