为了账号安全,请及时绑定邮箱和手机立即绑定

初始化嵌套匿名结构

初始化嵌套匿名结构

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 关注
  • 95 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信