Minimalistic Go library to make handling http URLs easier.
httpurl complements Golang's url.URL from the net/url standard library. The desire to implement httpurl stems
from:
- noticing developers are using string manipulation functions to manipulate URLs/URIs. Inevitably, we end up with bits and pieces of incorrect code (especially when mixing regular expressions and URLs for domain validation).
- the lack of
url.MustParse. - query manipulation requiring going back and forth from
url.Querytourl.RawQuery. - somewhat inspired by Java's OkHttpUrl library. An early attempt to use a builder pattern didn't make production code any shorter or easier to write.
https://pkg.go.dev/github.com/alokmenghrajani/httpurl
Instead of:
u, err := url.Parse("http://example.com/")
if err != nil {
// Need to handle this error
}
q := u.Query()
q.Add("code", "1234")
u.RawQuery = q.Encode()
You can do:
u := httpurl.MustParse("http://example.com")
AddQueryParam(u, "code", 1234)
* * *
And instead of :
basePath := "https://example.com/users/%d/products/%s/"
baseURL, err := url.Parse(fmt.Sprintf(basePath, userId, productId))
if err != nil {
// Need to handle this error
}
You can do:
basePath := "https://example.com/users/{userId}/products/{productId}/"
baseURL := httpurl.MustParse(basePath)
httpurl.ExpandPath(baseURL, httpurl.ExpandMap{
"userId": userId,
"productId": productId,
})