forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example3.go
81 lines (66 loc) · 1.96 KB
/
example3.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// http://play.golang.org/p/OX6jTkWEF7
// https://gist.github.com/jmoiron/e9f72720cef51862b967#file-02-curl-go
// Sample code provided by Jason Moiron
// ./example3 -o goinggo.rss -s http://www.goinggo.net/feeds/posts/default
// Sample program to show how to use a MultiWriter to perform writes to
// multiple devices with one write call.
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
)
// Config contains program configuration options.
var Config struct {
Silent bool
DestFile string
}
// init is called before main.
func init() {
// Let the flag package handle the options; -o for output and -s for silent.
flag.StringVar(&Config.DestFile, "o", "", "output file")
flag.BoolVar(&Config.Silent, "s", false, "silent (do not output to stdout)")
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Println("Usage: ./example3 [options] <url>")
os.Exit(2)
}
}
// main is the entry point for the application.
func main() {
// r here is a response, and r.Body is an io.Reader
resp, err := http.Get(flag.Args()[0])
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// A slice of io.Writers we will write the file to.
var writers []io.Writer
// If we aren't in Silent mode, lets add Stdout to our writers.
if !Config.Silent {
writers = append(writers, os.Stdout)
}
// If DestFile was provided, lets try to create it and add to the writers.
if len(Config.DestFile) > 0 {
file, err := os.Create(Config.DestFile)
if err != nil {
fmt.Println(err)
return
}
writers = append(writers, file)
defer file.Close()
}
// MultiWriter(io.Writer...) returns a single writer which multiplexes its
// writes across all of the writers we pass in.
dest := io.MultiWriter(writers...)
// Write to dest the same way as before, copying from the Body.
_, err = io.Copy(dest, resp.Body)
if err != nil {
fmt.Println(err)
}
}