为什么db变量在函数nil中getBooks?
package main
import (
...
)
var db *sql.DB
func init() {
gotenv.Load()
}
func main() {
db, err := sql.Open("postgres", os.Getenv("ELEPHANTSQL_URL"))
err = db.Ping()
fmt.Println(db, err)
router := mux.NewRouter()
router.HandleFunc("/books", getBooks).Methods("GET")
log.Fatal(http.ListenAndServe("localhost:8000", router))
}
func getBooks(w http.ResponseWriter, r *http.Request) {
if db == nil {
log.Print("!!!!!!!!!!")
os.Exit(2)
}
}
繁星coding
相关分类