Created
February 14, 2012 15:41
-
-
Save acidsound/1827639 to your computer and use it in GitHub Desktop.
part of routing module for Go language
This file contains 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" | |
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