Go Language Functions

Functions are basic code blocks used to perform a task.

Go language has at least one main() function.

You can divide different functions by using functions; logically each function executes a specified task.

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

The Go standard library provides various built-in functions that are available. For example, the len() function can accept arguments of different types and return the length of that type. If we pass a string, it returns the length of the string; if we pass an array, it returns the number of elements contained in the array.


Function definition

The format for defining functions in Go is as follows:

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

Function Definition Analysis:

  • func: A function is declared starting with func.
  • function_name: The function name, parameter list, and return value type constitute the function signature.
  • parameter list: The parameter list. Parameters are like placeholders. When a function is called, you can pass values to the parameters, and these values are called actual parameters. The parameter list specifies the parameter types, order, and number of parameters. Parameters are optional, meaning a function may not contain parameters.
  • return_types: The return type. The function returns a list of values. return_types is the data type of that list of values. Some functions do not need a return value; in this case, return_types is not necessary.
  • Function body: the collection of code that defines the function.

Example

The following example is the code for the max() function, which takes two integer parameters num1 and num2 and returns the maximum of these two parameters:

Example

/* Function returns the maximum of two numbers */
func max(num1, num2 int) int {
   /* declare local variables */
   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 the specified task is executed by calling the function.

Calling a function, passing arguments to the function, and returning values, for example:

Example

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 100
   var b int = 200
   var ret int

   /* Call function and return the maximum value */
   ret = max(a, b)

   fmt.Printf( "Maximum value is : %d\n\n", ret )
}

/* Function returns the maximum of two numbers */
func max(num1, num2 int) int {
   /* Define local variables */
   var result int

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

In the above example, the max() function is called in the main() function, and the execution result is:

最大值是 : 200

Functions Returning Multiple Values

Go functions can return multiple values, for example:

Example

package main

import "fmt"

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

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

The execution result of the above example is:

Example Google

Function parameters

If a function uses parameters, those variables can be called the formal parameters of the function.

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

When calling a function, there are two ways to pass arguments:

Passing type Description
Pass by value Pass by value means copying the actual parameter and passing it to the function when calling the function, so that if the parameter is modified in the function, it will not affect the actual parameter.
Pass by reference Passing by reference means passing the address of the actual parameter into the function when calling it. Modifications made to the parameter inside the function will then affect the actual parameter.

By default, the Go language uses value passing, which means the actual parameters will not be affected during the call.


Function usage

Function usage Description
Function as an argument of another function After a function is defined, it can be passed as an actual parameter to another function.
Closure A closure is an anonymous function that can be used in dynamic programming.
Methods A method is a function that contains a receiver.
other extensions