-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse_test.go
58 lines (53 loc) · 1.17 KB
/
use_test.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
package scope_test
import (
"bytes"
"compress/gzip"
"encoding/hex"
"fmt"
"io"
"os"
"github.com/goaux/scope"
)
func ExampleUse() {
buf := &bytes.Buffer{}
for gz := range scope.Use(gzip.NewWriter(buf)) {
fmt.Fprintln(gz, "hello world")
}
fmt.Println(hex.EncodeToString(buf.Bytes()))
// Output:
// 1f8b08000000000000ffca48cdc9c95728cf2fca49e102040000ffff2d3b08af0c000000
}
func ExampleUse2() {
defer os.Remove("test.gz")
for file, err := range scope.Use2(os.Create("test.gz")) {
if err != nil {
fmt.Println(err)
break
}
for gz := range scope.Use(gzip.NewWriter(file)) {
if _, err := fmt.Fprintln(gz, "hello world"); err != nil {
fmt.Println(err)
break
}
// gz will be closed at the end of the loop body.
}
// file will be closed at the end of the loop body.
}
for file, err := range scope.Use2(os.Open("test.gz")) {
if err != nil {
fmt.Println(err)
break
}
if gz, err := gzip.NewReader(file); err != nil {
fmt.Println(err)
break
} else if b, err := io.ReadAll(gz); err != nil {
fmt.Println(err)
} else {
fmt.Print(string(b))
}
// file will be closed at the end of the loop body.
}
// Output:
// hello world
}