-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
52 lines (43 loc) · 947 Bytes
/
copy.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
package main
import (
"bytes"
"fmt"
"io"
"os"
)
func main() {
// io.Copy - arbitrary function to stream data from
// source reader to target writer.
data := new(bytes.Buffer)
data.WriteString("this ")
data.WriteString("is ")
data.WriteString("a ")
data.WriteString("book from copy.")
file, err := os.Create("./created-file.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
if _, err := io.Copy(file, data); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("successfully copied from buffer to file")
file2, err := os.Open("./created-file.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file2.Close()
if _, err := io.Copy(os.Stdout, file2); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("\nsuccessfully copied from file to stdout")
}
/* output
successfully copied from buffer to file
this is a book from copy.
successfully copied from file to stdout
*/