In a hobby project of mine, I have a struct like this:
type Resource struct {
Id int
ParentIds []int
Title string
Contents []byte
Resources []Resource
}
Each resource possibly has some sub-resources ([]Resource). I'd like to get started by using a query-to-struct mapper like gorp, but I don't know how I can map a query like
SELECT r.Id, r.Title, r.Contents
FROM Resources r
LEFT OUTER JOIN Resources sub ON sub.ParentIds @> ARRAY[r.Id]::integer[]
Can anyone create a minimal working example or point me to a relevant documentation? Maybe gorp isn't the right tool for the job? If there is a better alternative, I'm also open to suggestions. Thank you.
There is an example of a join in the gorp readme at https://github.com/go-gorp/gorp . I don't think there is any built-in way of putting separate tables into arrays like you are doing.
Where InvoicePersonView is a struct to hold the results of the query.
// Run your query
query := "select i.Id InvoiceId, p.Id PersonId, i.Memo, p.FName " +
"from invoice_test i, person_test p " +
"where i.PersonId = p.Id"
// pass a slice to Select()
var list []InvoicePersonView
_, err := dbmap.Select(&list, query)
// this should test true
expected := InvoicePersonView{inv1.Id, p1.Id, inv1.Memo, p1.FName}
if reflect.DeepEqual(list[0], expected) {
fmt.Println("Woot! My join worked!")
}