Golang URL query parameters: url.Values, encoding, and reading requests

Golang url values and url.Values.Encode to add query parameters; golang http request parameters from r.URL.Query; golang url parameters with Set and Add; avoid raw fmt.Sprintf for untrusted strings. Related HTTP overview in Go.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Golang URL query parameters: url.Values, encoding, and reading requests

Some Search Console rows look like literal path fragments (for example /go/url=) from redirects or analytics, not questions about the net/url API. This page instead matches golang url values, url.values golang, golang url add query parameters, golang url parameters, and golang http request parameters: building encoded query strings with net/url and reading them back from http.Request. For broader HTTP context, see HTTP in Go.

Tested with Go 1.24 on Linux.


golang url values and url.Values

url.Values is a map[string][]string with helpers for query strings. Typical flow for golang url add query parameters:

  • Set(key, value) replaces any existing values for key with a single value.
  • Add(key, value) appends another value for the same key (multiple ?tag=go&tag=linux style).
  • Encode() returns a safely escaped query string (without a leading ?).

Build on top of url.URL by assigning RawQuery:

go
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
)

func main() {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		for k, vs := range r.URL.Query() {
			fmt.Fprintf(w, "%s=%s\n", k, strings.Join(vs, ","))
		}
	}))
	defer srv.Close()

	base, _ := url.Parse(srv.URL)
	q := base.Query()
	q.Set("name", "Go Linux")
	q.Set("id", "1")
	q.Add("tag", "go")
	q.Add("tag", "linux")
	base.RawQuery = q.Encode()

	resp, err := http.Get(base.String())
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	fmt.Print(string(body))
}
Output

Run it locally; you should see lines such as name=Go Linux, id=1, and tag=go,linux (order may vary). The space in Go Linux is encoded in the real HTTP request even though this demo prints decoded keys and values.


golang http request parameters on the server

Inside a handler, r.URL.Query() returns the same url.Values type. Use Get("id") for a single value, or r.URL.Query()["tag"] when you need every repetition of a key.


Why not only fmt.Sprintf for URLs?

fmt.Sprintf("http://host/?q=%s", userInput) skips proper escaping and breaks on spaces, &, =, or non-ASCII text. Treat that pattern as a last resort for fixed demo strings; prefer url.Values or url.QueryEscape for golang url parameters built from variables.


Summary

Golang url values workflows center on url.Values: Set, Add, and Encode implement golang url add query parameters with correct escaping. golang http request parameters are read with r.URL.Query() on the server. Rows like /go?url= in analytics usually belong to redirect URLs, not this API surface—use net/url when you truly need golang pass multiple strings into URL style behavior safely.


References


Frequently Asked Questions

1. What is url.Values in Go?

url.Values is a map[string][]string alias used for query strings and forms. Use Set for a single value, Add to append another value for the same key, then Encode for a safe application/x-www-form-urlencoded style query string.

2. Should I build query strings with fmt.Sprintf?

Only for fixed, already-safe literals. For user or dynamic text, use url.Values or url.QueryEscape so spaces, ampersands, and Unicode are encoded correctly.

3. How do I read golang http request parameters on the server?

Call r.URL.Query() which returns url.Values; use Get for one value or index the map for all values when a key may appear multiple times.

4. When should I use url.ParseQuery instead of Values on a raw string?

ParseQuery returns an error for malformed pairs; URL.Query uses ParseQuery internally but discards bad pairs—use ParseQuery when you need strict validation.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …