-1

So I have this struct:

type AllShipments struct {
    Shipments []struct {
        ShipmentID    int       `json:"shipmentId"`
        ShipmentDate  time.Time `json:"shipmentDate"`
        ShipmentItems []struct {
            OrderItemID string `json:"orderItemId"`
            OrderID     string `json:"orderId"`
        } `json:"shipmentItems"`
        Transport struct {
            TransportID int `json:"transportId"`
        } `json:"transport"`
    } `json:"shipments"`
}

I want to get a list containing only the shipmentID values, because I need it to make API calls to get specific details of a single shipment. Then later I can loop over it in my API calls.

In Python I did this:

# Create shipments dataframe
df_shipments = json_normalize(full_df['shipments'], 'shipmentItems', columns)

# Turn into list so we can loop over the shipmentId property in our API call to get more detailed information
ship_list = df_shipments['shipmentId'].tolist()

Very powerfull, with two lines of code. But a lot of magic going on. I want to do the same with Golang. From the docs and some searching I came up with this idea:

var shipmentList []string
for _, v := range t.Shipments {
    shipmentList = append(shipmentList, string(v.ShipmentID))
}

Then the shipmentList should return me an array containing all the shipmentID's. But I get this output: [� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �]

Can someone maybe clarify what is going on and why I get this result? And how to get the expected result I want?

Raf Rasenberg
  • 534
  • 2
  • 14
  • 27

1 Answers1

2

You are appending in the right way. What's wrong is how you are converting integers to strings, you are doing it the Python way. Please take a look at strconv.Itoa for the Go equivalent.

satoru
  • 31,822
  • 31
  • 91
  • 141