Post

Go serve files

How to serve static files in Go using http.ServeFile, io.Copy, and http.ServeContent, with examples for serving images from the local filesystem and the internet.

This example demonstrates three different ways to serve files in Go: using io.Copy, http.ServeContent, and http.ServeFile. The server registers handlers for the root path (which renders an HTML image tag), a direct image route, and a route that serves an image referenced from the internet.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import (
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	http.HandleFunc("/frominternets", fromInternet)
	http.HandleFunc("/", teslaAsReference)
	http.HandleFunc("/tesla.jpg", tesla)
	http.ListenAndServe(":8080", nil)

}

func teslaAsReference(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	io.WriteString(w, `<img src="/tesla.jpg">`)
}

func tesla(w http.ResponseWriter, r *http.Request) {
	f, err := os.Open("tesla.jpg")
	if err != nil {
		log.Fatalln(err)
	}

	defer f.Close()
	// w.Header().Set("Contnt-Type", "text/html; charset=utf-8")
	// io.WriteString(w, `<h1>This is being served from io.Copy(w, f)!</h1>`)

	// 1. way: io.Copy(w, f)
	// io.Copy(w, f)

	// 2. way: http.ServeContent(w, r, infoImage.Name(), infoImage.ModTime(), f)
	// infoImage, err := f.Stat()
	// if err != nil {
	// 	http.Error(w, "file not found", 404)
	// 	return
	// }
	// http.ServeContent(w, r, infoImage.Name(), infoImage.ModTime(), f)

	// 3. way: http.ServeFile(w, r, "tesla.jpg")
	http.ServeFile(w, r, "tesla.jpg")


}

func fromInternet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Contnt-Type", "text/html; charset=utf-8")
	io.WriteString(w, `<h1>This is being served from internet!</h1>`)
	io.WriteString(w, `
	<!-- not serving from our server-->
	<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/2018_Tesla_Model_S_75D.jpg/800px-2018_Tesla_Model_S_75D.jpg">`)

}

This post is licensed under CC BY 4.0 by the author.