Here’s how to add key/value pairs to an http.Reqest
’s query string in Go:
package main
import (
"fmt"
"net/http"
)
func main() {
// create a request
req, _ := http.NewRequest(http.MethodGet, "http://www.example.com/", nil)
// get the current query string from the http.Request
values := req.URL.Query()
// add or set values
values.Set("myKey1", "myVal1") // overwrite!
values.Add("myKey1", "myVal2") // append!
// add values back to the request
req.URL.RawQuery = values.Encode() // query string: mykey1=myVal1&myKey1=myVal2
// print the url
fmt.Println(req.URL.String())
}
The above program will output:
http://www.example.com/?myKey1=myVal1&myKey1=myVal2
Easy peasy!