I trying to learn error handling in golang to understand how error handling works. I have the following code:
var a int8
var b int32
var err error
c := a + b //types mismatched error
if err != nil {
fmt.Println(err)
}
when I try to run this from within vim with :GoRun
I get types mismatched error.
My question is how do I catch that error and print message to screen if this is even possible as the error occurs during compiling?
Trying to add two different types in Go is a compile-time error. The program will never compile, and therefore never run, so there's nothing to catch--except when writing your program.
You'll only check err
when it's returned from a function. You have no function here, so your err
is never set (aside from the fact that your code won't compile).
This is different from languages like JavaScript or Perl, which don't have strict type checking, so you do get runtime errors with things like this.