Created
January 6, 2015 04:07
-
-
Save wadewegner/a048fadd9b32b85dcded to your computer and use it in GitHub Desktop.
Trying out the gorilla/pat request router and dispatcher package for Go.
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 ( | |
"github.com/gorilla/pat" | |
"net/http" | |
) | |
func main() { | |
r := pat.New() | |
r.Get("/route1", http.HandlerFunc(Route1Handler)) | |
r.Get("/route2", http.HandlerFunc(Route2Handler)) | |
r.Get("/route3/{variable}", http.HandlerFunc(Route3Handler)) | |
r.Get("/route4/{variable}/{id:[0-9]+}", Route4Handler) | |
r.Get("/", http.HandlerFunc(HomeHandler)) | |
http.Handle("/", r) | |
http.ListenAndServe(":3000", nil) | |
} | |
func Route1Handler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("Route1Handler")) | |
} | |
func Route2Handler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("Route2Handler")) | |
} | |
func Route3Handler(w http.ResponseWriter, r *http.Request) { | |
variable := r.URL.Query().Get(":variable") | |
w.Write([]byte("Route3Handler " + variable)) | |
} | |
func Route4Handler(w http.ResponseWriter, r *http.Request) { | |
variable := r.URL.Query().Get(":variable") | |
id := r.URL.Query().Get(":id") | |
w.Write([]byte("Route4Handler " + variable + " " + id)) | |
} | |
func HomeHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("HomeHandler")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment