我正在用Go 1.11和postgres弄湿自己的脚。
package db
import (
"database/sql"
_ "github.com/lib/pq" //database connector
g "app/globalvariables"
)
func CreateTable(tid int) {
s := "CREATE TABLE someschema.sometable" + string(tid) + "(id serial PRIMARY KEY,...);"
db, _ := sql.Open("postgres", "user="+g.DB_USER+" dbname="+g.DB_NAME+" sslmode=disable")
defer db.Close()
q, _ := db.Prepare(s)
q.Exec()
}
在测试功能时,我得到:
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x51d46a]
那会是什么?
答案 0 :(得分:1)
我很费力地用“ string”将整数转换为string。我必须使用strconv.Iota。