blob: f8772bd86ee838ecae06b6cdb7026ac679b1c01c [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001#summary Serving static files with HTTP.
2#labels http
3
4# Serving Static Files with HTTP
5
6The HTTP package provides good support for efficiently serving static files.
7
8This is a complete Go webserver serving static files:
9
10```
11package main
Dave Day0d6986a2014-12-10 15:02:18 +110012
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110013import "net/http"
Dave Day0d6986a2014-12-10 15:02:18 +110014
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110015func main() {
16 panic(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
17}
18```
19
20That example is intentionally short to make a point. Using panic() to deal with an error is probably too aggressive & would produce too much output. More typical style would be:
21
22See the [net/http documentation](http://golang.org/pkg/net/http/) and in particular the [FileServer example](http://golang.org/pkg/net/http/#example_FileServer) for more details.