loop over a array of struts in golang


My terminology maybe off so I’m using some python terms.

What trying to do in golang is iterate over a strut that is has a stored value like this (which is what I get back from the api)

[{STOREA 0 0 0} {STOREB 0 0 0} {STOREC 0 0 0} {STORED 0 0 0}]

In python I would call this a list of dicts.

When I hover over the value in visual code it states this:

field itemData []struct{Code string "json:"Code""; Items int
"json:"Items""; Price float64 "json:"Price""; Qty int

package main

// I think this is the right way to interpret what visual code stated
type itemData struct {
    Code string  `json:"Code"`
    Items int     `json:"Items"`
    Price   float64 `json:"Price"`
    Qty int     `json:"Qty"`

//I'm trying to simulate the api response by storing it in a varible like this but I think the [] are an issue and im doing it incorrectly
var storeData = itemData[{STOREA 0 0 0} {STOREB0 0 0} {STOREC 0 0 0} {STORED0 0 0}] // I think the [ ] brackets are causing an issue

//The idea is I can iterate over it like this:
for k,v := range storeData {


Recommended to give A Tour of Go – Slice

Then Try this:

    var storeData = []itemData{{"STOREA", 0, 0, 0}, {"STOREB", 0, 0, 0}, {"STOREC", 0, 0, 0}, {"STORED", 0, 0, 0}}
    for k, v := range storeData {
        fmt.Println(k, v)


Answered By – Kamol Hasan

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.