Skip to content

proposal: net/url: URL.Clone, Values.Clone #73450

Open
@seankhliao

Description

@seankhliao

Proposal Details

Proposed addition:

// Clone returns a copy of u
func (u *URL) Clone() *URL

// Clone returns a copy of v
func (v Values) Clone() Values

Motivation

Right now, there's no clear way to safely copy a net/url.URL: #38351
As the linked issue states, it is safe to do url2 := *url1, but it's not clear to users.
For example a rough (String/Parse are common names, so guestimate with u) search shows:

That is a significant portion (1/8) of users paying the additional cost of serialization/deserialization for what could be a simple copy.
The parse variant also means having to deal with an error return variable that should always be nil.

The safe and efficient way of doing it should be:

func cloneURL(u *url.URL) *url.URL {

For Values, because of its nested map[string][]string structure, a deep clone is necessary (and not possible with . While it is less common than a url's Parse(u.String()), I think it should be available:

cc @neild @rsc

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolPerformanceProposal

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions