req, err := http.NewRequest("GET", "https://example.com", nil) if err != nil { log.Fatal(err) } resp, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Println(string(body))
data := url.Values{} data.Set("name", "John") data.Add("hobby", "reading") req, err := http.NewRequest("POST", "https://example.com", strings.NewReader(data.Encode())) if err != nil { log.Fatal(err) } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Println(string(body))This code creates a new `POST` request to `https://example.com`, sets the request body to a URL-encoded form containing `name=John` and `hobby=reading`, sets the `Content-Type` header to `application/x-www-form-urlencoded`, sends the request using the default HTTP client, reads the response body, and prints it to the console. In both examples, the `net/http` package was used to create and send HTTP requests. In the first example, a simple `GET` request was sent, while in the second example, a `POST` request was sent with a request body containing form data.