Table of contents
4. The function returns multiple values
1. Built-in functions:
Go language has some built-in functions that can be used without import operation.
They can sometimes operate on different types, such as: len, cap, and append, or must be
operating at the system level,
For example: panic. Therefore, they require direct support from the compiler.
Under Windows, the location of the standard library is a subdirectory under the root directory of the Go language
in pkg\windows_amd64;
Under Linux, the standard library is in the subdirectory of go env == "goroot" in the root directory of the Go language
in pkg\linux_amd64
(If the installation is 32-bit, it will be in the linux_386 directory).
2. Standard library functions
Normally, the standard package will be stored in the $GOROOT/pkg/$GOOS_$GOARCH/ directory.
The standard library of the Go language provides support in the form of packages. The following table lists the common packages and
its function.
Online standard library: http://www.golang.ltd/
https://studygolang.com/pkgdoc
https://pkg.go.dev/std official help
3. Custom functions
Functions are basic blocks of code that perform a task.
Go language has at least one main() function.
You can use functions to divide different functions. Logically, each function performs a specified task.
A function declaration tells the compiler the function's name, return type, and parameters.
Three elements of a function:
1. Name == "function
2. Parameter == "Interface
3. Return value == "result
Function classification:
1. Built-in functions == "do not need to refer to other modules, use eg:len() directly
2. Standard library function == "refer to third-party modules, or other modules, use with names
fmt.Println()
3. User-defined functions == "Programmers write and call by themselves.
1. Function definition
The format of Go language function definition is as follows:
func function_name ( [parameter list] ) [return_types] {
function body
}
//parameter parameter defines the type of return_types return value
eg
func add (a,b int) int {
return a + b
}
Function definition analysis:
• func: Functions are declared beginning with func
• function_name: the name of the function, the function name and the parameter list form the function signature together.
Note: Capitalized initials indicate public functions, lowercased initials indicate private functions.
• parameter list]: parameter list, the parameter is like a placeholder, when the function is called, you
Values can be passed to parameters,
This value is called the actual parameter. The parameter list specifies the parameter type, order and number of parameters.
number. parameter is optional,
That is to say, the function can also contain no parameters.
• return_types: return type, the function returns a list of values. return_types is the number of values in the column
data type.
Some functions do not need to return a value, in which case return_types is not necessary.
• Function body: the collection of code for a function definition.
Example, the following example is the code of the max() function,
The function takes 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
}
2. Function call
When you create a function, you define what the function needs to do, and you call the function to perform the specified task.
Call a function, pass parameters to the function, and return a value, for example:
package main
import "fmt"
func main() {
/* 定义局部变量 */
var a int = 100
var b int = 200
var ret int
/* 调用函数并返回最大值 */
ret = max(a, b) // a ,b 实参
fmt.Printf( "最大值是 : %d\n", ret )
}
/* 函数返回两个数的最大值 */
// num1,num2是 形参
func max(num1, num2 int) int {
/* 定义局部变量 */
var result int
if (num1 > num2) {
result = num1
} else {
result = num2
}
return result
}
The above example calls the max() function in the main() function, and the execution result is:
Maximum value is: 200
3. Function parameters
If a function uses parameters, the variable can be called a formal parameter of the function.
Formal parameters are like local variables defined within the body of a function.
There are two ways to pass parameters when calling a function:
Pass by value:
Passing refers to copying the actual parameters to the function when calling the function.
In this way, if the parameters are modified in the function, the actual parameters will not be affected.
By default, the Go language uses value passing, that is, it will not affect the actual
parameter.
Next, let's call the swap() function using pass-by-value:
package main
import "fmt"
func main() {
/* 定义局部变量 */
var a int = 100
var b int = 200
fmt.Printf("交换前 a 的值为 : %d\n", a )
fmt.Printf("交换前 b 的值为 : %d\n", b )
/* 通过调用函数来交换值 */
swap(a, b)
fmt.Printf("交换后 a 的值 : %d\n", a )
fmt.Printf("交换后 b 的值 : %d\n", b )
}
/* 定义相互交换值的函数 */
func swap(x, y int) int {
var temp int
temp = x /* 保存 x 的值 */
x = y /* 将 y 值赋给 x */
y = temp /* 将 temp 值赋给 y*/
return temp;
}
The program uses value passing, so the two values do not interact, we can use references
Pass to achieve the exchange effect.
Pass by value by reference:
Passing by reference refers to passing the address of the actual parameter into the function when calling the function.
Then the modification of the parameters in the function will affect the actual parameters.
The pointer parameter is passed by reference to the function. The following is the exchange function swap() that uses the passed by reference
hand over:
Below we call the swap() function by using pass by reference:
package main
import "fmt"
func main() {
/* 定义局部变量 */
var a int = 100
var b int= 200
fmt.Printf("交换前,a 的值 : %d\n", a )
fmt.Printf("交换前,b 的值 : %d\n", b )
/* 调用 swap() 函数
* &a 指向 a 指针,a 变量的地址
* &b 指向 b 指针,b 变量的地址
*/
swap(&a, &b)
fmt.Printf("交换后,a 的值 : %d\n", a )
fmt.Printf("交换后,b 的值 : %d\n", b )
}
func swap(x *int, y *int) {
var temp int
temp = *x /* 保存 x 地址上的值 */
*x = *y /* 将 y 值赋给 x */
*y = temp /* 将 temp 值赋给 y */
}
4. The function returns multiple values
Go functions 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 execution result of the above example is:
Kumar Mahesh
5. defer statement
The defer statement in the Go language will delay the processing of the following statements
When the function to which defer belongs is about to return, the delayed processing statements will be executed in reverse order according to the order defined by defer, that is, first in last out
package main
import "fmt"
func main() {
fmt.Println("开始")
defer fmt.Println(1)
defer fmt.Println(2)
defer fmt.Println(3)
fmt.Println("结束")
}
The above code execution results are as follows
start
Finish
3
2
1
6. The init function:
Each source file can contain an init function, which will be executed before the main function,
Called by the go framework, that is to say, the init() function will be executed before the main() function.
Results of the:
practise:
1. Write a function to complete the addition of two numbers and return the result.
2. Write a function to complete the statistics of a string, Chinese, English letters, numbers, spaces, and the number of occurrences:
3. Write a function to detect palindrome (Chinese)