Appending to a Querystring using Go
As noted in Don't Just String Append to a Querystring, we should avoid using string concatenation, and instead use our URL library's functionality.
With Go, we'd parse the URL, amend the querystring, and then make sure we set the RawQuery
to allow us to convert it back to a string:
func addTracking(theUrl string) string {
u, err := url.Parse(theUrl)
if err != nil {
panic(err)
}
queryString := u.Query()
// append
queryString.Add("utm_campaign", "the_tracking_campaign")
// override
queryString.Set("utm_campaign", "the_tracking_campaign")
// make sure
u.RawQuery = queryString.Encode()
return u.String()
}