-2

I'm learning about the json package in go, so I go to the package documentation and found this example:

import (
    "encoding/json"
    "fmt"
    "os"
)

func main() {
    type ColorGroup struct {
        ID     int
        Name   string
        Colors []string
    }
    group := ColorGroup{
        ID:     1,
        Name:   "Reds",
        Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
    }
    b, err := json.Marshal(group)
    if err != nil {
        fmt.Println("error:", err)
    }
    os.Stdout.Write(b)
}

I try to modify the example:

import (
    "encoding/json"
    "fmt"
    "os"
)

func main() {
    type animal struct {
        id      int
        name    string
        age     int
        sisters []string
    }

    a := animal{1, "max", 10, []string{"emily", "giova"}}

    b, err := json.Marshal(a)
    if err != nil {
        fmt.Println("error:", err)
    }

    os.Stdout.Write(b) // out: {}
}

But the last function return a empty curly brackets. I can't find what's wrong.

expected to return the json of variable b

0 Answers0