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.
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
.