如果在Go中构造,则无法初始化struct

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.

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.