garettmd
5/14/2018 - 1:43 PM

Example of using http.Get in go (golang)

Example of using http.Get in go (golang)

package main

import (
        "fmt"
        "io"
        "log"
        "net/http"
        "os"
)

func main() {
        if len(os.Args) != 2 {
                fmt.Fprintf(os.Stderr, "Usage: %s URL\n", os.Args[0])
                os.Exit(1)
        }
        response, err := http.Get(os.Args[1])
        if err != nil {
                log.Fatal(err)
        } else {
                defer response.Body.Close()
                _, err := io.Copy(os.Stdout, response.Body)
                if err != nil {
                        log.Fatal(err)
                }
        }
}