HTTP Client

This program demonstrates making an HTTP GET request to the "https://gobyexample.com" website and printing the first five lines of the response body. Here's a breakdown of the code:

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

Response status: 200 OK
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example</title>

Explanation:

  1. The http.Get function is used to make an HTTP GET request to the specified URL ("https://gobyexample.com"). The response and any error are checked.

  2. The defer statement ensures that the response body is closed when the function exits.

  3. The response status is printed to the console.

  4. A bufio.Scanner is created to read the response body line by line.

  5. A loop is used to read and print the first five lines of the response body.

  6. Any errors that occurred during scanning are checked and handled.

You can run the program to see the response status and the first five lines of the HTML content from "https://gobyexample.com".

Last updated