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

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

初始化嵌套匿名結構

初始化嵌套匿名結構

Go
萬千封印 2023-08-07 18:57:03
我想用 func 處理結構內部的結構:我的代碼:package modelstype CalendarPushNotification struct {    Id                  string      `db:"id"`    UserId              string      `db:"user_id"`    EventId             string      `db:"event_id"`    Title               string      `db:"title"`    StartDate           string      `db:"start_date"`    PushDate            string      `db:"push_date"`    PushDeliveryLineId  string      `db:"push_delivery_line_id"`    IsPushDelivered     string      `db:"is_push_delivered"`}type ResponseGetCalendar    struct {    View struct {        EventId            string `db:"event_id"`        Title              string `db:"title"`        StartDate          string `db:"start_date"`        PushDate           string `db:"push_date"`        PushDeliveryLineId string `db:"push_delivery_line_id"`        IsPushDelivered    string `db:"is_push_delivered"`    }`json:"schedules"`}var CalendarUtils = CalendarPushNotification{}func (CalendarPushNotification) GetResponseGetCalendar(model *CalendarPushNotification) * ResponseGetCalendar {    return &ResponseGetCalendar{            EventId:            model.EventId,            Title:              model.Title,            StartDate:          model.StartDate,            PushDate:           model.PushDate,            PushDeliveryLineId: model.PushDeliveryLineId,            IsPushDelivered:    model.IsPushDelivered,    }}返回 struct 時,我的 funcGetResponseGetCalendar無法看到 內部的成員。ResponseGetCalendar struct我缺少什么?
查看完整描述

3 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

如果您將 View 設置為非匿名結構,您可以執行以下操作:


type View struct {

    EventId            string `db:"event_id"`

    Title              string `db:"title"`

    StartDate          string `db:"start_date"`

    PushDate           string `db:"push_date"`

    PushDeliveryLineId string `db:"push_delivery_line_id"`

    IsPushDelivered    string `db:"is_push_delivered"`

}

type ResponseGetCalendar struct {

    Schedules View `json:"schedules"`

}


var CalendarUtils = CalendarPushNotification{}


func (CalendarPushNotification) GetResponseGetCalendar(model *CalendarPushNotification) *ResponseGetCalendar {

    return &ResponseGetCalendar{

        Schedules: View{

            EventId:            model.EventId,

            Title:              model.Title,

            StartDate:          model.StartDate,

            PushDate:           model.PushDate,

            PushDeliveryLineId: model.PushDeliveryLineId,

            IsPushDelivered:    model.IsPushDelivered,

        },

    }

}


查看完整回答
反對 回復 2023-08-07
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

View是一個匿名結構。初始化匿名結構可能很乏味。你必須做:


&ResponseGetCalendar{

   View: struct { // List all elements of View here}

            { // List them again and initialize them here}

}

相反,你可以這樣做:


 ret:= &ResponseGetCalendar{}

 ret.View.EventId=model.EventId

 ...

 return ret


查看完整回答
反對 回復 2023-08-07
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

錯誤是因為 ResponseGetCalendar 結構中缺少結構 View。將您的替換GetResponseGetCalendar func為以下內容:


func (CalendarPushNotification) GetResponseGetCalendar(model *CalendarPushNotification) *ResponseGetCalendar {

    ret := &ResponseGetCalendar{}

    ret.View.EventId = model.EventId

    ret.View.Title = model.Title

    ret.View.StartDate = model.StartDate

    ret.View.PushDate = model.PushDate

    ret.View.PushDeliveryLineId = model.PushDeliveryLineId

    ret.View.IsPushDelivered = model.IsPushDelivered

    return ret

}


查看完整回答
反對 回復 2023-08-07
  • 3 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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