resp, err := http.Get("http://example.com") if err != nil { // handle error } defer resp.Body.Close() // check status code if resp.StatusCode == http.StatusOK { fmt.Println("Success!") } else { fmt.Println("Error:", resp.Status) }
resp, err := http.Get("http://example.com/nonexistent-resource") if err != nil { // handle error } defer resp.Body.Close() // check status code if resp.StatusCode == http.StatusNotFound { fmt.Println("The requested resource could not be found.") } else { fmt.Println("Error:", resp.Status) }The use of the net/http package can simplify the process of sending HTTP requests and handling responses, allowing developers to focus on the functionality of their applications rather than low-level networking details.