初始化嵌套匿名结构

我想用 func 处理结构内部的结构:我的代码:


package models


type 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

https://img4.mukewang.com/64d0ce1e00013cd808750266.jpg

我缺少什么?



万千封印
浏览 99回答 3
3回答

烙印99

如果您将 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,        },    }}

慕盖茨4494581

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

慕侠2389804

错误是因为 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}
打开App,查看更多内容
随时随地看视频慕课网APP