Last active
February 22, 2022 08:02
-
-
Save romanitalian/35fd782eaf6f27492026a6b98fcec8c8 to your computer and use it in GitHub Desktop.
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 ( | |
"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