亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在這種情況下括號是什么意思?

在這種情況下括號是什么意思?

Go
紅顏莎娜 2023-03-21 15:10:34
在一些源代碼中我發現了這個:if etherbase != (common.Address{}) {    return etherbase, nil}etherbase是類型common.Address,它被定義為:// Lengths of hashes and addresses in bytes.const (    HashLength    = 32    AddressLength = 20)// Address represents the 20 byte address of an Ethereum account.type Address [AddressLength]byte問題是:parethesis 在這種情況下是什么意思?為什么不能省略它們?像這樣:if etherbase != common.Address{} {    return etherbase, nil}
查看完整描述

1 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

Go 編程語言規范

復合文字

當使用 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


查看完整回答
反對 回復 2023-03-21
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號