This is a creation in Article, where the information may have evolved or changed.
Http://www.jb51.net/article/56831.htm
Functions in the Go language are system functions and custom functions.
1. System functions
System functions are the functions of the Go language, the system functions are generally encapsulated in different packages according to function, such as print, Printf, println are in the FMT package, Abs, sin are in the math package, Listen, dial are in net package and so on. If you want to use system functions, you need to introduce the relevant packages. Look at the following code:
Package Mainimport "FMT" import "Math" Func Main () { A: = Math. Abs ( -1) FMT. Println (A)}
In fact, the first entry of the program main function is also defined in a package, called the main package.
Note: If you import this package without using the resources within the package, this is not allowed in the go language and the system will report an error message.
2. Custom Functions
The function in the go language and the biggest difference in C is that you can return multiple values.
function format:
The custom function uses the keyword func, which defines the following format:
Func function name (parameter 1 parameter 1 type, parameter 2 parameter 2 type,...) (return value type 1, return value type 2,...) { function Body return var1, var2 ...}
- Function Name: is the name of the functions, consisting of letters, numbers, underscores, where the first letter cannot be a number. Within the same package, the name of the function cannot be duplicate.
- Parameter: Declare the parameter to indicate the type, and if all parameters are consistent, just add 1 type declarations at the end.
- return value: Where Var1,var2, ... The data type to be separate and return type 1, return type 2, ... Same. If there is only one return value, you can use parentheses when declaring a return value type.
An example of a function that asks for two numbers:
Func Sum (A, b int) int {//Side A, B is all int, all at last add an int to return a + b}
Example of a function that indicates the return value name:
The function in the go language also has an interesting feature, if the return value name is directly indicated in the return value type declaration, then the return parameter is not required in the return statement. The following example:
Func Power (a int) (result int) { result = A * a return}
The name of the return value is indicated in the example above, so you can use it directly without declaring result in the function, and return directly when the function returns. Note: If you indicate the return value name, even if there is only one return value, enclose it in parentheses.
Examples of functions that return multiple values:
This function is used to find the minimum and maximum values in a shaped array.
Func extremuminarray (Array []int) (int, int) { If Len (array) < 1 { return 0, 0 } min: = array[0] ma x: = Array[0] for _, V: = range array { if v < min { min = v } else if v > max { max = v }< c11/>} return min, max}
When called, separate the returned variable with a comma.
min, max: = Extremuminarray (array)