// Created on savesnippets.com · https://savesnippets.com/W2kuYDBFKx7v6a package main import ( "fmt" "net/url" ) func main() { // Build from scratch u := url.URL{ Scheme: "https", Host: "api.example.com", Path: "/users", } q := u.Query() q.Set("active", "true") q.Add("role", "admin") q.Add("role", "editor") // repeated key — fine q.Set("q", "hello world & friends") // spaces and & properly escaped u.RawQuery = q.Encode() fmt.Println(u.String()) // https://api.example.com/users?active=true&q=hello+world+%26+friends&role=admin&role=editor // Parse an existing URL parsed, _ := url.Parse("https://api.example.com/users?id=42") fmt.Println(parsed.Host) // api.example.com fmt.Println(parsed.Query().Get("id")) // 42 }