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:
SchemestringOpaquestring// encoded opaque dataUser*Userinfo// username and password informationHoststring// host or host:portPathstring// path (relative paths may omit leading slash)RawPathstring// encoded path hint (see EscapedPath method); added in Go 1.5ForceQuerybool// append a query ('?') even if RawQuery is empty; added in Go 1.7RawQuerystring// encoded query values, without '?'Fragmentstring// fragment for references, without '#'
The trick here is that User *Userinfo is actually "immutable". This is documented in Userinfo: 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:
Can I get a confirmation that copying a URL is the correct way and not just an accident of its current implementation (so, it's guaranteed by Go 1.0 compatibility)?
Should we document it a little bit better, since it tends to be confusing to newcomers to this library? Either explicitly stating the correct way of copying it, or moving the "immutable" word in the inline documentation of the User *Userinfo field (or both?).
The text was updated successfully, but these errors were encountered: