-
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
net: clarify safe way to do URL deep-copying #38351
Comments
This documentation is better? |
Hi @rasky, Indeed it's safe and actually I, the author of that Medium post, used the |
Relevant comment #41733 (comment) by @rsc
|
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-464312241de4
The 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:go/src/net/http/request.go
Line 361 in 245409e
Hence the subject of this issue:
User *Userinfo
field (or both?).The text was updated successfully, but these errors were encountered: