0
I’m having problems to mount a Query in Go, using the Beego framework.
Error:
2018/12/20 09:21:32.104 [C] [asm_amd64.s:522] Handler Crashed with error Runtime error: invalid memory address or nil Pointer dereference 2018/12/20 09:21:32.104 [C] [asm_amd64.s:522] C:/Go/src/Runtime/asm_amd64.s:522 2018/12/20 09:21:32.104 [C] [asm_amd64.s:522] C:/Go/src/Runtime/Panic.go:513 2018/12/20 09:21:32.104 [C] [asm_amd64.s:522] C:/Go/src/Runtime/Panic.go:82 2018/12/20 09:21:32.104 [C] [asm_amd64.s:522] C:/Go/src/Runtime/signal_windows.go:204 2018/12/20 09:21:32.105 [C] [asm_amd64.s:522] C:/Users/Joao/go/src/hello/controllers/cities.go:35 2018/12/20 09:21:32.109 [C] [asm_amd64.s:522] C:/Users/Joao/go/src/github.com/astaxie/beego/router.go:834 2018/12/20 09:21:32.110 [C] [asm_amd64.s:522] C:/Go/src/net/http/server.go:2741 2018/12/20 09:21:32.110 [C] [asm_amd64.s:522] C:/Go/src/net/http/server.go:1847 2018/12/20 09:21:32.111 [C] [asm_amd64.s:522] C:/Go/src/Runtime/asm_amd64.s:1333 2018/12/20 09:21:32.111 [server.go:2977] [HTTP] http: Multiple Response.Writeheader calls
Controller:
package controllers
import (
"hello/models"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/lib/pq"
)
type CidadesController struct {
beego.Controller
}
func init() {
beego.BConfig.WebConfig.AutoRender = false
orm.RegisterDriver("postgres", orm.DRPostgres)
orm.RegisterDataBase("default", "User", "postgres:senha@/nomeDoDB?
charset=utf8", 30)
}
func (this *CidadesController) Get() {
var cidade []models.Cidade
cidades, err := orm.NewQueryBuilder("postgres")
if err != nil {
//
}
cidades.Select("cid_codigo",
"cid_nome").
From("ger_cidade").
Limit(10).
Offset(0)
sql := cidades.String()
o := orm.NewOrm()
o.Raw(sql, 20).QueryRows(&cidade)
}
Model:
package models
import (
"github.com/astaxie/beego/orm"
)
type Cidade struct {
Id int `db:"cid_codigo"`
Name string `db:"cid_nome"`
}
func init() {
orm.RegisterModel(new(Cidade))
}
From what I understand is coming back a err
in Query, but as I am not treating is giving this error. What I do not understand is why return error in query? From what I understand it’s all right.
RESOLVED
You can’t use Querybuilder with Postgres, it has to be on Raw
Put the solution you found as an answer, or else I’m mistaken you have the option to answer your own question in this way you make it easy for other people to find the solution.
– Thiago Drulla
Show, now I answer
– João.Mistura