-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree.go
45 lines (37 loc) · 977 Bytes
/
three.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
// fool around a bit with data in file
package main
import (
"os"
"fmt"
)
var (
filename = "test2.txt"
content = "test string \n" // notice format char
contentB = []byte("1234567890 5 8") // Write expects bytes
contentC = "back up"
)
func main() {
f, err := os.Create(filename) // O_RDWR|O_CREATE|O_TRUNC, 0666
// get used to writing error processing code
if err != nil {
fmt.Println("error : os.Create : " + err.String())
os.Exit(1)
}
f.WriteString(content)
if err != nil {
fmt.Println("error : os.WriteString : " + err.String())
os.Exit(1)
}
f.Write(contentB) // notice 8 on end
if err != nil {
fmt.Println("error : os.Write : " + err.String())
os.Exit(1)
}
f.Seek(-2, 2) // backs up 2 bytes for next write start
f.WriteString(contentC) // backed up over 8 - how about that?
if err != nil {
fmt.Println("error : os.WriteString : " + err.String())
os.Exit(1)
}
f.Close() // close file - release memory
}