英文:
Keeping ListenAndServe up on GoClipse
问题
我正在学习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中有没有办法保持程序运行?还是我在这里漏掉了什么?
感谢任何帮助。
英文:
I'm beginning with Go http/net programming, and with ListenAndServe i'm not getting a template rendered because when i run the main.go file it prints the Listening line and exits with state 0 on Eclipse, this is the code:
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)
}
There's any way on GoClipse to keep the program up?, or there's something i'm missing here?
Any help is appreciated, thanks.
答案1
得分: 0
只是移动我的评论,因为我找不到其他问题的任何内容(我知道我上周回答过这个问题,但我找不到它)。
Go的第一条规则是,始终检查错误。
对于http.ListenAndServe
,通常惯例是使用fmt.Println(http.ListenAndServe(":3000", nil))
或log.Println
。
英文:
Just moving my comment since I couldn't find any of the other questions (I know I answered this last week but I can't find it).
Rule #1 of Go, always check for errors.
For http.ListenAndServe
usually it's common practice to use fmt.Println(http.ListenAndServe(":3000", nil))
or log.Println
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论