In the Go language, the return keyword is used to terminate a function and optionally return one or more result values. If the return statement is used in an ordinary function, it means to jump out of the function and no longer execute the code after the return in the function; if the return statement is used in the main function, it means to terminate the main function, that is, to terminate the running of the program.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
In the Go language, in addition to goto, break and continue, jump control statements can also use return.
If the return statement is used in an ordinary function, it means to jump out of the function and no longer execute the code after return in the function. It can be understood as terminating the function and optionally returning one or more result values.
If the return statement is used in the main function, it means to terminate the main function, that is, to terminate the running of the program.
Syntax:
Use the return statement to terminate the execution of function fun without returning a value.
func fun(){ //do something return }
Use the return statement to terminate the execution of the function and return the value.
func funcName(param1, param2 paramType1, ...)(returnType1, returnType2, ...){ //执行语句... return 返回值列表 }
Explanation
#The return value of the function can be specified as one or more. When the return value is multiple, a list of returnType needs to be Use parentheses, otherwise the syntax will report an error.
When a function returns multiple values, when calling the function, the relative number of parameters must also be used to accept the return value. If the return value is not needed, we can use anonymous variables to accept the save.
Use cases of return statement
1. Use the return statement to terminate function execution without returning a value
package main import "fmt" func checkUserParam(val int){ if val <= 0{ fmt.Println("UserParam check Error") return } fmt.Println("UserParam check OK") } func main() { //使用return语句,终止函数执行 checkUserParam(0) fmt.Println("Over") }
2. Return one value
package main import ( "fmt" ) func gcdNormal(x, y int) int { var n int if x > y { n = y } else { n = x } for i := n; i >= 1; i-- { if x%i == 0 && y%i == 0 { return i } } return 1 } func main() { //函数只返回一个值 returnVal := gcdNormal(10,20) fmt.Println("returnVal =", returnVal) }
3. Return multiple values
package main import ( "fmt" "math" ) func getPageCount(pageSize int)(bool, int){ if pageSize <= 0{ return false, 0 } pageCount := math.Ceil(float64(100/pageSize)) return true, int(pageCount) } func main() { //Go语言的函数可以返回多个值 if isOk, pageCount := getPageCount(10); !isOk{ fmt.Println("Error") }else{ fmt.Println("Ok, PageCount =", pageCount) } }
Summary
The return keyword is used to terminate a function and optionally return one or more result values. It can also be used to control program jumps.
If the return statement is used in an ordinary function, it means to jump out of the function and no longer execute the code after the return in the function. It can be understood as a termination function.
If the return statement is used in the main function, it means to terminate the main function, that is, to terminate the running of the program.
[Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of How to use return in go language. For more information, please follow other related articles on the PHP Chinese website!