去结构类型,填充嵌入式结构域

时间:2017-12-16 08:41:12

标签: go struct

我有以下结构类型:

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"`
}

现在我在一个完全不同的项目中使用这个结构,通过导入包等等。到目前为止,这很好,但后来我想填充结构的具体实例:

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

我先#34;填写"根域,但是我需要填充一个Formats数组,但我不知道这个语法是怎么样的。有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

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

不仅仅在链接上转发,整个工作程序:

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)
}

我的观点:因为地狱和容易出错而难看,因为很容易错过标记或省略字段并得到编译错误,这些错误并非有用。 (他们告诉你,你不能将它用作字段值或其他类似的东西,但不要分析你使用的类型和预期的类型之间的差异,以使你更容易,找到你的遗漏/错字。)

我害怕思考,如果你开始倒更多,看起来如何 而不是切片文字中的一个元素。

是的,你可以省略标签,因为它们参与了类型标识,如最后一个代码示例之前的golang spec最后一段所述。

正如grokify指出的那样,你不需要为每个切片元素重复结构类型,所以这也有效:

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)
}

在我看来稍好一点,但仍然不好,因为如果你改变嵌入式类型,你还有很多工作要做。 (比较命名嵌入式类型)。

答案 1 :(得分:1)

命名所有结构类型以避免在composite literals中重复结构类型:

type Format 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"`
}

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    []Format `json:"formats"`
}

以下是使用这些类型composite literal的示例:

job := ExportJob{
    AssetID:    "blablat",
    ObjectType: "blablab",
    Reference:  "blbla",
    Ordering:   0,
    Chunk:      "blablba",
    Formats: []Format{
        {SizePreset: "no",
            Fit:             "blah",
            OutputFormat:    "blub",
            Location:        "loc",
            BackgroundColor: "green",
            Height:          1,
            Width:           2,
            Trimfactor:      4,
            Quality:         7,
        },
    },
}
相关问题