登录
首页 >  Golang >  Go问答

多次嵌套的结构填充错误

来源:stackoverflow

时间:2024-03-17 12:42:27 194浏览 收藏

**文章首段摘要** 嵌套结构的填充有时会出现错误,提示“undefined: payload”。这是因为内部结构没有明确声明为命名的类型。为了解决这个问题,需要在代码中定义这些结构,并为每个字段指定一个名称。通过将匿名类型显式实例化为命名的类型,可以正确地填充嵌套结构并避免错误。

问题内容

我不明白错误在哪里,因为完成结构后,在我看来 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)
}

解决方案


您的内部 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)
}

理论要掌握,实操不能落!以上关于《多次嵌套的结构填充错误》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>