]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: better stand-alone FileServer doc example
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 4 Apr 2013 20:40:15 +0000 (13:40 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 4 Apr 2013 20:40:15 +0000 (13:40 -0700)
Motivated by a deletion in the wiki, which had a better
example.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/8288045

src/pkg/net/http/example_test.go

index eed3beeea31a4c9c1d7d374d7f677bd017fb08cd..bc60df7f2b587a2c73bfac87ce62befa0c7d037d 100644 (file)
@@ -51,11 +51,20 @@ func ExampleGet() {
 }
 
 func ExampleFileServer() {
-       // we use StripPrefix so that /tmpfiles/somefile will access /tmp/somefile
+       // Simple static webserver:
+       log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
+}
+
+func ExampleFileServer_stripPrefix() {
+       // To serve a directory on disk (/tmp) under an alternate URL
+       // path (/tmpfiles/), use StripPrefix to modify the request
+       // URL's path before the FileServer sees it:
        http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
 }
 
 func ExampleStripPrefix() {
-       // we use StripPrefix so that /tmpfiles/somefile will access /tmp/somefile
+       // To serve a directory on disk (/tmp) under an alternate URL
+       // path (/tmpfiles/), use StripPrefix to modify the request
+       // URL's path before the FileServer sees it:
        http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
 }