这完美地工作并且数据库已连接,没有问题。但是我不想在函数中对我的值进行硬编码,因此我使用的是 env 文件。但是使用 os.getenv 不会连接到数据库。
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "bond"
password = "password"
dbname = "bookstore"
)
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("You connected to your database.")
}
以下代码给出了错误 -panic: pq: password authentication failed for user "bond"
var (
host = "localhost"
port = 5432
user = "bond"
password = os.Getenv("DATABASE_PWD")
dbname = "bookstore"
)
为什么会这样?
慕的地8271018
炎炎设计
相关分类