Skip to content

Instantly share code, notes, and snippets.

@arnaldopereira
Created June 6, 2016 19:50
Show Gist options
  • Save arnaldopereira/87fe1554efc06086bde329876d054c77 to your computer and use it in GitHub Desktop.
Save arnaldopereira/87fe1554efc06086bde329876d054c77 to your computer and use it in GitHub Desktop.
golang webserver listening on a random port
package main
import (
"fmt"
"log"
"net"
"net/http"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "foobar\n")
}
func main() {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
log.Printf("listening on: %s", listener.Addr().String())
http.HandleFunc("/", indexHandler)
panic(http.Serve(listener, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment