Skip to content

Instantly share code, notes, and snippets.

@tzmartin
Created March 17, 2018 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tzmartin/4d68bcf16ca2a7e742fb82aa2acb64b3 to your computer and use it in GitHub Desktop.
Save tzmartin/4d68bcf16ca2a7e742fb82aa2acb64b3 to your computer and use it in GitHub Desktop.
Simple static server in go
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func printRecovery() {
if r := recover(); r != nil {
log.Fatalln(fmt.Sprintf("ERROR: %+v", r))
}
}
func main() {
defer printRecovery()
if len(os.Args) < 2 {
panic("Require first command-line argument to be port")
}
portStr := os.Args[1]
log.Println(fmt.Sprintf("Listening on %s...", portStr))
panic(http.ListenAndServe(":"+portStr, http.FileServer(http.Dir("."))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment