Skip to content

Instantly share code, notes, and snippets.

@falmar
Forked from filewalkwithme/main.go
Created January 16, 2016 17:22
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 falmar/2f309b8808e74bf09313 to your computer and use it in GitHub Desktop.
Save falmar/2f309b8808e74bf09313 to your computer and use it in GitHub Desktop.
Listening multiple ports on golang http servers (using http.Handler)
package main
import (
"net/http"
)
func main() {
go func() {
http.ListenAndServe(":8001", &fooHandler{})
}()
//the last call is outside goroutine to avoid that program just exit
http.ListenAndServe(":8002", &barHandler{})
}
type fooHandler struct {
}
func (m *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Listening on 8001: foo "))
}
type barHandler struct {
}
func (m *barHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Listening on 8002: bar "))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment