在我的一个业余爱好项目中,我有一个这样的结构:
type Resource struct {
Id int
ParentIds []int
Title string
Contents []byte
Resources []Resource
}每个资源可能有一些子资源([] resource )。我想从使用像gorp这样的查询到结构映射器开始,但是我不知道如何映射像这样的查询
SELECT r.Id, r.Title, r.Contents
FROM Resources r
LEFT OUTER JOIN Resources sub ON sub.ParentIds @> ARRAY[r.Id]::integer[]有没有人可以创建一个最小的工作示例,或者给我一个相关的文档?也许gorp不是适合这项工作的工具?如果有更好的选择,我也乐于接受建议。谢谢。
发布于 2014-07-24 14:56:44
在https://github.com/go-gorp/gorp的gorp自述文件中有一个联接的例子。我认为没有任何内置的方法可以像您这样将单独的表放入数组中。
其中InvoicePersonView是保存查询结果的结构。
// 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!")
}https://stackoverflow.com/questions/23412343
复制相似问题