/ / Go type de structure, remplissage du champ de structure incorporée - go, struct

Allez type struct, remplissez le champ struct intégré - allez, struct

J'ai le type de structure suivant:

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

Maintenant, j'utilise cette structure dans un projet complètement différent, en important le paquet, etc. Jusque là, tout va bien, mais je souhaite ensuite remplir une instance concrète de la structure:

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

Je "remplis" d'abord les champs racine, mais ensuite je dois remplir un tableau de Formats, mais je ne sais pas à quoi ressemble cette syntaxe. Est-ce que quelqu'un peut me diriger dans la bonne direction?

Réponses:

1 pour la réponse № 1

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

Pour ne pas relayer sur les liens uniquement, tout le programme de travail:

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

Mon avis: moche comme l’enfer et sujet aux erreurs, car il est facile de rater une balise ou d’omettre un champ et d’obtenir des erreurs de compilateur qui ne sont pas si utiles. (Ils vous disent que vous ne pouvez pas les utiliser comme valeur de champ ou autre, mais don "t pas analyser quelle est la différence entre le type que vous avez utilisé et le type que vous attendiez, pour vous faciliter la tâche, pour trouver votre omission / faute de frappe.)

Et je redoute de penser à quoi ça ressemble si vous commencez à verser plus d'un élément dans le littéral de tranche.

Et oui, vous pouvez "omettre les tags, car ils participent à l'identité de type, comme indiqué ici Spécification Golang dernier paragraphe avant le dernier exemple de code.

Comme grokifier a souligné, vous n’avez pas besoin de répéter le type struct pour chaque élément slice, donc ça marche aussi:

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

À mon avis, un peu mieux, mais toujours pas bon, car vous avez encore du travail à faire si vous modifiez le type incorporé. (En comparaison du nom du type incorporé).


1 pour la réponse № 2

Nommez tous les types de structure pour éviter de dupliquer le type de structure dans littéraux composites:

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

Voici un exemple de littéral composite en utilisant ces types:

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