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

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

如何在結構中打印結構切片的標記

如何在結構中打印結構切片的標記

Go
隔江千里 2023-05-04 17:35:11
我有結構 Config ,它由 Field SliceOfAnotherStruct組成,其中包含指向AnotherStruct 的指針切片 。在這里,我如何才能在 AnotherStruct 中獲取字段 Bank 的 json 標記。type Config struct {    SliceOfAnotherStruct         []*AnotherStruct        `bson:"anotherStruct" json:"anotherStruct" validate:"required,dive,required"`}type AnotherStruct struct {    Name                   string        `bson:"name" json:"name" validate:"required"`    Cost                   string        `bson:"cost" json:"cost" validate:"required"`    Bank    string        `bson:"bank" json:"bank" validate:"required"`    IFSC     string        `bson:"ifsc" json:"ifsc" validate:"required"`}
查看完整描述

1 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

使用創建新變量AnotherStruct,然后使用反射嘗試獲取該Bank字段,之后您將能夠獲取它的標簽。


// new object created from struct AnotherStruct

obj := AnotherStruct{}


// getting `Bank` field information. 

// the `FieldByName` function return two variables, 

// 1. the field data 

// 2. a boolean data, determines whether field is exists or not

bankField, ok := reflect.TypeOf(obj).FieldByName("Bank")

if ok {

    // if the field is exists, then get the desired tag

    jsonTagValue := bankField.Tag.Get("json")

    fmt.Println(jsonTagValue) // bank

}

工作游樂場:https://play.golang.org/p/TJDCEVm23Hz


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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