Go网络编程-Assets and Files
跳到导航
跳到搜索
这个例子将展示如何从特定目录提供静态文件,如CSS、JavaScript或图片。
// 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;
}