Error Handling

Sample code link: (https://repl.it/@jjoco/go-error-handling)

Some functions return an error type

1
2
3
4
sampleVar, err := canReturnErrorFct(args)
if err != nil {
    //Do something if there's an error
}
If the function is successful, err would be nil.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sampleStr := "ffgds"
strToInt, err := strconv.Atoi(sampleStr)

if err != nil {
    log.Fatal(err)
}

fmt.Println(strToInt)
// Output = "2020/10/22 19:39:38 strconv.Atoi: parsing "ffgds": invalid syntax"
// "exit status 1"
Ignoring errors

If you're feeling ambitious, you can certainly skip error handling of a function like below using _:

1
sampleVar, _ := canReturnErrorFct(args)
However, only do this if you're really confident that the function will not return an error ever