如何在golang中的嵌套结构上循环?

I have a nested struct and I want to loop over this struct. Please help me how to loop over the struct to get the output in this format. I have mentioned the struct which I am using.

Expected Output:

    {  
    "PrefcatID":"PREF_001"
    "prefname: :"PREF_name"
    "PrefSubcategory":
        {
        "subcatid":"SUB_PREF_001",
        "PrefcatID":"PREF_001",
        "subcatname":"Sub Category Name 1"
     },
     {  
        "subcatid":"SUB_PREF_002",
        "PrefcatID":"PREF_001",
        "subcatname":"Sub Category Name 2"
     }
     }

Struct in Go:

    type PrefCategory struct {
    PrefcatID           string `json:"PrefcatID"`
    PrefName            string `json:"prefname"`
    Temp_PrefSubcategory []PrefSubcategory `json:"prefSubcategory "`
    }

    type PrefSubcategory struct {
    PrefcatID        string `json:"PrefcatID"`
    SubcatId         string `json:"subcatid"`
    SubCatName       string `json:"subcatname"`
     }

You're almost there but note that there are some syntax errors in your JSON example. The structs are almost there too, ultimately, you just need to build an instance of PrefCategory with the expected values and marshal it to JSON:

type PrefCategory struct {
  PrefcatID            string            `json:"PrefcatID"`
  PrefName             string            `json:"prefname"`
  Temp_PrefSubcategory []PrefSubcategory `json:"prefSubcategory"`
}

type PrefSubcategory struct {
  PrefcatID  string `json:"PrefcatID"`
  SubcatId   string `json:"subcatid"`
  SubCatName string `json:"subcatname"`
}

func main() {
  pref := PrefCategory{
    "PREF_001",
    "PREF_name",
    []PrefSubcategory{
      {"SUB_PREF_001", "PREF_001", "Subcategory Name 1"},
      {"SUB_PREF_002", "PREF_001", "Subcategory Name 2"},
    },
  }

  jsonbytes, err := json.MarshalIndent(&pref, "", "  ")
  if err != nil {
    panic(err)
  }
  fmt.Println(string(jsonbytes))
  /*
  {
    "PrefcatID": "PREF_001",
    "prefname": "PREF_name",
    "prefSubcategory": [
      {
        "PrefcatID": "SUB_PREF_001",
        "subcatid": "PREF_001",
        "subcatname": "Subcategory Name 1"
      },
      {
        "PrefcatID": "SUB_PREF_002",
        "subcatid": "PREF_001",
        "subcatname": "Subcategory Name 2"
      }
    ]
  }
  */
}