Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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".

PreviousLoggingNextHTTP Server

Last updated 1 year ago

Was this helpful?