Go language functions


Functions are basic blocks of code that perform a task.

The Go language has at least a main() function.

You can divide different functions through functions. Logically, each function performs a specified task.

The function declaration tells the compiler the name, return type, and parameters of the function.

The Go language standard library provides a variety of built-in functions that can be used. For example, the len() function can accept arguments of different types and return the length of that type. If we pass in a string, the length of the string is returned. If we pass in a number, the number of functions contained in the array is returned.


Function definition

The Go language function definition format is as follows:

func function_name( [parameter list] ) [return_types] {
   函数体
}

Function definition analysis:

  • func: function Declaration starting from func

  • function_name: function name, function name and parameter list together constitute the function signature.

  • parameter list]: Parameter list, the parameter is like a placeholder, when the function is called, you can pass the value to the parameter, this value is called the actual parameter. The parameter list specifies the parameter type, order, and number of parameters. Parameters are optional, which means that the function can also contain no parameters.

  • return_types: Return type, the function returns a list of values. return_types is the data type of the column value. Some functions do not require a return value, in which case return_types is not required.

  • Function body: a collection of code defined by the function.

Example

The following example is the code of the max() function. This function passes in two integer parameters num1 and num2 and returns the maximum value of these two parameters. Value:

/* 函数返回两个数的最大值 */
func max(num1, num2 int) int {
   /* 声明局部变量 */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result 
}

Function call

When you create a function, you define what the function needs to do, and you perform the specified task by calling the function.

Call a function, pass parameters to the function, and return a value, for example:

package main

import "fmt"

func main() {
   /* 定义局部变量 */
   var a int = 100
   var b int = 200
   var ret int

   /* 调用函数并返回最大值 */
   ret = max(a, b)

   fmt.Printf( "最大值是 : %d\n", ret )
}

/* 函数返回两个数的最大值 */
func max(num1, num2 int) int {
   /* 定义局部变量 */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result 
}

The above example calls the max() function in the main() function, and the execution result is:

最大值是 : 200

Function returns multiple values

The Go function can return multiple values, for example:

package main

import "fmt"

func swap(x, y string) (string, string) {
   return y, x
}

func main() {
   a, b := swap("Mahesh", "Kumar")
   fmt.Println(a, b)
}

The execution result of the above example is:

Kumar Mahesh

Function Parameter

If a function uses parameters, the variable can be called the formal parameter of the function.

Formal parameters are like local variables defined in the function body.

When calling a function, parameters can be passed in two ways:

Transfer typeDescription
Value passingValue passing refers to copying a copy of the actual parameters to the function when calling the function, so that if the parameters are modified in the function, they will not affect the actual parameters.
Pass by referencePass by reference means to pass the address of the actual parameters to the function when calling the function. Then the modification of the parameters in the function will affect the actual parameters.

By default, the Go language uses value transfer, that is, the actual parameters will not be affected during the call.


Function Usage

Function UsageDescription
Function as valueFunction can be used as value after definition
ClosureClosure is an anonymous function that can be used in dynamic programming Use the
method in The method is a function that contains the receiver