簡體   English   中英

轉到結構類型,填充嵌入式結構字段

[英]Go struct type, fill embedded struct field

我有以下結構類型:

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

我首先“填充”了根字段,但是隨后我需要填充一個Formats數組,但是我不知道這種語法的樣子。 有人可以指出我正確的方向嗎?

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指出的那樣,您無需為每個slice元素重復struct類型,因此也可以正常工作:

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

我認為稍微好一點,但仍然不好,因為如果您更改嵌入式類型,您還有更多工作要做。 (與命名嵌入式類型相比)。

命名所有結構類型,以避免在復合文字中復制結構類型:

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

這是使用這些類型的復合文字的示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM