Tracks
/
Go
Go
/
Syllabus
/
Multiple Return Values
Mu

Multiple Return Values in Go

0 exercises

About Multiple Return Values

Go functions and methods can return multiple values. Very often, a second return value is used to return an error. For example:

func GetCard() (Card, error) { ... }

The assignment for multiple return values just uses a comma to separate the variables:

card, err := GetCard()

If statements can use an initializer before the condition separated by a semicolon. This is a common idiom seen for error handling:

if card, err := GetCard(); err != nil {
    // handle the error
}
Edit via GitHub The link opens in a new window or tab