Go language function


Release date:2023-09-09 Update date:2023-10-13 Editor:admin View counts:311

Label:

Go language function

A function is a basic block of code used to perform a task.

Go language has at least one main() function.

You can divide different functions by functions, and logically each function performs a specified task.

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

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

Function definition

The format of Go language function definition is as follows:

func function_name( [parameter list] ) [return_types] {
   Function body
}

Function definition parsing:

  • func : The function is composed of func begin to declare

  • function_name : The function name, parameter list, and return value typeconstitute the function signature.

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

  • return_types : Returns the type, and the function returns a list of values return_types is the data type of the column value. Some functions do not need to return a value, in this case return_types is not necessary.

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

Example

The following example is max() function, which passes in two integer arguments num1 and num2 , returns the maximum values of these two parameters:

Example

/* Function returns the maximum value 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 call the function to perform the specified task.

Call the function, pass parameters to the function, and return a value, such as:

Example

package main
import "fmt"
func main() {
   /* Define local variables */
   var a int = 100
   var b int = 200
   var ret int
   /* Call the function and return the maximum value */
   ret = max(a, b)
   fmt.Printf( "The maximum value is : %d\\n", ret )
}
/* Function returns the maximum value 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
}

The above examples are available in main() called in the function max() function, and the result of execution is:

The maximum value is : 200

Function returns multiple values

Go function can return multiple values, such as:

Example

package main
import "fmt"
func swap(x, y string) (string, string) {
   return y, x
}
func main() {
   a, b := swap("Google", "Runoob")
   fmt.Println(a, b)
}

The execution result of the above example is:

Runoob Google

Function parameter

If a function takes an argument, the variable can be called a formal parameter of the function.

A parameter is like a local variable defined in the body of a function.

To call a function, you can pass parameters in two ways:

Transfer type

Description

Value transfer

Value passing means that a copy of the actual parameter is passed to the function when the function is called, so that if the parameter is modified in the function, the actual parameter will not be affected.

Reference transfer

Reference passing means that the address of the actual parameter is passed to the function when the function is called, so the modification of the parameter in the function will affect the actual parameter.

By default Go language uses value passing, that is, the actual parameters are not affected during the call.

Function usage

Function usage

Description

Function as an argument to another function

After the function is defined, it can be passed in as the real parameter of another function.

Closure

Closures are anonymous functions that can be used in dynamic programming

Method

Method is a function that contains the recipient

Powered by TorCMS (https://github.com/bukun/TorCMS).