Go Language Functions
Function is the basic block of code for performing a task.
Go language at least a main () function.
You can divide the different functions via the function logic to perform each function is specified task.
Function declaration tells the compiler function name, return type, and parameters.
Go language standard library provides a variety of built-in functions can use. For example, len () function can accept different types of arguments and returns the type of length. If we pass a string length of the string is returned, if the incoming numbers, the function returns the number contained in the array.
Function definition
Go language function is defined in the following format:
func function_name( [parameter list] ) [return_types] { 函数体 }
Analytical function definition:
- func: function starts a statement by the func
- function_name: function name, function name and parameter list together constitute the function signature.
- parameter list]: list of parameters, parameter as a placeholder, when the function is called, you can pass a value to the parameter, this value is called the actual parameters. Parameter list specifies the parameter type, order, and number of parameters. Parameter is optional, that function can not contain parameters.
- return_types: return type, the function returns a value. return_types is the data type of the column value. Some functions do not need to return a value, in which case return_types not necessary.
- Body of the function: The function defined set of codes.
Examples
The following examples are max () function code, the function passed two integer parameters num1 and num2, and returns the maximum value of these two parameters:
/* 函数返回两个数的最大值 */ 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 a function of what needs to be done to perform the tasks assigned by calling the change function.
Call the function, passing the function parameters and return values, 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 examples call in main () function max () function, the result is:
最大值是 : 200
Function return multiple values
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 above examples Implementation of the results:
Kumar Mahesh
Function Arguments
If the function parameters, the variable may be referred to the function parameter.
Parameters as defined in the local variable functions in the body.
Call the function, you can pass parameters in two ways:
Delivery type | description |
---|---|
Value transfer | Value is passed when calling the function refers to the actual parameters passed to the copy function, so that if the function parameters can be modified, will not affect the actual parameters. |
Passed by reference | Passing by reference refers to when calling the function passes the address of the actual parameter to the function, then the function to modify the parameters carried out, will affect the actual parameters. |
By default, Go language is passed by value, that is, during the call will not affect the actual parameters.
Function usage
Function usage | description |
---|---|
Functions as values | After the function is defined as a value to use |
Closure | Closures are anonymous functions can be used in dynamic programming |
method | The method is a function that contains the recipient |