1

Although I've been using language for more than a couple of months now, I hadn't seen myself with this need. What I want is to initialize the structure that I show below (basically initialize Project and then, inside, aaa, bbb and ccc structs). My question is whether I can really initialize it without making an unmarshall.

type example struct {
    Name    string `yaml:"name" json:"name"`
    Key     string `yaml:"key" json:"key"`
    Version string `yaml:"version" json:"version"`
    Project []struct {
        Name string `yaml:"name" json:"name"`
        aaa  struct {
            Name    string `yaml:"name" json:"name"`
            Key     string `yaml:"key" json:"key"`
            xxx struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"xxx" json:"xxx"`
            zzz struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"zzz" json:"zzz"`
        } `yaml:"aaa" json:"aaa"`
        bbb struct {
            Name    string `yaml:"name" json:"name"`
            Key     string `yaml:"key" json:"key"`
            xxx struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"xxx" json:"xxx"`
            zzz struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"zzz" json:"zzz"`
        } `yaml:"bbb" json:"bbb"`
        ccc struct {
            Name    string `yaml:"name" json:"name"`
            Key     string `yaml:"key" json:"key"`
            xxx struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"xxx" json:"xxx"`
            zzz struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"zzz" json:"zzz"`
        } `yaml:"ccc" json:"ccc"`
    } `yaml:"project" json:"project"`
}

I know I could do it in some way similar to the following. But I try to know the language better.

type example struct {
    Name    string `yaml:"name" json:"name"`
    Project proyectos }

type proyectos structs{
    [] proyecto
}

type proyecto struct{
    Name string `yaml:"name" json:"name"`
    aaa  struct {
    .....
} `yaml:"project" json:"project"`

I appreciate anyone who can give me a hint about this with an example.

Thank you.

Hugo L.M
  • 1,053
  • 17
  • 31

1 Answers1

2

That actually looks right. I'd prefer to remove the proyectos struct definition as it's unnecessary in my opinion - you can just use []proyecto instead, which looks cleaner.

I hate to be that guy, but your question has already been answered here: https://stackoverflow.com/a/24809404/7471182

marhaupe
  • 267
  • 1
  • 3
  • 10