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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序