/ / Go構造体タイプ、埋め込み構造体フィールドの入力-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??
}

最初にルートフィールドに「入力」しますが、次に配列を入力する必要があります Formats、しかし、この構文がどのように見えるかわかりません。誰かが私を正しい方向に向けることができますか?

回答:

回答№1は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)
}

私の意見: タグを見逃したり、フィールドを省略したり、コンパイラーエラーを取得したりするのは簡単です。 「使用したタイプと予想されたタイプの違いを分析して、あなたの脱落/タイプミスを見つけやすくします。)

そして、私はあなたがより多くを注ぎ始めた場合、それがどのように見えるかについて考えることを恐れます スライスリテラルの複数の要素。

ここで述べたように、タイプアイデンティティに参加しているため、タグを省略することはできません ゴーラン仕様 最後のコード例の前の最後の段落。

として 曲がる スライス要素ごとに構造体型を繰り返す必要はないため、これも同様に機能します。

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

私の意見では、埋め込み型を変更する場合にはまだやるべきことがあるため、やや良いですが、まだ良くありません。 (埋め込み型の命名と比較して)。


回答№2の場合は1

すべての構造体タイプに名前を付けて、構造体タイプが重複しないようにします 複合リテラル

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