When developing web applications, we often need to send HTTP requests to the server and include some parameters in the request. In Go, we can use the net/http packet to send HTTP requests and add parameters to GET requests very conveniently.
This article will introduce how to send HTTP requests in Go and add parameters to GET requests, and show them with an example of a practical problem.
Send HTTP request
First, we need to import the net/http packet to send HTTP requests. Here is an example of sending a request using the GET method:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { resp, err := (" if err != nil { ("Request failed to send:", err) return } defer () body, err := () if err != nil { ("Read response failed:", err) return } (string(body)) }
In the example above, we sent a GET request using the method and saved the response in the resp variable. We then read the response's content using the method and print it out.
Add parameters to GET request
If we need to add parameters to the GET request, just add the query string to the URL. The query string starts with?, followed by a series of key-value pairs, separated by &. Here is an example of adding parameters to a GET request:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := " params := "?key1=value1&key2=value2" resp, err := (url + params) if err != nil { ("Request failed to send:", err) return } defer () body, err := () if err != nil { ("Read response failed:", err) return } (string(body)) }
In the example above, we define a URL variable and a params variable. The URL variable specifies the requested URL address, and the params variable specifies the parameters to be added. Then, when we use the method to send the request, we connect the URL and params to form the complete request URL.
Examples and practical problems
Now let's look at an example of a practical problem, suppose we need to get weather information for a city from a public API. We can use the following code to send a GET request and add parameters:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := " params := "?city=Beijing&lang=en" resp, err := (url + params) if err != nil { ("Request failed to send:", err) return } defer () body, err := () if err != nil { ("Read response failed:", err) return } (string(body)) }
In the example above, we used a public API called to get weather information for Beijing and set the language to English. We added two parameters to the GET request: city and lang. We then use the method to send the request and print out the response content.
This example shows how to send a GET request with parameters in Go language, solving the actual problem of getting weather information.
Summarize
By using the net/http package, we can easily send HTTP requests and add parameters to the GET requests. In actual development, we often need to use GET requests to obtain specific information, and adding parameters to GET requests is a very common requirement. The above example provides a simple and practical way to solve this problem and provides an example of a practical problem of getting weather information.
This is the article about sending HTTP requests in Go and adding parameters to GET. For more information about sending HTTP requests in Go, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!