英文:
connecting postgresql to go
问题
package main
import (
"database/sql"
"fmt"
"html/template"
"net/http"
"unicode"
_ "github.com/lib/pq"
"golang.org/x/crypto/bcrypt"
)
/* const (
host = "localhost"
port = 5432
user = "postgres"
password = "*******"
dbname = "db"
) */
var tpl *template.Template
var db *sql.DB
func main() {
tpl, _ = template.ParseGlob("templates/*.html")
var err error
db, err = sql.Open("postgres", "root:password@tcp(localhost:localhost/db")
if err != nil {
panic(err.Error())
}
defer db.Close()
http.HandleFunc("/register", registerHandler)
http.HandleFunc("/registerauth", registerAuthHandler)
fmt.Println("Listening")
http.ListenAndServe("localhost:8080", nil)
}
当我运行这段代码时,我遇到了一个错误:
panic: sql: unknown driver "postgresql" (forgotten import?)
顺便说一下,我只是在按照在线课程的指导进行操作,但他们使用的是MySQL,而我使用的是Postgres,我正在为我的论文做这个。
英文:
package main
import (
"database/sql"
"fmt"
"html/template"
"net/http"
"unicode"
_ "github.com/lib/pq"
"golang.org/x/crypto/bcrypt"
)
/* const (
host = "localhost"
port = 5432
user = "postgres"
password = "*******"
dbname = "db"
) */
var tpl *template.Template
var db *sql.DB
func main() {
tpl, _ = template.ParseGlob("templates/*.html")
var err error
db, err = sql.Open("postgresql", "root:password@tcp(localhost:localhost/db")
if err != nil {
panic(err.Error())
}
defer db.Close()
http.HandleFunc("/register", registerHandler)
http.HandleFunc("/registerauth", registerAuthHandler)
fmt.Println("Listening")
http.ListenAndServe("localhost:8080", nil)
}
When I run this I get an error:
> panic: sql: unknown driver "postgresql" (forgotten import?)
btw, I'm just following a lesson online but they are using MySQL while I on the other hand use Postgres and I'm doing this for my thesis
答案1
得分: 1
你应该使用 "postgres" 数据库驱动字符串,而不是 "postgresql"。
英文:
You should use the "postgres" database driver string, not "postgresql".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论