NewRequestWithContext example to send get request
- User
NewRequestWithContext example to send get request
Assistant
Here's an example of how to use
http.NewRequestWithContextto send a GET request in Go:package main import ( "context" "fmt" "io" "net/http" "time" ) func main() { // Create a context with timeout ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() // Create a new request with context req, err := http.NewRequestWithContext(ctx, "GET", "https://example.com", nil) if err != nil { fmt.Printf("Error creating request: %v\n", err) return } // Send the request client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Printf("Error sending request: %v\n", err) return } defer resp.Body.Close() // Read the response body body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Error reading response: %v\n", err) return } fmt.Printf("Response status: %s\n", resp.Status) fmt.Printf("Response body: %s\n", body) }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.