[Solved] How to build a URL / Query in Golang


Normally, one should use url package’s Values.

Here’s an example, that does what I think you want, on play
Both a simple main, and in http.HandlerFunc form:

package main

import "fmt"
import "net/url"
import "net/http"

func main() {
    baseURL := "https://www.example.org/3/search/movie"
    v := url.Values{}
    v.Set("query", "this is a value")
    perform := baseURL + "?" + v.Encode()
    fmt.Println("Perform:", perform)
}

func formHandler(w http.ResponseWriter, r *http.Request) {
    baseURL := "https://www.example.org/3/search/movie"
    v := url.Values{}

    v.Set("query", r.Form.Get("GetSearchKey")) // take GetSearchKey from submitted form
    v.Set("api_ley", "YOURKEY") // whatever your api key is

    perform := baseURL + "?" + v.Encode() // put it all together
    fmt.Println("Perform:", perform) // do something with it
}

Output:
Perform: https://www.example.org/3/search/movie?query=this+is+a+value

Notice how the values are put in to query string, properly escaped, for you.

4

solved How to build a URL / Query in Golang