Open rasky opened 4 years ago
User *Userinfo // username and password immutable information
This documentation is better?
Hi @rasky,
Indeed it's safe and actually I, the author of that Medium post, used the newU := *u
method in a PR that got merged some weeks ago: https://github.com/golang/go/pull/35578/files#diff-6c2d018290e298803c0c9419d8739885R824-R837
Relevant comment https://github.com/golang/go/issues/41733#issuecomment-718048068 by @rsc
I still don't understand what exactly is worth calling out in URL's documentation. It is a general property of data that if you want to make a copy before mutating you can do u2 := *u; mutate u2.
And I still don't understand how often this operation is needed on URLs. @ainar-g has seen it, but that establishes existence not frequency.
There is something confusing about #41733 due to there actually is cloneURL function in net/http internals not just a u2 := *u thing, however it's not a part of url.URL public protocol itself for some reason.
Ran into this recently where there is no good way to deep clone a URL.
It sometimes happens to have a need to duplicate (aka "deep copy") a
net.URL
object.My understanding is that
net.URL
can be safely duplicated by simply copying the structure:Since
net.URL
is usually handled by pointer, it's normal to check the documentation to see whether the object contains internal pointers, in which case duplication might get hairy.net.URL
has the following contents:So there is indeed a pointer in there. This brought people to come up with alternative ways of duplicating a URL such as a
String
/Parse
roundtrip: https://medium.com/@nrxr/quick-dirty-way-to-deep-copy-a-url-url-on-go-464312241de4The trick here is that
User *Userinfo
is actually "immutable". This is documented inUserinfo
:The Userinfo type is an immutable encapsulation of username and password [...]
. So it looks like that, given this guarantee of immutability, it is indeed safe to simply copy a URL instance to duplicate it. Copying a URL instance is also performed by the standard library, for instance:https://github.com/golang/go/blob/245409ea86f20fd9f4167223c2339fb238f9e4b6/src/net/http/request.go#L361
Hence the subject of this issue:
User *Userinfo
field (or both?).