Created
June 6, 2016 19:50
-
-
Save arnaldopereira/87fe1554efc06086bde329876d054c77 to your computer and use it in GitHub Desktop.
golang webserver listening on a random port
This file contains hidden or 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 ( | |
"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