-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello-world-19.go
122 lines (97 loc) · 2.4 KB
/
hello-world-19.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func readFileDemo() {
dat, err := ioutil.ReadFile("D:\\GoProject\\gobyExample\\fileTest.txt")
check(err)
fmt.Println(string(dat))
f, err := os.Open("D:\\GoProject\\gobyExample\\fileTest.txt")
check(err)
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes:%s\n", n1, string(b1[:n1]))
o2, err := f.Seek(6, 0)
check(err)
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes @ %d: ", n2, o2)
fmt.Printf("%v\n", string(b2[:n2]))
o3, err := f.Seek(6, 0)
check(err)
b3 := make([]byte, 2)
n3, err := io.ReadAtLeast(f, b3, 2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))
// 返回文件起始位置
_, err = f.Seek(0, 0)
check(err)
r4 := bufio.NewReader(f)
b4, err := r4.Peek(5)
check(err)
fmt.Printf("5 bytes: %s\n", string(b4))
f.Close()
}
func writeFileDemo() {
d1 := []byte("hello\ngo\n")
err := ioutil.WriteFile("D:\\GoProject\\gobyExample\\writeFile.txt", d1, 0644)
check(err)
f, err := os.Create("D:\\GoProject\\gobyExample\\writeTest1.txt")
check(err)
defer f.Close()
d2 := []byte{115, 111, 109, 101, 10}
n2, err := f.Write(d2)
fmt.Printf("wrote %d bytes\n", n2)
n3, err := f.WriteString("writes\n")
fmt.Printf("wrote %d bytes\n", n3)
// 将缓冲的数据写入硬盘
f.Sync()
w := bufio.NewWriter(f)
n4, err := w.WriteString("buffered\n")
fmt.Printf("wrote %d bytes\n", n4)
//使用 Flush 来确保,已将所有的缓冲操作应用于底层 writer。
w.Flush()
}
func filepathDemo() {
p := filepath.Join("dir1", "dir2", "filename")
fmt.Println("p:", p)
fmt.Println(filepath.Join("dir1//", "filename"))
fmt.Println(filepath.Join("dir1/../dir1", "filename"))
fmt.Println("Dir(p):", filepath.Dir(p))
fmt.Println("Base(p):", filepath.Base(p))
fmt.Println(filepath.IsAbs("dir/file"))
fmt.Println(filepath.IsAbs("/dir/file"))
filename := "config.json"
ext := filepath.Ext(filename)
fmt.Println(ext)
fmt.Println(strings.TrimSuffix(filename, ext))
// 寻找相对路径
rel, err := filepath.Rel("a/b", "a/b/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
rel, err = filepath.Rel("a/b", "a/c/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
rel, err = filepath.Rel("c/b", "a/c/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
}