2.10. Go language function

发布时间 :2023-10-12 23:00:08 UTC      

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.