-
Notifications
You must be signed in to change notification settings - Fork 17.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: net/url: add AppendQuery #63777
Comments
This is a pretty common need, but should it be append or set? // WithValues returns a copy of u with the keys and values in vv set on its query string.
func (u *URL) WithValues(vv Values) *URL {
q := u.Query()
for k, v := range vv {
q[k] = v
}
ret := *u
ret.RawQuery = q.Encode()
return &ret
} I think the main argument against adding this is that people need different variations on this in different circumstances, so the easiest thing is for people to just keep doing it by hand each time. But probably setting Values covers the 80% case, so it might be worth it… |
👍 I agree that that's the more common use case. I was thinking append would be more versatile, but versatility isn't necessarily a good thing. |
Kinda surprised we don't have a I like Are there other parts of the URL we should give the same treatment to? For example, /cc @golang/security since the example shows a pretty scary security footgun. It's not a vulnerability in the standard library but we can probably make safer APIs. |
By the way, the race detector would catch this if there was a concurrent test. Those are not easy to write but often catch bugs especially with the race detector. Moreover, I wonder if @dominikh's staticcheck could try to flag any global write in an http.Handler, which is by nature concurrent. (Maybe it's too hard to ignore mutex-guarded values?) |
Any part of the URL could probably benefit from this. There's already |
I have no particular dog here, but I would like to point out a couple of things. This is the URL type:
|
URL.Clone was proposed here #41733 |
URL already has JoinPath. I don’t think modifying host or scheme is common enough to need a helper. Modifying query parameters is a frequent need though. Maybe call it JoinValue so it rhymes with JoinPath? |
It's pretty easy to accidentally mutate a shared
URL
struct when trying to create a URL with query parameters:APIs to help with constructing query strings and adding them to a URL — a la
(*URL) JoinPath(elems ...string) *URL
— could prevent this:The text was updated successfully, but these errors were encountered: