-
-
Save vzool/bb6d2115e53f28cf6adf2bbd80427a06 to your computer and use it in GitHub Desktop.
A golang web server on a randomly (os) chosen 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" | |
"github.com/skratchdot/open-golang/open" | |
"net" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", helloWorld) | |
listener, err := net.Listen("tcp", "127.0.0.1:0") | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("listening on", listener.Addr().String()) | |
open.Run("http://" + listener.Addr().String()) | |
panic(http.Serve(listener, nil)) | |
} | |
func helloWorld(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("hello world")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment