-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise09.go
More file actions
38 lines (31 loc) · 775 Bytes
/
Copy pathexercise09.go
File metadata and controls
38 lines (31 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright © 2016, 2019 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Modified by Andy Dalton to implement exercise solution.
// Fetch prints the content found at a URL.
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
const httpPrefix = "http://"
for _, url := range os.Args[1:] {
if !strings.HasPrefix(url, httpPrefix) {
url = httpPrefix + url
}
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
fmt.Printf("HTTP Response Status: %v\n\n", resp.Status)
if _, err = io.Copy(os.Stdout, resp.Body); err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
}
}