Golang and JSON with array of struct

The json package can only serialize exported fields of your struct. Change your struct to start all fields with an uppercase letter so they can be included in the output:

type SpanInfo struct {
    Imsi string
    Network string
    Network_status string
    Signal_quality int
    Slot int
    State string
}

Read the documentation of json.Marshal() for details and more information.


Your struct fields must be exported (field is exported if it begins with a capital letter) or they won't be encoded:

Struct values encode as JSON objects. Each exported struct field becomes a member of the object

To get the JSON representation as probably expected change the code to this:

type SpanInfo struct {
    IMSI string `json:"imsi"`
    Network string `json:"network"`
    NetworkStatus string `json:"network_status"`
    SignalQuality int `json:"signal_quality"`
    Slot int `json:slot"`
    State string `json:"state"`
}

type GatewayInfo []SpanInfo

Tags:

Struct

Json

Go