2.10. Go language function

发布时间 : 2025-10-25 13:32:57 UTC      

Page Views: 10 views

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.

2.10.1. 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.

2.10.2. 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 } 

2.10.3. 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 

2.10.4. Function returns multiple values #

Go function can return multiple values, such as:

2.10.5. 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 

2.10.6. 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.

2.10.7. 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

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。