Skip to content

HTTP client

The net/http package provides an HTTP client. Use http.Get for simple GET requests, or http.Client for custom timeouts, headers, etc.

Fetch a URL and print the status code.

package main
import (
"fmt"
"net/http"
"time"
)
func main() {
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get("https://example.com")
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
fmt.Println("Status:", resp.Status)
}
Terminal window
Status: 200 OK