Answer the question
In order to leave comments, you need to log in
Why does a Go app crash on VPS but work locally?
Tell me, I made a simple go application, several html templates and data output from mongodb.
Brief content of Main:
func main() {
session, err := mgo.Dial("mongodb://127.0.0.1")
if err != nil {
fmt.Println(err)
}
defer session.Close()
http.Handle("/layout/", http.StripPrefix("/layout/", http.FileServer(http.Dir("templates/layout"))))
http.HandleFunc("/login", loginHandler)
....еще handlerы...
err1 := http.ListenAndServe(port, nil)
if err1 != nil { log.Fatal("ListenAndServe: ", err1) }
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("templates/login.html", "templates/header.html", "templates/footer.html")
t.ExecuteTemplate(w, "login", nil)
}
}
2018/09/10 17:39:47 http: panic serving 85.172.11.140:51035: runtime error: invalid memory address or nil pointer dereference
goroutine 93 [running]:
net/http.(*conn).serve.func1(0xc4202723c0)
/usr/local/go/src/net/http/server.go:1726 +0xd0
panic(0x78ea60, 0xa08ad0)
/usr/local/go/src/runtime/panic.go:502 +0x229
html/template.(*Template).lookupAndEscapeTemplate(0x0, 0x7fc464, 0x5, 0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:144 +0x43
html/template.(*Template).ExecuteTemplate(0x0, 0x849120, 0xc420288380, 0x7fc464, 0x5, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:133 +0x43
main.loginHandler(0x84c160, 0xc420288380, 0xc4202ae300)
/Users/evgeniy/go/src/zdrav_1/logic.go:128 +0x5b5
net/http.HandlerFunc.ServeHTTP(0x8197b0, 0x84c160, 0xc420288380, 0xc4202ae300)
/usr/local/go/src/net/http/server.go:1947 +0x44
net/http.(*ServeMux).ServeHTTP(0xa180a0, 0x84c160, 0xc420288380, 0xc4202ae300)
/usr/local/go/src/net/http/server.go:2337 +0x130
net/http.serverHandler.ServeHTTP(0xc42005b040, 0x84c160, 0xc420288380, 0xc4202ae300)
/usr/local/go/src/net/http/server.go:2694 +0xbc
net/http.(*conn).serve(0xc4202723c0, 0x84c660, 0xc420286440)
/usr/local/go/src/net/http/server.go:1830 +0x651
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2795 +0x27b
Answer the question
In order to leave comments, you need to log in
session, _ := mgo.Dial("mongodb://127.0.0.1")
How so, do not check the error!
Most likely, your session after this line is nil, which is what causes the panic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question