Created
August 28, 2016 13:44
-
-
Save matryer/dbc2419cb39b84b5c4cf1135ae9ad416 to your computer and use it in GitHub Desktop.
Simple path parameter parsing function for Go
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
// pathParams parses the URL.Path of the Request with the given | |
// pattern, and extracts the value for each segment into a map. | |
func pathParams(r *http.Request, pattern string) map[string]string { | |
params := map[string]string{} | |
pathSegs := strings.Split(strings.Trim(r.URL.Path, "/"), "/") | |
for i, seg := range strings.Split(strings.Trim(pattern, "/"), "/") { | |
if i > len(pathSegs)-1 { | |
return params | |
} | |
params[seg] = pathSegs[i] | |
} | |
return params | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment