function
The core design in Go language is declared through the keyword func
func funcName(input type1, input2 type2) (output1 type1, output2 type2) {
//logical code
return value1, value2
}
Basic syntax
1. Syntax
//General functions
func func_name(a int) {
println(a)
}
//Multiple parameters, no return value
func func_name(a, b int, c string) {
println(a, b, c)
}
//Single return value
func func_name(a, b int) int { //Same type, a, b int can be omitted
return a + b
}
//Multiple return values
func func_name(a, b int) (c int, err error) { //The return value can also be (int, error)
return a+b, nil
}
func SumAndProduct(A, B int) (int, int) {
return A+B, A*B
}
2. Description:
Keyword func declaration
There can be one or more parameters, each parameter is followed by a type, and multiple values can be returned through the "," separating function.
Return value declaration, you can declare only the type
If there is no return value, the last return information can be omitted.
If there is a return value, return must be added to the outer layer.
Go functions do not support nested, overloaded and default parameters
support:
1. No need to declare the prototype
2. Indefinite length variable parameters
3. Multiple return values
4. Name the return value parameter
5. Anonymous functions
6. Closure
Notice:
The function starts with func, and the left brace cannot be added to the next line
Functions starting with lowercase letters refer to functions that can be visible in this package, and functions starting with uppercase letters can be called by other packages.
Multiple return values and naming return parameters
Can return multiple results like python, just non-tuple
For unwanted return values, you can throw the trash can
If you return the parameter with a name, the return statement can be empty. return is not empty, the return value order is the order of return rather than the order declared in the function header
package main
func change(a, b int) (x, y int) {
x = a + 100
y = b + 100
return //101, 102
//return x, y //Same as above
//return y, x //102, 101
}
func main(){
a := 1
b := 2
c, d := change(a, b)
println(c, d)
If the naming return parameter is overwritten by the variable of the same name in the code block, the result must be returned using an explicit return
There is no need to force a name to return value, but the named return value can make the code clearer and more readable
Parameter pass: value passing and pointer passing
Pointer, Go reserves pointers, and uses "." instead of "->" to operate pointers target object members.
& Get the variable address
* Indirect access to the target function through pointers
func add1(a int) int {
a = a + 1
return a
}
x := 3
x1 := add1(x)
x //3
x1 //4
Pass the value, the value of x1 has not changed
func add2(a *int) int {
*a = *a + 1
return *a
}
x := 3
x1 := add2(&x)
x // 4
x1 // 4
Passing multiple functions to operate the same object
Passing pointers is lightweight (8byte), just passing memory addresses. We can use pointers to pass large structures.
In Go language, the implementation mechanisms of string, slice, and map are similar to pointers, so they can be passed directly without having to pass pointers after the address is taken.
Note that if the function needs to change the slice length, you still need to get the address and pass the pointer.
Parameter passing: variable parameters
The variable parameter is essentially a slice, and must be the last formal parameter
When passing slice to variable parameter function, please use... to expand it, otherwise it will be treated as a single parameter of the dang, similar to python
package main
func sum(s string, args ...int) {
var x int
for _, n := range args {
x += n
}
println(s, x)
}
func main(){
sum("1+2+3=", 1, 2, 3)
x := []int{0,1,2,3,4}
sum("0+1+2+3=", x[:4]...)
}
...type type can only exist as a parameter type of a function and is the last parameter
Essentially an array slice, i.e. []type
Indefinite parameters of any type
func Printf(format string, args ...interface{}) {
}
Anonymous functions
f := func(x,y int) int {
return x + y
}
Functions as values and types
In Go language, a function is also a variable, which can be defined by type, and its type is that all have the same parameters and the same return value
grammar
type typeName func (input1 inputType1, input2 inputType2 [, ....]) (result1 resultType1 [,....])
Usage.1
type testInt func(int) bool //Declare a function type
func filter(slice []int, f testInt) []int {
var result []int
for _, value := range slice {
if f(value) {
result = append(result, value)
}
}
}
func isOdd(integer int) bool {
if integer % 2 == 0 {
return false
}
return true
}
filter(a, isOdd)
This usage is very useful when writing interfaces
Usage.2
You can define function types or pass functions as values (default value nil)
package main
//Define the function type callback
type callback func(s string)
//Define a function that can receive another function as a parameter
// sum is the parameter name, func(int, int) int is the parameter type
func test(a, b int, sum func(int, int) int) {
println( sum(a,b) )
}
func main(){
//Demo 1
var cb callback
cb = func(s string) {
println(s)
}
cb("hello world")
//Demo 2
test(1, 2, func(a, b int) int {return a + b})
}
result:
hello world
3