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