Created
July 27, 2012 05:12
-
-
Save athom/3186277 to your computer and use it in GitHub Desktop.
an example of pat usage
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/bmizerany/pat" | |
"io" | |
"log" | |
"net/http" | |
) | |
// hello world, the web server | |
func HelloServer(w http.ResponseWriter, req *http.Request) { | |
io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n") | |
} | |
// I want a route changable after host | |
func HelloRoot(w http.ResponseWriter, req *http.Request) { | |
io.WriteString(w, "root: "+req.URL.Query().Get(":name")+"!\n") | |
} | |
func main() { | |
m := pat.New() | |
m.Get("/hello/:name", http.HandlerFunc(HelloServer)) | |
m.Get("/:name/", http.HandlerFunc(HelloRoot)) | |
// Register this pat with the default serve mux so that other packages | |
// may also be exported. (i.e. /debug/pprof/*) | |
http.Handle("/", m) | |
err := http.ListenAndServe(":12345", nil) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
给人家提issue的..