This is a creation in Article, where the information may have evolved or changed.
Functions are an important part of the go language.
First, function definition
Package Mainimport "FMT"//This function calculates the and of two int input data and returns the int type and the Func plus (a int, b int) int {//GO need to return the value explicitly with return statement A + b}func main () {//functions are called in a simple way//"name (parameter list)" Res: = Plus (1, 2) fmt. Println ("1+2 =", res)}
The output result is
1+2 = 3
Here, the function can be defined in front of the main () function, or after main (). The following code is equivalent to the above:
Package Mainimport "FMT" Func Main () {///function is very simple to call//"name (parameter list)" Res: = Plus (1, 2) fmt. Println ("1+2 =", res)}//This function computes the and of two int input data and returns the int type and the Func plus (a int, b int) int {//GO needs to return the value explicitly with return a + B}
Second, function named return value
The function accepts parameters. In Go, a function can return more than one result parameter, not just a value. They can be named and used like variables.
If the return value parameter is named, a statement with no arguments return
returns the current value as the return value. Note that if you encounter a code block with the same name as a return value, you also need to explicitly write out the return value.
Package Mainimport "FMT" func split (sum int) (x, y int) {x = sum * 4/9 y = sum-x return}func main () (FMT). Println (Split (17))}
Run results
7 10