dtja73027 2017-12-16 08:41
浏览 89

转到结构类型,填充嵌入式结构字段

I have the following struct type:

type ExportJob struct {
    AssetID    string `json:"asset_id"`
    ObjectType string `json:"object_type"`
    Usage      string `json:"usage"`
    Reference  string `json:"reference"`
    Chunk      string `json:"chunk_id"`
    Ordering   uint64 `json:"ordering"`
    Formats    []struct {
        SizePreset      string `json:"size_preset"`
        Fit             string `json:"fit"`
        OutputFormat    string `json:"output_format"`
        Location        string `json:"location"`
        BackgroundColor string `json:"background_color"`
        Height          uint64 `json:"height"`
        Width           uint64 `json:"width"`
        Trimfactor      uint64 `json:"trimfactor"`
        Quality         uint64 `json:"quality"`
    } `json:"formats"`
}

Now I use this struct in a completely different project, by importing the package, etc. So far so good, but then I want to fill a concrete instance of the struct:

job := workers.ExportJob{
    AssetID:    "blablat",
    ObjectType: "blablab",
    Reference:  "blbla",
    Ordering:   0,
    Chunk:      "blablba,
    Formats:    ?????, // how does this syntax looks like??
}

I first "fill" the root fields, but then I need to fill an array of Formats, but I don't know how this syntax looks like. Can someone point me in the right direction?

  • 写回答

2条回答 默认 最新

  • doudu22272099831 2017-12-16 09:06
    关注

    https://play.golang.org/p/D17WYx6mRr

    To not relay on links only, whole working program:

    package main
    
    import (
        "fmt"
    )
    
    type ExportJob struct {
        AssetID    string `json:"asset_id"`
        ObjectType string `json:"object_type"`
        Usage      string `json:"usage"`
        Reference  string `json:"reference"`
        Chunk      string `json:"chunk_id"`
        Ordering   uint64 `json:"ordering"`
        Formats    []struct {
            SizePreset      string `json:"size_preset"`
            Fit             string `json:"fit"`
            OutputFormat    string `json:"output_format"`
            Location        string `json:"location"`
            BackgroundColor string `json:"background_color"`
            Height          uint64 `json:"height"`
            Width           uint64 `json:"width"`
            Trimfactor      uint64 `json:"trimfactor"`
            Quality         uint64 `json:"quality"`
        } `json:"formats"`
    }
    
    func main() {
        job := ExportJob{
            AssetID:    "blablat",
            ObjectType: "blablab",
            Reference:  "blbla",
            Ordering:   0,
            Chunk:      "blablba",
            Formats: []struct {
                SizePreset      string `json:"size_preset"`
                Fit             string `json:"fit"`
                OutputFormat    string `json:"output_format"`
                Location        string `json:"location"`
                BackgroundColor string `json:"background_color"`
                Height          uint64 `json:"height"`
                Width           uint64 `json:"width"`
                Trimfactor      uint64 `json:"trimfactor"`
                Quality         uint64 `json:"quality"`
            }{struct {
                SizePreset      string `json:"size_preset"`
                Fit             string `json:"fit"`
                OutputFormat    string `json:"output_format"`
                Location        string `json:"location"`
                BackgroundColor string `json:"background_color"`
                Height          uint64 `json:"height"`
                Width           uint64 `json:"width"`
                Trimfactor      uint64 `json:"trimfactor"`
                Quality         uint64 `json:"quality"`
            }{SizePreset: "no",
                Fit:             "blah",
                OutputFormat:    "blub",
                Location:        "loc",
                BackgroundColor: "green",
                Height:          1,
                Width:           2,
                Trimfactor:      4,
                Quality:         7,
            },
            },
        }
        fmt.Println(job)
    }
    

    My opinion: ugly as hell and error prone, as it is easy to miss a tag or omit a field and get compiler errors which aren't that helpful. (They tell you, you can't use that as field value or some such, but don't analyse whats the difference between the type you used and the type that was expected, to make it easier for you, to find your omission/typo.)

    And I dread to think about, how that looks if you start to pour more than one element in the slice literal.

    And yes, you can't omit the tags, as they take part in type identity, as stated here golang spec last paragraph before the last code example.

    As grokify pointed out, you don't need to repeat the struct type for each slice element, so this works as well:

    package main
    
    import (
        "fmt"
    )
    
    type ExportJob struct {
        AssetID    string `json:"asset_id"`
        ObjectType string `json:"object_type"`
        Usage      string `json:"usage"`
        Reference  string `json:"reference"`
        Chunk      string `json:"chunk_id"`
        Ordering   uint64 `json:"ordering"`
        Formats    []struct {
            SizePreset      string `json:"size_preset"`
            Fit             string `json:"fit"`
            OutputFormat    string `json:"output_format"`
            Location        string `json:"location"`
            BackgroundColor string `json:"background_color"`
            Height          uint64 `json:"height"`
            Width           uint64 `json:"width"`
            Trimfactor      uint64 `json:"trimfactor"`
            Quality         uint64 `json:"quality"`
        } `json:"formats"`
    }
    
    func main() {
        job := ExportJob{
            AssetID:    "blablat",
            ObjectType: "blablab",
            Reference:  "blbla",
            Ordering:   0,
            Chunk:      "blablba",
            Formats: []struct {
                SizePreset      string `json:"size_preset"`
                Fit             string `json:"fit"`
                OutputFormat    string `json:"output_format"`
                Location        string `json:"location"`
                BackgroundColor string `json:"background_color"`
                Height          uint64 `json:"height"`
                Width           uint64 `json:"width"`
                Trimfactor      uint64 `json:"trimfactor"`
                Quality         uint64 `json:"quality"`
            }{{SizePreset: "no",
                Fit:             "blah",
                OutputFormat:    "blub",
                Location:        "loc",
                BackgroundColor: "green",
                Height:          1,
                Width:           2,
                Trimfactor:      4,
                Quality:         7,
            },
            },
        }
        fmt.Println(job)
    }
    

    In my opinion slightly better, but still not good, as you still have more work to do if you alter the embedded type. (In comparsion to naming the embedded type).

    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢