Golang HTTP GET request with parameters
Example-1
To make an HTTP GET request with parameters in Go, you can use the http package and the net/url package to build the URL with the parameters. Here is an example of how to do this:
Example
package main
import (
"fmt"
"net/http"
"net/url"
)
func main() {
baseURL := "http://example.com"
resource := "/path"
params := url.Values{}
params.Add("param1", "value1")
params.Add("param2", "value2")
u, _ := url.ParseRequestURI(baseURL)
u.Path = resource
u.RawQuery = params.Encode()
urlStr := fmt.Sprintf("%v", u) // "http://example.com/path?param1=value1¶m2=value2"
resp, err := http.Get(urlStr)
// handle error and response
}
Example-2
Here is an example of making an HTTP GET request in Go with parameters in the query string:
Example
package main
import (
"fmt"
"net/http"
"net/url"
)
func main() {
baseURL, _ := url.Parse("http://example.com")
params := url.Values{}
params.Add("param1", "value1")
params.Add("param2", "value2")
baseURL.RawQuery = params.Encode()
resp, _ := http.Get(baseURL.String())
defer resp.Body.Close()
fmt.Println(resp.Status)
}
Example-3
To make an HTTP GET request with parameters in Go, you can use the net/http package. Here's an example of how to do it:
Example
package main
import (
"fmt"
"net/http"
"net/url"
)
func main() {
// Define the parameters
params := url.Values{}
params.Add("param1", "value1")
params.Add("param2", "value2")
// Create the URL with the parameters
url := "https://example.com?" + params.Encode()
// Make the GET request
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// Do something with the response
// ...
}
Most Helpful This Week
How to Improve the Performance of Your Golang Application?
How do you send an HTTP PUT request in Go?
Cannot convert <type1> to <type2> error in Golang
Example: ReadAll, ReadDir, and ReadFile from IO Package
GO Program to Generate Fibonacci Sequence Up to a Certain Number
Program in Go language to Calculate Average Using Arrays
Most Helpful This Week
How to print string with double quote in Go?How to handle HTTP Get response?How to read names of all files and folders in current directory?Various examples of printing and formatting in GolangRuntime package variablesHow to declare empty Map in Go?How pointer & and * and ** works in Golang?How to import structs from another package in Go?Golang Read Write Create and Delete text fileHow to use for and foreach loop?