Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | #summary Serving static files with HTTP. |
| 2 | #labels http |
| 3 | |
| 4 | # Serving Static Files with HTTP |
| 5 | |
| 6 | The HTTP package provides good support for efficiently serving static files. |
| 7 | |
| 8 | This is a complete Go webserver serving static files: |
| 9 | |
| 10 | ``` |
| 11 | package main |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 12 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 13 | import "net/http" |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 14 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 15 | func main() { |
| 16 | panic(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc")))) |
| 17 | } |
| 18 | ``` |
| 19 | |
| 20 | That 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 | |
| 22 | See 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. |