Assets and Files

这个例子将展示如何使用静态文件比如CSS、 JavaScripts或图片.

// static-files.go
package main

import "net/http"

func main() {
	fs := http.FileServer(http.Dir("assets/"))
	http.Handle("/static/", http.StripPrefix("/static/", fs))

	http.ListenAndServe(":8080", nil)
}
$ tree assets/
assets/
└── css
    └── styles.css
$ go run static-files.go

$ curl -s http://localhost:8080/static/css/styles.css
body {
    background-color: black;
}