golang: invalid operation: someVar == "" (mismatched types *string and string)

14 Jul 2014 in TIL

I was trying to see if a value was empty in a golang script I was writing, when I came across an odd error message:

invalid operation: someVar == "" (mismatched types *string and string)

It turns out that the following code doesn't return a simple string, but an instance of flag.String

go
someVar := flag.String("somevar", "", "this is a random flag")

To perform the comparison, you need to use a pointer to it as follows:

go
if *someVar != "" {
// Do this
}