K
K
keddad2020-08-26 16:35:03
go
keddad, 2020-08-26 16:35:03

How to parse an array of arrays of structures in Golang?

I have a structure like this:

type PhotoFaceResp struct {
  Traits map[string][]string `json:"traits"`
  Probability float32 `json:"probability"`
}


I am making a request to some server, the response is JSON from arrays of arrays of such structures. I am trying to serialize it like this:
coreResp := make([][]PhotoFaceResp, 0, 0)

  err := json.Unmarshal(body, &coreResp)

  if err != nil {
    panic(err)
  }


The problem is that when I try to iterate over coreResp afterwards, GoLand tells me that my [][]PhotoFaceResp iterates over ints when iterating over it.

Those:

for face := range coreResp{
    // Тут face - int, а не []coreResp
}


What am I doing wrong and how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2020-08-26
@keddad

Do it right like this:

for _, face := range coreResp{
    ...
}

An array iterator returns two values. The index of the element and the element itself. You take the index, because you have it int.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question