Go serving files hands on 1
Go serving files hands on 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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")
}
This post is licensed under CC BY 4.0 by the author.
