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

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

參考內部結構

參考內部結構

Go
回首憶惘然 2023-05-04 16:49:30
我正在嘗試以編程方式創建一些 API 文檔,我有這個:type APIDoc struct {    Route           string    ResolutionValue struct {       v           string    }}然后我嘗試這樣做:    json.NewEncoder(w).Encode(APIDoc.ResolutionValue{"foo"})但它說APIDoc.ResolutionValue 未定義(類型 APIDoc 沒有方法 ResolutionValue)所以我求助于這樣做:type ResolutionValue struct {    v string}type APIDoc struct {    Route           string    ResolutionValue ResolutionValue}然后做:    json.NewEncoder(w).Encode(ResolutionValue{"foo"})有點蹩腳,有沒有辦法以某種方式確保完整性?
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

從 Go 1.11 開始,不支持嵌套類型。

在我看來,您的修訂版看起來好多了。

編輯:可能與問題無關,但您可以使用類型嵌入來簡化您的類型。但是,請注意表示形式不同:

type Inner struct {

? ? Whatever int

}


type ResolutionValue struct {

? ? Val string

? ? Inner

}


type ResolutionValue2 struct {

? ? Val? ? string

? ? Inner Inner

}


func main() {


? ? a, _ := json.Marshal(ResolutionValue{})

? ? b, _ := json.Marshal(ResolutionValue2{})

? ? fmt.Printf("%s\n%s", a, b)


}

哪個打?。?/p>


{"Val":"","Whatever":0}

{"Val":"","Inner":{"Whatever":0}}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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