Radar · Go · Archive · Week 8 · Feb 16 – 22, 2026
net/url: URL.Clone, Values.Clone
Worth knowingstdlib
- What changed
URL.Clone()returns a deep copy of anet/url.URL, copying theUserfield.Values.Clone()returns a deep copy of aurl.Valuesmap.- Production impact
- The source does not say.
- Try it
u2 := u1.Clone()and verifyu2 != u1and that modifications tou2do not affectu1.- Source
- github.com/golang/go/issues/73450
Understand it, then run it
URL.Clone() and Values.Clone() let you copy a net/url.URL or a url.Values without touching the original. Before, copying a URL was done by url2 := *url1 or by converting to a string and parsing again, both of which are confusing or expensive. Now you can simply call u.Clone() to get a new URL that is a deep copy, including the user info. The same applies to query parameters: v.Clone() copies a url.Values map so changes to the copy don’t affect the source.
Run it now
// This program demonstrates the new Clone methods for net/url.URL and url.Values.
// It runs in Go 1.27.1 and shows how to copy a URL and its query parameters safely.
package main
import (
"fmt"
"net/url"
)
func main() {
// Original URL with user info and query parameters.
orig, _ := url.Parse("https://user:[email protected]/path?foo=bar&baz=qux")
// Clone the URL. The returned value is a deep copy, including the User field.
cloned := orig.Clone()
// Modify the clone: change the path and add a query value.
cloned.Path = "/newpath"
cloned.Query().Set("new", "value")
// Show that the original URL is unchanged.
fmt.Println("Original:", orig.String())
fmt.Println("Cloned :", cloned.String())
// Clone the query values separately.
origVals := orig.Query()
clonedVals := origVals.Clone()
// Modify the cloned values.
clonedVals.Set("foo", "changed")
// The original values remain untouched.
fmt.Println("Original query:", origVals.Encode())
fmt.Println("Cloned query :", clonedVals.Encode())
}
What it printed when we ran it on Go 1.27.1
Original: https://user:[email protected]/path?foo=bar&baz=qux Cloned : https://user:[email protected]/newpath?foo=bar&baz=qux Original query: baz=qux&foo=bar Cloned query : baz=qux&foo=changed
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed