looping over struct and accessing array in golang


My issue is

  1. I want to be able to loop over each server and within that loop, loop over UsageData
  2. My current loop gives me an error about ranging over assets not sure why
  3. I cant access assets.Server.UsageData

Here is my code : https://go.dev/play/p/ttNVW5_Q4Ys

package main

import (

type Asset struct {
    Server struct {
        Host string `json:"host"`
        Port string `json:"port"`
    } `json:"server"`
    Postgres struct {
        Host      string `json:"host"`
        User      string `json:"user"`
        Password  string `json:"password"`
        DB        string `json:"db"`
        UsageData []struct {
            Region string `json:"Region"`
            Mbps   int    `json:"Mpbs"`
        } `json:"UsageData"`
    } `json:"database"`

func main() {
    jsonConfig := []byte(`[
            "UsageData":[{"Region":"US","Mbps":100}, {"Region":"EU","Mbps":140}]
            "UsageData":[{"Region":"US","Mbps":58}, {"Region":"EU","Mbps":250}]
    var assets []Asset
    err := json.Unmarshal(jsonConfig, &assets)
    if err != nil {
    fmt.Printf("Assets: %+v\n", assets)
    //fmt.Printf("Config: %+v\n", assets.Server.Host)
    //fmt.Printf("Config: %+v\n", assets.database.UsageData)
    //fmt.Printf("Config: %+v\n", assets.Server.UsageData)

    for _, asset := range assets {
        fmt.Printf("%v\n", asset)
        //for _, Usage := range assets.UsageData {
        //  fmt.Printf("%v\n",Usage)

** Code with the correct answer, I was calling the nested struct incorrectly**



Provided jsonConfig is not technically incorrect but keys SHOULD be unique (see question).
It seems that encoding/json will override value with last occurrence.

So you have 2 options:

  1. change jsonConfig to [{asset1}, {asset2}] (see fixed playground)
  2. implement custom unmarshaler

I definitely recommend first option.

Answered By – medasx

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.