post image :date_long | 1 min Read

Go HandlerFunc() review

package main

import (
	"html/template"
	"net/http"
)

var tpl *template.Template

func init() {
	tpl = template.Must(template.ParseFiles("index.gohtml"))
}

func dogs(w http.ResponseWriter, r *http.Request) {
	// io.WriteString(w, "This is the web about dogs!\n")
	data := `This is the web about dogs!\n`
	w.Header().Set("Content-type", "text/html")
	tpl.ExecuteTemplate(w, "index.gohtml", data)
}

func me(w http.ResponseWriter, r *http.Request) {
	// io.WriteString(w, "This is the web about me!\n")
	data := `This is the web about me!\n`
	tpl.ExecuteTemplate(w, "index.gohtml", data)
}

func about(w http.ResponseWriter, r *http.Request) {
	// io.WriteString(w, "About!\n")
	data := `About me!\n`
	tpl.ExecuteTemplate(w, "index.gohtml", data)
}

func main() {

	// http.HandleFunc("/dogs/", dogs)
	// http.HandleFunc("/me/", me)
	// http.HandleFunc("/", about)
	http.Handle("/dogs/", http.HandlerFunc(dogs))
	http.Handle("/me/", http.HandlerFunc(me))
	http.Handle("/", http.HandlerFunc(about))


	http.ListenAndServe(":8080", nil)

}

author image

Jan Toth

I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …

comments powered by Disqus