dongwo2772 2018-03-25 18:37
浏览 527
已采纳

Go中的嵌套数组结构

I have started using https://mholt.github.io/json-to-go/ to turn API JSON into go structs and I really like it, however I am stuck on how to initialize the Filters array struct in the Report Definition struct shown below.

type ReportDefinition struct {
    ReportName string `json:"reportName"`
    ReportType string `json:"reportType"`
    Product    string `json:"product"`
    Email      string `json:"email"`
    CreatedBy  string `json:"createdBy"`
    Duration   struct {
        Duration  string      `json:"duration"`
        StartDate interface{} `json:"startDate"`
        EndDate   interface{} `json:"endDate"`
    } `json:"duration"`
    Filters []struct {
        Column struct {
            ColumnName string `json:"columnName"`
            Value      string `json:"value"`
        } `json:"column"`
        Operator string `json:"operator"`
    } `json:"filters"`
    SortBy            interface{}   `json:"sortBy"`
    ReportGroup       interface{}   `json:"reportGroup"`
    ReportOnHierarchy bool          `json:"reportOnHierarchy"`
    IsReportPreview   string        `json:"isReportPreview"`
    OutputRecordCount interface{}   `json:"outputRecordCount"`
    Schedule          interface{}   `json:"schedule"`
    Columns           []interface{} `json:"columns"`
}

I cannot seem to reference the items declared within the Filters struct or even the Filters struct in order to create a new Filter item and append it to Filters.

Is it possible to reference the Column struct with the ReportDefinition written as is? Or am I am doing something silly and I should just declare Filters outside of the ReportDefinition structure?

  • 写回答

1条回答 默认 最新

  • dshmvqnl98119 2018-03-26 07:58
    关注

    Since the sliced type in the Filters field doesn't have a name, you have to repeat the definition when you want to initialize it:

    package main
    
    import "fmt"
    
    type ReportDefinition struct {
        Filters []struct {
                Column struct {
                        ColumnName string `json:"columnName"`
                        Value      string `json:"value"`
                } `json:"column"`
                Operator string `json:"operator"`
        } `json:"filters"`
    }
    
    func main() {
        var rd ReportDefinition
        rd.Filters = append(rd.Filters, struct {
                Column struct {
                        ColumnName string `json:"columnName"`
                        Value      string `json:"value"`
                } `json:"column"`
                Operator string `json:"operator"`
        }{
                Column: struct {
                        ColumnName string `json:"columnName"`
                        Value      string `json:"value"`
                }{
                        ColumnName: "foo",
                        Value:      "bar",
                },
                Operator: "==",
        })
    
        fmt.Printf("+%v
    ", rd)
    }
    

    It should be clear that this is very unconvenient, so you should just give your types names (at least those that you want to initialize):

    package main
    
    import "fmt"
    
    type ReportDefinition struct {
        Filters []Filter `json:"filters"`
    }
    
    type Filter struct {
        Column   Column `json:"column"`
        Operator string `json:"operator"`
    }
    
    func NewFilter(col, op, val string) Filter {
        return Filter{
                Column: Column{
                        ColumnName: col,
                        Value:      val,
                },
                Operator: op,
        }
    }
    
    type Column struct {
        ColumnName string `json:"columnName"`
        Value      string `json:"value"`
    }
    
    func main() {
        var rd ReportDefinition
        rd.Filters = append(rd.Filters, NewFilter("foo", "==", "bar"))
    
        fmt.Printf("+%v
    ", rd)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用