post image :date_long | 1 min Read

Go serving files hands on 1

package main

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

func main() {
	http.HandleFunc("/", foo)
	http.HandleFunc("/dog/", dog)
	http.HandleFunc("/dog.jpg", chien)

	log.Fatalln(http.ListenAndServe(":8080", nil))

}

func foo(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "foo.run")
}

func dog(w http.ResponseWriter, r *http.Request) {
	tpl, err := template.ParseFiles("dog.gohtml")
	if err != nil {
		log.Fatalln(err)
	}
	tpl.ExecuteTemplate(w, "dog.gohtml", nil)
}

func chien(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "tesla.jpg")
}

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