初始化嵌套的匿名结构

时间:2019-12-05 04:41:35

标签: go struct

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

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,
    }
}

我的函数GetResponseGetCalendar返回结构时看不到ResponseGetCalendar struct内部的成员。

enter image description here

我想念什么?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

LPWIN32_FIND_DATA fd = 0; HANDLE findFile = 0; do { findFile = FindFirstFile((LPCWSTR)"HxD.exe", fd); if (fd == 0) { std::cout << "ERROR " << GetLastError(); // prints "ERROR 2" } } while (FindNextFile(findFile, fd)); CloseHandle(findFile); 是一个匿名结构。初始化匿名结构可能很乏味。您必须这样做:

View

您可以执行以下操作:

&ResponseGetCalendar{
   View: struct { // List all elements of View here}
            { // List them again and initialize them here}
}

答案 2 :(得分:0)

错误是由于ResponseGetCalendar结构内缺少结构视图所致。将您的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
}