-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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 URL.Clone method #41733
Comments
Related #38351 |
What new API do you suggest? |
That is, what is the exact definition of the new |
// Clone returns a copy of u.
func (u *URL) Clone() *URL {
u2 := *u
return &u2
} |
I'm confused about this. This operation would apply to many structs where you want to make a change to one field to produce a new copy. This ends up being a common idiom in Go. Why is URL special? Why does it merit a special method? You wrote:
But honestly I have not seen it come up often at all, which would warrant not adding a helper. |
The
I have no empirical evidence to support my statement. However, I like to use this type of pattern: // will usually come from configuration
base, _ := url.Parse("http://foo/bar")
func DoReq() error {
u := base.Clone()
u.Path = path.Join(u.Path, "my/route")
// use the url to make a request
// ...
} |
I also end up making modifications to a base URL (e.g. here). Maybe it's enough just to document that |
@carlmjohnson "Can be shallow copied safely" is true of many types, not just URLs. But also, URLs can be mutated safely too, if you own the URL. Just like most types. |
Yes but as @icholy said originally,
You have to dig into the documentation for |
But the reason to make a shallow copy is to get a local copy you can mutate. If you are not going to modify the UserInfo in the copy then you don't need to dig into the documentation. |
It seems like there are two concerns with this proposal:
|
Regarding the first question, I've definitely seen the “base URL clone plus path” pattern described above in the wild. As for the second, perhaps this is a documentation issue, and we just need to add an example of doing that to the docs? |
I'd be happy with an example or a note in the documentation. |
I still don't understand what exactly is worth calling out in URL's documentation. And I still don't understand how often this operation is needed on URLs. |
Put another way, if we decide to add net.URL.Clone, what other types will we need to add Clone to? |
Speaking for myself, I’m happy not to have a Clone() method, but just noting that even though UserInfo is a pointer, it’s immutable so you don’t have to worry about it being mutated by someone else holding the original url.URL. |
For me in most these cases what i actually want is to resolve a new URL. So in this case if I understand your problem correctly I would do base, _ := url.Parse("http://foo/bar/")
u := base.ResolveReference(&url.URL{Path: "my/route"}) Maybe a bit cumbersome but I like that it makes it very explicit what is going on. |
@wader I don't like @rsc IMO the ambiguity around |
Copying a url is often done by shallow copying the struct. This can be confusing because it's not clear if the
Userinfo
should also be copied.Another option is to format/re-parse the url. But this adds unnecessary overhead.
Given how often url cloning comes up, I think it warrants adding a helper.
The text was updated successfully, but these errors were encountered: