保持在GoClipse上运行ListenAndServe

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2014年8月17日 04:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/25343656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定