Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Last active February 22, 2022 08:02
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 romanitalian/35fd782eaf6f27492026a6b98fcec8c8 to your computer and use it in GitHub Desktop.
Save romanitalian/35fd782eaf6f27492026a6b98fcec8c8 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/http"
)
// go run main.go
//
// $ curl "http://127.0.0.1:8090"
// hello on port: 8090
//
// $ curl "http://127.0.0.1:8091"
// hello on port: 8091
func main() {
go runServer("8090")
go runServer("8091")
select {}
}
type helloHandler struct {
foo string
}
func (h helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello on port: " + h.foo))
}
// func helloHandler(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("hello"))
// }
func runServer(port string) {
// "panic: http: multiple registrations for /"
// http.HandleFunc("/", helloHandler)
// http.ListenAndServe(host+":"+port, nil)
mux := http.NewServeMux()
helloSrv := helloHandler{foo: port}
mux.Handle("/", helloSrv)
log.Println("Listening... on port: " + port)
http.ListenAndServe(":"+port, mux)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment