Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
part of routing module for Go language
package main
import "fmt"
import "strings"
var routes = []string{
"/user/:id/friends/:friendsId",
"/scrapbook/:scrapbookid/scrap/:scrapid",
"/"}
func getParams(url string) map[string]string {
for idx,_:=range routes {
format := routes[idx]
urlToken := strings.Split(url, "/")
formatToken := strings.Split(format, "/")
/* match pattern */
if len(urlToken)==len(formatToken) && len(formatToken)>0 {
params := make(map[string]string)
match := true
for i,v:=range formatToken {
if len(v)>0 && v[0:1] == ":" {
params[v[1:]]=urlToken[i]
} else if v!=urlToken[i] {
match =false
}
}
if match {
return params
}
}
}
return nil
}
func main() {
for i,v:=range getParams("/scrapbook/1ABCDE/scrap/401") {
fmt.Printf("[%s:%s]",i,v)
}
fmt.Println("")
for i,v:=range getParams("/user/spectrum/friends/cid") {
fmt.Printf("[%s:%s]",i,v)
}
fmt.Println("")
for i,v:=range getParams("/root") {
fmt.Printf("[%s:%s]",i,v)
}
fmt.Println("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment