Created
August 1, 2017 19:43
-
-
Save rnbguy/9243fe9c5c873ba47b6f28f369598521 to your computer and use it in GitHub Desktop.
simple golang fileserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"net" | |
"net/http" | |
) | |
// simple golang fileserver | |
// alternative to `python -m http.server` | |
func main() { | |
var path string | |
flag.StringVar(&path, "dir", ".", "directory to serve") | |
flag.Parse() | |
fmt.Println("serving", path) | |
listener, err := net.Listen("tcp", ":0") | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("listening on", listener.Addr()) | |
http.Handle("/", http.FileServer(http.Dir(path))) | |
panic(http.Serve(listener, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment