Skip to content

Instantly share code, notes, and snippets.

@juneym
Forked from gotnix/http_serv.go
Last active October 22, 2015 05:19
Show Gist options
  • Save juneym/40d3ebd7cb1c2465212a to your computer and use it in GitHub Desktop.
Save juneym/40d3ebd7cb1c2465212a to your computer and use it in GitHub Desktop.
Share you directory via http server by golang.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"strconv"
)
func main() {
dir := flag.String("dir", "", "Directory path you want to share (start with / or ./)")
port := flag.Int("port", 8080, "Port number to listen")
flag.Parse()
if *dir == "" {
flag.Usage()
return
}
h := http.FileServer(http.Dir(*dir))
fmt.Scanf("%s", &port)
fmt.Println("Listening to 0.0.0.0:" + strconv.Itoa(*port) + "\n")
err := http.ListenAndServe(":"+strconv.Itoa(*port), h)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment