duanlei2458 2019-02-14 20:25
浏览 28
已采纳

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

I am not understanding where the error is, since after completing the struct, it appears to me undefined: Payload

This is a very annoying struct because it has enough nesting of structs and slices of structs

Could you help me solve this problem, because I can not solve it?

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条回答 默认 最新

  • dongpu3347 2019-02-14 20:45
    关注

    Your internal structs aren’t declared anywhere - they’re all anonymous types. To actually explicitly instantiate them by name, they need to exist somewhere (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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置