blob: 87de8f8800d441cc5758d69ba4ce684672b618ff [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Serving Static Files with HTTP
2
3The HTTP package provides good support for efficiently serving static files.
4
5This is a complete Go webserver serving static files:
6
Alex Tanb34411f2016-07-10 15:09:34 -05007```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11008package main
Dave Day0d6986a2014-12-10 15:02:18 +11009
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110010import "net/http"
Dave Day0d6986a2014-12-10 15:02:18 +110011
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110012func main() {
13 panic(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
14}
15```
16
sbdchdd65cafb2015-08-20 20:09:57 -040017That example is intentionally short to make a point. Using `panic()` to deal with an error is probably too aggressive & would produce too much output.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110018
sbdchdd65cafb2015-08-20 20:09:57 -040019See [net/http documentation](http://golang.org/pkg/net/http/) and in particular the [FileServer example](http://golang.org/pkg/net/http/#example_FileServer) for a more typical example.