在Go中使用表单值发出HTTP GET [重复]

This question already has an answer here:

I see there is an easy way to send a post with form values, but is there a function I could use to do essentially the same as PostForm but for a GET request?

-- edit --

Basically, I want to construct the URL: https://uri.com?key=value but without using string concatenation with unescaped keys and values.

</div>

As explained in this question, you probably don't actually want to do this. Most servers won't do anything useful with a form body in a GET request—and most that do will just treat it as synonymous with the same parameters sent in a query string, in which case you should just put them in the query string of your url.URL object in the first place.

However, if you do have a good reason to do this, you can. As the docs you linked explain, PostForm is just a thin convenience wrapper around NewRequest and Do. Just as you're expected to use NewRequest yourself if you want to add custom headers, you can use it to attach a body.

And if you look at the source to PostForm, it's pretty trivial:

return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))

Basically, instead of this:

resp, err := client.PostForm(url, data)

… you do this:

body := strings.NewReader(data.Encode())
req, err := http.NewRequest("GET", url, body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)

Encode the query parameters using Values.Encode. Concatenate the base URL and query parameters to get the actual URL.

resp, err := http.Get(fmt.Sprintf("%s?%s", baseURL, data.Encode()))