我开始使用 Go http/net 编程,使用 ListenAndServe 时我没有得到呈现的模板,因为当我运行 main.go 文件时,它会打印 Listening 行并在 Eclipse 上以状态 0 退出,这是代码:
package main
import (
"fmt"
"html/template"
"net/http"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/index.html")
if err != nil {
fmt.Fprintf(w, err.Error())
}
t.ExecuteTemplate(w, "index", nil)
}
func main() {
fmt.Println("Listening on port: 3000")
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":3000", nil)
}
在 GoClipse 上有什么方法可以让程序保持运行?或者我在这里遗漏了什么?
感谢任何帮助,谢谢。
最佳答案
只是移动我的评论,因为我找不到任何其他问题(我知道我上周回答了这个问题,但我找不到)。
Go 规则 #1,始终检查错误。
对于 http.ListenAndServe
通常使用 fmt.Println(http.ListenAndServe(":3000", nil))
或 log.Println
。
关于go - 在 GoClipse 上保持 ListenAndServe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25343656/