英文:
Cannot init struct in if construct in Go
问题
当我注意到以下代码片段无法编译时,我感到非常惊讶:
aTime := time.Time{}
if defaultTime := time.Time{}; aTime != defaultTime {}
编译器返回以下错误信息:
type time.Time is not an expression
defaultTime := time.Time used as
value undefined: defaultTime
这里的意图是测试变量aTime是否设置为其默认值。
如果我获取结构体的指针(defaultTime := &time.Time{}),它也无法编译通过。
然而,如果我在if语句之外初始化defaultTime,或者使用内置的new()函数进行初始化,它就可以编译通过:
aTime := time.Time{}
if defaultTime := new(time.Time); aTime != *defaultTime {}
根据我在各个地方阅读到的内容,new(myStruct) 应该与 &myStruct{} 完全等价。
根据我的理解,defaultValue := time.Time{} 被认为是一个SimpleStmt(具体来说是一个Assignment),详细说明在If语句规范中。
尽管我尽力搜索,但对于这种行为我找不到解释。如果有人能帮我理清思路,我将不胜感激。
英文:
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.
答案1
得分: 10
{
被识别为Block
的开始,终止了对SimpleStmt
的解析。在做出这个决定后,编译器认为aTime := time.Time
作为SimpleStmt
是无效的,因为time.Time
不是可以赋值的值。然而,解析器可能已经太晚尝试另一种对{
的解释。
使用new
的版本有效,因为它不包含{
字符,因此避免了以这种方式混淆解析器。
您还可以通过将其放在括号中使用文字格式,因为块不能在表达式中间合法地开始,所以这也有效:
if defaultTime := (time.Time{}); aTime != defaultTime {
// ...
}
gofmt
给出了有用的消息“expected boolean expression, found simple statement (missing parentheses around composite literal?)”,但奇怪的是,go编译器本身没有。
英文:
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.
答案2
得分: 0
我不认为我要提出的建议一定是一个更好的解决方案。但是对于你的用例,你可以尝试这个简洁的方法:
if !aTime.IsZero() {
// ...
}
你的情况可能会有所不同。
英文:
I don't think what I am going to suggest is necessarily a better solution. But for your use case, you can try this to be concise
if !aTime.IsZero() {
// ...
}
Ymmv
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论