将ListenAndServe保持在GoClipse上

时间:2014-08-16 20:27:23

标签: go 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有什么方法可以让程序保持正常状态?或者我在这里找不到什么东西?

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

因为我找不到任何其他问题(我知道我上周回答了这个问题,但我找不到它),只是提出我的评论。

Go的规则#1,始终检查错误。

对于http.ListenAndServe,通常使用fmt.Println(http.ListenAndServe(":3000", nil))log.Println