Error handling

Error return contracts

The final error result conventionally indicates success with nil. On failure, document whether other results are usable; some APIs return partial data with a nonnil error. Do not assume every nonnil error implies no result. Example output: '0 true'.

Formal Syntax & Structure

go syntaxgo
func f() (T, error)

Working Example

Example Snippetgo
package main
import "fmt"
func positive(n int) (int, error) {  if n <= 0 {    return 0, fmt.Errorf("not positive")  }  return n, nil}func main() {  v, err := positive(-1)  fmt.Println(v, err != nil)}
Go Reference