capypad
0 day streak
go / beginner
Snippet

Multiple Return Values

Go functions can return multiple values. This is frequently used to return both a result and an error from a single function call.

snippet.go
go
1
2
3
4
5
6
7
func swap(a, b int) (int, int) {
return b, a
}
 
func main() {
first, second := swap(10, 20)
}
Breakdown
1
func swap(a, b int) (int, int)
Defines a function that takes two integers and returns two integers.
2
return b, a
Returns two values separated by a comma.