-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.go
More file actions
37 lines (33 loc) · 723 Bytes
/
main.go
File metadata and controls
37 lines (33 loc) · 723 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
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func fetchRemoteResource(client *http.Client, url string) ([]byte, error) {
r, err := client.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
return io.ReadAll(r.Body)
}
func createHTTPClientWithTimeout(d time.Duration) *http.Client {
client := http.Client{Timeout: d}
return &client
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stdout, "Must specify a HTTP URL to get data from")
os.Exit(1)
}
client := createHTTPClientWithTimeout(15 * time.Second)
body, err := fetchRemoteResource(client, os.Args[1])
if err != nil {
fmt.Fprintf(os.Stdout, "%#v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "%s\n", body)
}