Skip to content

Instantly share code, notes, and snippets.

@martin-g
Created July 9, 2020 12:59
Show Gist options
  • Save martin-g/5a857be4a0767794757218f5ef987155 to your computer and use it in GitHub Desktop.
Save martin-g/5a857be4a0767794757218f5ef987155 to your computer and use it in GitHub Desktop.
package main
// run with: env PORT=8081 go run http-server.go
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("Please specify the HTTP port as environment variable, e.g. env PORT=8081 go run http-server.go")
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "Hello World")
})
log.Fatal(http.ListenAndServe(":" + port, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment