I'm trying to convert a []*Struct
to a JSON response, I'm able to get the data from the []*Struct
and iterate over it. The part I get stuck around is the iterating over a loop then returning the data to an interface{}
I've tried to putting an interface in an interface, but I just can't make ends meet. Here's what I have at the moment, I was told to turn to StackOverflow if I had any issues.
package main
import (
"log"
"context"
"cloud.google.com/go/datastore"
)
var ctx = context.Background()
type ItemList struct {
Id string `datastore:"_id"`
Name string `datastore:"name"`
}
type Data struct {
ManyItems []Item
}
type Item struct {
Id string `json:"id"`
Name string `json:"name"`
}
func Get() ([]*ItemList, error) {
client, err := datastore.NewClient(ctx, "project-name")
if err != nil {
log.Println(err)
}
var Response []*ItemList
query := datastore.NewQuery("query")
_, err = client.GetAll(ctx, query, &Response)
if err != nil {
log.Println(err)
}
return Response, error
}
func Read() (interface{}) {
resp, err := Get()
if err != nil {
log.Println(err)
}
for i, _ := range resp {
// this is where i get stuck
r := &Data{
ManyItems: Item{
Id: resp[i].Id,
Name: resp[i].Name,
},
}
return r
}
}
Thank you to anyone who reads this or can provide me some sort of guidance, I really appreciate it.