I have this struct:
type Movies struct {
ID string
Title string
Actors []struct {
Name string
City string
State string
Country string
}
}
And then I have my sql query and logic:
query := `select m.id as id, m.title as title, m.actors as actors from Movies where id = $1`
rows, err := db.Query(query, movieID)
var rowsOut []types.Movies
for rows.Next() {
var movies types.Movies
var actors_json string
scanerr := rows.Scan(
&movies.id,
&movies.title,
&movies.actors_json
)
}
So when I print out fmt.Println(actors_json)
, it prints out the way I want it to, actually exactly the way its stored in the db which is like:
{"actors": [{"name": "x", "city": "Miami", "state": "FL", "country": "US"}]}
but then when I go onto unmarshal it and add it to the struct:
err := json.Unmarshal([]byte(actors_json), &movies)
if err != nil {
fmt.Println("Error unmarshaling JSON:", err)
} else {
fmt.Println("Unmarshalled TSD:", movies.Actors)
}
if scanerr != nil {
return rowsOut, err
}
this fmt.Println("Unmarshalled TSD:", movies.Actors)
prints []
, like its empty?
Why would this happen? Could someone please explain? I’ve been stuck for some time now, so thank you in advance.