Skip to content

Instantly share code, notes, and snippets.

@tejzpr
Forked from xcsrz/server.go
Created July 18, 2022 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejzpr/4ad08f3a8488b974d53e1cfa683c59e6 to your computer and use it in GitHub Desktop.
Save tejzpr/4ad08f3a8488b974d53e1cfa683c59e6 to your computer and use it in GitHub Desktop.
A golang web server on a randomly (os) chosen port.
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