The Error Interface
type error interface {
Error() string
}Atoi
func Atoi(s string) (int, error)// Atoi converts a stringified number to an integer
i, err := strconv.Atoi("42b")
if err != nil {
fmt.Println("couldn't convert:", err)
// because "42b" isn't a valid integer, we print:
// couldn't convert: strconv.Atoi: parsing "42b": invalid syntax
// Note:
// 'parsing "42b": invalid syntax' is returned by the .Error() method
return
}
// if we get here, then the
// variable i was converted successfully1οΈβ£ What is the error Interface?
error Interface?2οΈβ£ A simple example
3οΈβ£ Built-in errors in Go
4οΈβ£ Why return (value, error) instead of exceptions?
(value, error) instead of exceptions?5οΈβ£ Zero Values Rule
6οΈβ£ The Assignment Explained
7οΈβ£ Why itβs designed this way
Goβs approach
Compared to
TL;DR Summary
Concept
Description