function declaration
function declaration includes function name, parameter list, return value list (optional), function body composition
func test(parameters) (returns) { // ...}
Where parameters is the formal parameter list of the function, describes the function parameter name and parameter type, can have no parameters but this parenthesis can not be omitted; Returns is the return value list, which describes the variable name (optional) and type of the function return value, and if the function returns an argument with no name or no return value, you can leave the parentheses
Neither the parameter nor the type of the return value can be omitted, but it may be abbreviated (if any)
For example:
func add(x int, y int) int { return x + y;}// 可以简写成func add(x, y int) int { return x + y;}
It is important to note that function calls must provide arguments for all arguments in the order in which they are declared. And the function of the go language has no default value and cannot be specified by the parameter name, so the parameter name of the parameter and return value is useless to the function caller, but is used inside the function
How parameters are passed
If the argument is a reference type, such as a pointer, Slice,map,function,channel, and so on, the modification within the function will affect the incoming argument;
Other types of arguments are copies of values that are passed, and modifications to them do not affect the outside of the function.
Variable length parameter
语法:func test(x int, ns ...int) { //...}
is to precede the parameter type with three points "...", the point to note here is that the argument is essentially a slice, can be accessed directly inside the function, and a function can only have a variable length parameter, and only in the last position
For example:
func add(x int, nums ...int) int { for i := range nums { x += nums[i] } return x}
Above is the implementation of the N number of additions
At the same time the argument function is called, you can also use slice as an argument, but need to expand, that is, after the slice "..."
Call Mode:
add(10, 20, 30) // 60// 也可以使用slices := []int{10, 20, 40}add(10, ...s) // 80
Multiple return values
The Go language functions support multiple return values
func test() (int, int) { return 1, 2}
This returns two values, which requires two variables to be received, or you can use _ Receive to ignore a parameter
And we can also give a name for the return value.
func test() (x int, y, int) { x = 10 y = 10 return}
You can now use return implicitly returning parameters, which will automatically return the parameter of the corresponding name, noting that the name of the return value here does not affect the outside of the function.
anonymous functions
Go also supports anonymous functions, which can be assigned directly to variables, or as fields of structs.
var add = func(x, y int) { return x + y;}// 然后就可以使用变量名调用add(10, 20) // 30
Summary of features of functions
- function declaration includes function name, parameter list, return value list (optional), function body composition
- Parameter default values are not supported
- If the argument is a reference type, such as a pointer, Slice,map,function,channel, and so on, the modification will affect the argument;
- Other types of arguments are copies of values that are passed, and modifications to them do not affect the outside of the function
- Variable-length parameter support
- Support for multiple return values
Go language Learning-functions