Constants and variables are a kind of carrier of data. When data is operated by different operators, a certain degree of control over the calculation process is flow control, and less than a lot of data is stored by the built-in container. Then what should we do after that? It is through the integration of all the concepts above, it is abstracted into a module to complete a certain function, which is the topic of this paper – function.
Function is not a new concept. You hear the term function in many languages, so how do you use it in Go? How does it differ from other languages?
1. Function definition
In Go, a function definition starts with the keyword func, which has the following standard format:
Func function name (argument list) (return argument list) {function body}Copy the code
Definition:
func
: Function by keywordfunc
Let’s start by showing that this is a function.- Function name: The function name must comply with the variable naming conventions. The function name and parameter list together form the function signature. You cannot define the same function name within the same package. (Remember: Function overloading is not supported in Go!)
- Argument list: Arguments are placeholders that you can pass to when a function is called. This value is called the actual argument. The parameter list specifies the type, order, and number of parameters. Arguments are optional, which means functions can have no arguments.
- Return type: The data type of a function that returns a list of values. Sometimes a return value is not required, and the return type may not be.
- Function body: The collection of code defined by a function that implements specific function logic.
According to the function parameters, it can be divided into three categories, which will be elaborated in detail next.
- Fixed parameters
- Variable parameter
- Fixed and variable parameters
1.1 Function definition of fixed parameters
The function definition format is as follows:
Func function name (argument list) (return argument list) {function body}Copy the code
For example, multiply two numbers:
func multipleForTowNum(num1 int, num2 int) int {
result := num1 * num2
return result
}
Copy the code
1.2 Function definition of variable parameters
There is another type of variadic function that is supported in the Go language. The function definition format is as follows:
Func function name (v... T) (return argument list) {function body}Copy the code
Note: v… T indicates that V is a slice and T is the type of data in the slice.
For example, implementing majority multiplication:
func multipleForNums(nums ...int) int {
var result int = 1
for _, num := range nums {
result = result * num
}
return result
}
Copy the code
Nums is a slice used to store arrays of ints of variable length.
1.3 Function definition of fixed and variable parameters
The definitions of fixed and variable parameters have been introduced above. If fixed and variable parameters coexist, how to define them?
The format is as follows:
Func function name (fixed argument list, v... T) (return argument list) {function body}Copy the code
For example, after adding two numbers, multiply the majority:
func multipleForTowNums(num1 int, num2 int, nums ...int) int {
result := num1 + num2
for _, num := range nums {
result = result * num
}
return result
}
Copy the code
2. Classification of functions
Functions fall into two broad categories: custom functions and built-in functions.
2.1 Custom functions
For example, the example functions in function Definitions in the previous section are custom functions that start with the keyword func as required. Custom functions, on the other hand, refer to functions other than the Go language built-in functions.
2.2 Built-in Functions
Built-in functions, such as Len and Append, are directly used without importing packages. There are 15 built-in functions in Go, as follows:
The function name | instructions |
---|---|
make |
For slice,channel Type allocates memory and initializes objects. |
len |
Compute arrays, slicing, mappingmap , channel,channel The length of the. |
cap |
Compute arrays, slices, channelschannel The capacity. |
delete |
Delete the mappingmap The corresponding key-value pair in. |
append |
Add data to the end of the original slice. |
copy |
The data of the original slice is copied to the new slice. When the space of the new slice is insufficient, the redundant data is directly discarded. |
new |
Is slice and mapmap , channel,channel A type other than type allocates memory and initializes an object, returning a pointer of type. |
complex |
You get a complex number. |
real |
Returns the real part of the complex number. |
imag |
Returns the imaginary part of the complex number. |
print |
Print information to standard output. |
println |
Prints the information to standard output and wraps it. |
close |
Used to close channelschannel . |
panic |
Used to trigger an outage. |
recover |
To capturepanic Exception information thrown. |
3. Function usage
Next, a simple example shows how to use functions correctly.
The following is a function that computes the maximum value of two numbers, Max.
package main
import "fmt"
func main(a) {
fmt.Print(max(10.7))}// Get the maximum value of two numbers
func max(num1, num2 int) int {
if num1 > num2 {
return num1
} else {
return num2
}
}
Copy the code