return error golang code example
Example 1: golang create error
// simple string-based error
err1 := errors.New("math: square root of negative number")
Example 2: errors golang
err := errors.New("emit macho dwarf: elf header corrupted")
if err != nil {
fmt.Print(err)
}
Example 3: go error handling
func doStuff() (int, error) {
}
func main() {
result, err := doStuff()
if err != nil {
// handle error
} else {
// all is good, use result
}
}