-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreadfile.go
More file actions
52 lines (48 loc) · 853 Bytes
/
readfile.go
File metadata and controls
52 lines (48 loc) · 853 Bytes
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 (
"bufio"
"os"
"strings"
"io"
"fmt"
"time"
)
//读
func readfile(fileName string,handle func(string)) error {
f,err := os.Open(fileName)
if err != nil {
return err
}
buf := bufio.NewReader(f)
for {
line,err := buf.ReadString('\n')
//line = strings.TrimSpace(line)
line = strings.TrimRight(line,"")
handle(line)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
return nil
}
//写
func fwrite(fileName string,xcontent string) error {
f,err := os.OpenFile(fileName,os.O_WRONLY|os.O_APPEND|os.O_CREATE,0666)
if err != nil {
return err
}
defer f.Close()
f.WriteString(xcontent)
return nil
}
func printin (line string) {
fmt.Printf(line)
}
func main() {
readfile("mytext2.txt",printin)
content := fmt.Sprintf("write as %s\n",time.Now())
fwrite("mytext2.txt",content)
}