Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot init struct in if construct in Go

I got a great surprise when I noticed the following snippet not compiling:

aTime := time.Time{}
if defaultTime := time.Time{} ; aTime != defaultTime {}

The compiler returns:

type time.Time is not an expression

defaultTime := time.Time used as

value undefined: defaultTime

The intent here is to test the aTime variable if it's set to it's default value.

It also does not compile if I get the pointer of the struct (defaultTime := &time.Time{}).

However, it does compile if I init defaultTime outside of the if construct, or do the init using the new() builtin:

aTime := time.Time{}
if defaultTime := new(time.Time) ; aTime != *defaultTime {}

From what I've read everywhere, new(myStruct) it supposed to be completely equivalent to &myStruct{}.

As I interprate it, defaultValue := time.Time{} qualifies as a SimpleStmt (specifically an Assignment), as detailed in the If statement spec.

I've come up with no explanation for this behavior, despite my best googling efforts. Would be grateful if someone could make my head stop spinning.

like image 836
Anders Olsson Avatar asked Jul 10 '26 21:07

Anders Olsson


1 Answers

The { is recognized as the beginning of a Block, terminating the parsing of the SimpleStmt. After committing to that decision, the compiler decides that, as a SimpleStmt, aTime := time.Time isn't valid because time.Time isn't a value that can be assigned. However, it's presumably too late for the parser to try another interpretation of the {.

The version with new works because it doesn't contain a { character, and so avoids confusing the parser in this way.

You can also use the literal format by wrapping it in parentheses, because a block can't legally begin in the middle of an expression, so this also works:

if defaultTime := (time.Time{}); aTime != defaultTime {
    // ...
}

gofmt gives the helpful message "expected boolean expression, found simple statement (missing parentheses around composite literal?)", but oddly, the go compiler itself does not.

like image 115
hobbs Avatar answered Jul 13 '26 12:07

hobbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!