Errors in Go
Go programs express error state with error
values.
The error
type is a built-in interface similar to fmt.Stringer
:
1type error interface {
2 Error() string
3}
functions often return an error
value, and calling code should handle errors by testing whether the error equals to nil
:
1i, err := strconv.Atoi("42")
2if err != nil {
3 fmt.Printf("couldn't convert number: %v\n", err)
4 return
5}
6fmt.Println("Converted integer:", i)
References
#pointer #errors #stack #for_the_love_of_go #interface #methods #pass_by #reference #value #golang #programming #heap