Inplace URL Parameters in Go
Native Go lacks a fundamental mechanism for inplace URL parameter mapping. However, implementing such a feature is relatively straightforward.
Custom Solution
The following approach does not rely on external libraries:
Analyze each component:
Code Sample
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] } else { return defaultCode, p[1] } } else { return defaultCode, "" } }
This code can be invoked within request handlers using the getCode() function.
The above is the detailed content of Can Go Handle Inplace URL Parameter Mapping?. For more information, please follow other related articles on the PHP Chinese website!