go - 在 GoClipse 上保持 ListenAndServe

标签 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 上有什么方法可以让程序保持运行?或者我在这里遗漏了什么?

感谢任何帮助,谢谢。

最佳答案

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

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

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

关于go - 在 GoClipse 上保持 ListenAndServe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25343656/

相关文章:

go - 在 Go 中使用 fork 包导入

go - 使用 Go 的 net/http 包连接到 YouTube API 的问题

eclipse - 如何使用 GoClipse 在 Eclipse Neon 中创建断点?

eclipse - 不要使用 GoClipse 制作二进制文件

eclipse - 如何在 GoClipse 中交叉引用?

ubuntu - goclipse 去 ubuntu 上的二进制位置

go - 为什么这个 golang 函数_not_永远运行?

mongodb - "gcc": executable file not found in %PATH% when using mongo-go-driver

eclipse - 无法安装 GoClipse

go - 多次使用TCP连接返回 'broken pipe'错误