A Go function can return distinct values from one call. Declare their types in the function signature, return a matching value for each, and assign each result to a variable. Use the blank identifier (`_`) when a result is deliberately unused. section 1.2.1
Listing 1.1 Multiple returns
package main
import (
"fmt"
)
func getStrings() (string, string) { #1
return "Foo", "Bar" #2
}
func main() {
n1, n2 := getStrings() #3
fmt.Println(n1, n2) #3
n3, _ := getStrings() #4
fmt.Println(n3) #4
}
The function returns two strings; its callers either capture both or discard the second with `_`. section 1.2.1
Return values can also be named in the signature. Those names act as variables inside the function, and a bare `return` sends back their current values. More commonly, multiple returns pair a result with an `error`; by convention, the error comes last. section 1.2.1 section 4.1
Listing 4.1 Returning an error
package main
import (
"errors" #1
"strings"
)
func Concat(parts ...string) (string, error) { #2
if len(parts) == 0 {
return "", errors.New("No strings supplied") #3
} #3
return strings.Join(parts, " "), nil #4
}
`Concat` returns a joined string and `nil` on success, or an empty string and an error when given no strings. section 4.1
Check the error before trusting the result. An empty string may be a valid result as well as the value returned on failure, so discarding the error can hide the difference. When an error accompanies a value, choose a useful value where appropriate and document what callers receive under error conditions. section 4.1 section 4.1.1
result, err := Concat(args...)
if err != nil {
// handle err
}
// do something with result
Capture both results, handle a non-nil error, then use the result. section 4.1

