HTTP Client
package main
import (
"bufio"
"fmt"
"net/http"
)
func main() {
// Make an HTTP GET request
resp, err := http.Get("https://gobyexample.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Print the response status
fmt.Println("Response status:", resp.Status)
// Use a scanner to read the response body line by line
scanner := bufio.NewScanner(resp.Body)
for i := 0; scanner.Scan() && i < 5; i++ {
// Print the first five lines of the response body
fmt.Println(scanner.Text())
}
// Check for any errors that occurred during scanning
if err := scanner.Err(); err != nil {
panic(err)
}
}Output
Last updated