go - 具有许多嵌套的结构填充错误

标签 go

我不明白错误在哪里,因为在完成结构后,在我看来 undefined: Payload

这是一个非常烦人的结构,因为它有足够多的结构嵌套和结构 slice

你能帮我解决这个问题吗,因为我无法解决它?

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

最佳答案

你的内部 struct 没有在任何地方声明——它们都是匿名类型。要按名称实际显式实例化它们,它们需要存在于某处 ( playground ):

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

关于go - 具有许多嵌套的结构填充错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54698537/

相关文章:

mongodb - 如何insertOne到mongo数据库

go - 如何从json解析非标准时间格式

arrays - 如何在循环中使用golang的追加到数组中

go - 如何在go中获得文件的独占锁

go - 在 Go 中使用自定义结构作为另一个结构中的类型而不是构建

rest - Swag init 只生成 "general API information"

json - 在 Go 中遍历 json 数组以提取值

go - 从函数约定 Golang 返回地址

mysql - 如何解决mysqldump Got error : 1044: "Access denied for user ' ' @'localhost' to database 'db_name' "when selecting the database

c - 这是什么?获取proccount