Created
May 8, 2018 04:47
-
-
Save dmateos/ca301ef8cd5fcbd2ba14b7c03804c4c0 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 ( | |
"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