-
Notifications
You must be signed in to change notification settings - Fork 0
/
idiomatic_test.go
67 lines (55 loc) · 976 Bytes
/
idiomatic_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
59
60
61
62
63
64
65
66
67
package examples_test
import (
"bufio"
"fmt"
"os"
"path"
"testing"
)
func TestIdiomatic(t *testing.T) {
tmpdir := t.TempDir()
err := idiomatic(tmpdir)
if err != nil {
t.Fatal(err)
}
}
func idiomatic(tmpdir string) error {
d1 := []byte("hello\ngo\n")
dat1 := path.Join(tmpdir, "dat1")
err := os.WriteFile(dat1, d1, 0644)
if err != nil {
return err
}
dat2 := path.Join(tmpdir, "dat2")
f, err := os.Create(dat2)
if err != nil {
return err
}
defer f.Close()
d2 := []byte{115, 111, 109, 101, 10}
n2, err := f.Write(d2)
if err != nil {
return err
}
fmt.Printf("wrote %d bytes\n", n2)
n3, err := f.WriteString("writes\n")
if err != nil {
return err
}
fmt.Printf("wrote %d bytes\n", n3)
err = f.Sync()
if err != nil {
return err
}
w := bufio.NewWriter(f)
n4, err := w.WriteString("buffered\n")
if err != nil {
return err
}
fmt.Printf("wrote %d bytes\n", n4)
err = w.Flush()
if err != nil {
return err
}
return nil
}