Skip to content

Instantly share code, notes, and snippets.

@matryer
Created August 28, 2016 13:44
Show Gist options
  • Save matryer/dbc2419cb39b84b5c4cf1135ae9ad416 to your computer and use it in GitHub Desktop.
Save matryer/dbc2419cb39b84b5c4cf1135ae9ad416 to your computer and use it in GitHub Desktop.
Simple path parameter parsing function for Go
// 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