How to use return in go language
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Resource management in Go programming: Mysql and Redis connect and release in learning how to correctly manage resources, especially with databases and caches...

Automatic deletion of Golang generic function type constraints in VSCode Users may encounter a strange problem when writing Golang code using VSCode. when...
