我从Go http/net编程开始,使用ListenAndServe,我不会得到呈现的模板,因为当我运行main.go文件时,它会打印侦听行并在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上有什么方法可以让程序继续运行吗?或者我在这里遗漏了什么?
任何帮助都是非常感谢的,谢谢。
发布于 2014-08-17 00:00:20
只是移动我的评论,因为我找不到任何其他问题(我知道我回答了上周,但我找不到它)。
Go的第1条规则,总是检查错误。
对于http.ListenAndServe,通常使用fmt.Println(http.ListenAndServe(":3000", nil))或log.Println是常见的做法。
https://stackoverflow.com/questions/25343656
复制相似问题