(Go Lang) Multiple Return Values (GBEN)

Go by Example

Example

  • They are idiomatic

// The (int, int) in this function signature shows that the function returns 2
// ints.
vals := func()(int, int) {
  return 3, 7
}

// Here we use the 2 different return values from the call with multiple
// assignment.
a, b := vals()
fmt.Println(a)
fmt.Println(b)
// If you only want a subset of the returned values, use the blank identifier
// _.
_, c := vals()
fmt.Println(c)
3
7
7

Attribution

Leave a Reply

Your email address will not be published. Required fields are marked *