package main import ( "fmt" "net/http" ) func main() { resp, err := http.Get("https://www.google.com/") if err != nil { fmt.Println(err) return } defer resp.Body.Close() fmt.Println(resp.StatusCode) }
package main import ( "bytes" "fmt" "net/http" ) func main() { url := "https://httpbin.org/post" data := []byte(`{"name": "John", "age": 30}`) resp, err := http.Post(url, "application/json", bytes.NewBuffer(data)) if err != nil { fmt.Println(err) return } defer resp.Body.Close() fmt.Println(resp.StatusCode) }In this example, we use the http.Post() function to send an HTTP POST request to the URL "https://httpbin.org/post" with the JSON data {"name": "John", "age": 30}. We also set the content-type header to application/json. After checking for errors, we print the HTTP status code in the response to the console. Package library: net/http.