Ошибка в заполнении структуры со многими вложениями - PullRequest
0 голосов
/ 14 февраля 2019

Я не понимаю, где ошибка, так как после завершения структуры она мне кажется undefined: Payload

Это очень раздражающая структура, потому что в ней достаточно вложенности структур и фрагментов структур

Не могли бы вы помочь мне решить эту проблему, потому что я не могу ее решить?

https://play.golang.org/p/QewpCfTWY0l

package main

import (
    "fmt"
)

type DialogFlowResponseSuggestion struct {
    Payload struct {
        Google struct {
            ExpectUserResponse bool `json:"expectUserResponse"`
            RichResponse       struct {
                Items []struct {
                    SimpleResponse struct {
                        TextToSpeech string `json:"textToSpeech"`
                    } `json:"simpleResponse"`
                } `json:"items"`
                Suggestions []struct {
                    Title string `json:"title"`
                } `json:"suggestions"`
            } `json:"richResponse"`
        } `json:"google"`
    } `json:"payload"`
}

func main() {
    in := DialogFlowResponseSuggestion{
        Payload: Payload{
            Google: Google{
                ExpectUserResponse: true,
                RichResponse: RichResponse{
                    Items: []Items{
                        Items{SimpleResponse: SimpleResponse{dialog.MReturn.Message}},
                    },
                    Suggestions: []Suggestions{
                        Suggestions{Title: "Suggestion Chips"},
                        Suggestions{Title: "suggestion 1"},
                        Suggestions{Title: "suggestion 2"},
                    },
                },
            },
        },
    }

    fmt.Println(in)
}

1 Ответ

0 голосов
/ 14 февраля 2019

Ваши внутренние struct нигде не объявлены - все они анонимные типы.Чтобы на самом деле явно создать их экземпляр по имени, они должны где-то существовать ( детская площадка ):

package main

import (
    "fmt"
)

type SimpleResponse struct {
    TextToSpeech        string          `json:"textToSpeech"`
}

type Item struct {
    SimpleResponse      SimpleResponse  `json:"simpleResponse"`
}

type Suggestion struct {
    Title               string          `json:"title"`
}

type RichResponse struct {
    Items               []Item          `json:"items"`
    Suggestions         []Suggestion    `json:"suggestions"`
}

type Google struct {
    ExpectUserResponse  bool            `json:"expectUserResponse"`
    RichResponse        RichResponse    `json:"richResponse"`
}

type Payload struct {
    Google              Google          `json:"google"`
}

type DialogFlowResponseSuggestion struct {
    Payload             Payload         `json:"payload"`
}

func main() {
    in := DialogFlowResponseSuggestion{
            Payload: Payload{
                Google: Google{
                    ExpectUserResponse: true,
                    RichResponse: RichResponse{
                        Items: []Item{
                            Item{SimpleResponse: SimpleResponse{dialog.MReturn.Message}},
                        },
                        Suggestions: []Suggestion{
                            Suggestion{Title: "Suggestion Chips"},
                            Suggestion{Title: "suggestion 1"},
                            Suggestion{Title: "suggestion 2"},
                        },
                    },
                },
            },
        }

    fmt.Println(in)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...