Skip to content

Instantly share code, notes, and snippets.

@dmateos
Created May 8, 2018 04:47
Show Gist options
  • Save dmateos/ca301ef8cd5fcbd2ba14b7c03804c4c0 to your computer and use it in GitHub Desktop.
Save dmateos/ca301ef8cd5fcbd2ba14b7c03804c4c0 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
)
type Routable interface {
Get() string
}
type RequestRouter struct {
}
func (router RequestRouter) RouteObject(path string, routed Routable) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
fmt.Fprintf(w, routed.Get())
break
}
})
}
func (routed RequestRouter) Go() {
http.ListenAndServe(":12345", nil)
}
type IndexRoute struct{}
func (i IndexRoute) Get() string {
return "Hello Worldz"
}
func main() {
route := RequestRouter{}
route.RouteObject("/", IndexRoute{})
route.Go()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment