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

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
    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)
    }
    
    点赞 评论

相关推荐