I'm having trouble setting up a Query on 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/cidades.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 it is returning a err
in Query, but as I am not dealing with it is giving this error. What I do not understand is because it returns error in the query? From what I can understand, it's all right.
RESOLVED
You can not use QueryBuilder with Postgres, it has to be Raw