liveBook · Manning

Usable result on error

As explained in Go in Practice, Second Edition

A usable result on error is a return-value pattern in which a function provides a meaningful non-error result alongside an error when failure occurs.

Return a meaningful value alongside an error when failure occurs. In Go, multiple returns let a function preserve its result contract instead of returning nil or an unusable placeholder. Check the error first; treating an empty result as the error signal creates ambiguity. section 4.1.1

Concatenating no strings

Concat returns an empty string and an error when called without parts, because an empty string is the expected string-shaped result when there is no data. Callers can inspect the error before using the result, and the function documents this error-case contract. section 4.1.1

Listing 4.3 Returning useful data with an error
func Concat(parts ...string) (string, error) {
     if len(parts) == 0 {
            return "", errors.New("No strings supplied")    #1
     }
    return strings.Join(parts, " "), nil
}

Shows the complete multiple-return implementation, including the usable empty string and errors.New error. section 4.1.1

How Go in Practice, Second Edition covers it

How deep it goes
Explains the return contract, implementation, caller behavior, and documentation, but does not develop broader recovery strategies.
Examples
Uses a Go Concat function, empty-string results, errors.New, ignored errors, and documented error-case returns.
What you can do after
Apply it via Go multiple returns to design clearer functions and handle their errors before consuming results.
How it connects
Connects multiple return values with error handling, nil semantics, panics, and API documentation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.