How to easily remove query string parameters from a url.URL
in Go.
Today’s short post shows how simple it is.
To remove a query string param component from a Go url.URL
, you have to rebuild the RawQuery
portion of the url.URL
.
-
First you extract the
url.Values
from theurl.Url
withq := myURL.Query()
, and use theq.Del(key)
method to drop the component you wish to strip (wherekey
is the name portion of the key/value pair). -
Then, simply assign the output of
q.Encode()
to themyURL.RawQuery
field.
That’s it. Simple, but a little subtle, so I wrote this short post to remind myself.
You can see the code below in action on the Go Playground
The relevant lines are highlighted.
|
|