Home > Article > Backend Development > How to Implement In-Place URL Parameter Mapping in Go?
Go URL Parameters Mapping: In-Place Native Implementation
While Go does not provide a built-in method for in-place URL parameter mapping, it's relatively straightforward to implement it manually.
A solution is to split the URL path into parts and analyze them. For instance, for the URL "http://localhost:8080/blob/123/test", we can extract the parameter value "123" as follows:
func getCode(r *http.Request, defaultCode int) (int, string) { p := strings.Split(r.URL.Path, "/") if len(p) == 1 { return defaultCode, p[0] } else if len(p) > 1 { code, err := strconv.Atoi(p[0]) if err == nil { return code, p[1] } } return defaultCode, "" }
This code snippet can be invoked from a request handler to retrieve the URL parameter. By integrating this functionality into your code, you can achieve in-place URL parameter mapping without relying on external libraries.
The above is the detailed content of How to Implement In-Place URL Parameter Mapping in Go?. For more information, please follow other related articles on the PHP Chinese website!