1 回答

TA貢獻1893條經驗 獲得超10個贊
當使用 LiteralType 的 TypeName 形式的復合文字出現在關鍵字和“if”、“for”或“switch”語句塊的左大括號之間的操作數時,會出現解析歧義,并且復合文字是不包含在圓括號、方括號或大括號中。在這種罕見的情況下,文字的左大括號被錯誤地解析為引入語句塊的大括號。為了解決歧義,復合文字必須出現在括號內。
if x == (T{a,b,c}[i]) { … } if (x == T{a,b,c}[i]) { … }
塊common.Address{}之前的歧義復合文字。if{ … }
if etherbase != common.Address{} {
return etherbase, nil
}
(common.Address{})塊 { … }之前的明確復合文字if。
if etherbase != (common.Address{}) {
return etherbase, nil
}
例如,
package main
const AddressLength = 20
type Address [AddressLength]byte
func f(etherbase Address) (Address, error) {
// Unambiguous
if etherbase != (Address{}) {
return etherbase, nil
}
return Address{}, nil
}
func g(etherbase Address) (Address, error) {
// Ambiguous
if etherbase != Address{} {
return etherbase, nil
}
return Address{}, nil
}
func main() {}
游樂場:https://play.golang.org/p/G5-40eONgmD
- 1 回答
- 0 關注
- 120 瀏覽
添加回答
舉報