Skip to content

Commit bb88893

Browse files
committed
Update README.md
1 parent 334a6b3 commit bb88893

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
本书籍是记录自己在学习Go语言的过程中遇到的思考与感悟。写作过程中,大量参考借鉴甚至是复制了其他类似的项目。感谢每一个项目,致敬每一位Gopher!尽可能的熟练使用Go语言,尽可能的深入理解Go语言。努力成为Go语言特长型程序员。学习Go语言,面向信仰编程!作者:[0e0w](https://github.com/0e0w/LearnGolang)。Less is More or Less is Less.
44

5-
本项目创建于2020年9月1日,最近的一次更新时间为2021年11月21日。本项目会持续更新,直到海枯石烂。
5+
本项目创建于2020年9月1日,最近的一次更新时间为2021年11月22日。本项目会持续更新,直到海枯石烂。
66

77
项目暂计划共七章。项目未完成,持续更新整理中!感谢关注!今天你学习Go语言了吗?
88

@@ -3041,6 +3041,53 @@
30413041
30423042
```
30433043

3044+
- 案例四:
3045+
3046+
```go
3047+
func main(){
3048+
lines, err := LineReader("file.txt", 0)
3049+
if err != nil {
3050+
}
3051+
for line := range lines {
3052+
fmt.Println(line)
3053+
}
3054+
}
3055+
3056+
func LineReader(filename string, noff int64) (chan string,error) {
3057+
fp, err := os.Open(filename)
3058+
if err != nil {
3059+
return nil, err
3060+
}
3061+
3062+
// if offset defined then start from there
3063+
if noff > 0 {
3064+
// and go to the start of the line
3065+
b := make([]byte, 1)
3066+
for b[0] != '\n' {
3067+
noff--
3068+
//fp.Seek(noff, os.SEEK_SET)
3069+
fp.Read(b)
3070+
}
3071+
noff++
3072+
}
3073+
out := make(chan string)
3074+
go func() {
3075+
defer fp.Close()
3076+
// we need to close the out channel in order
3077+
// to signal the end-of-data condition
3078+
defer close(out)
3079+
scanner := bufio.NewScanner(fp)
3080+
scanner.Split(bufio.ScanLines)
3081+
for scanner.Scan() {
3082+
noff, _ = fp.Seek(0, os.SEEK_CUR)
3083+
out <- scanner.Text()
3084+
}
3085+
}()
3086+
3087+
return out, nil
3088+
}
3089+
```
3090+
30443091
- [ ] 把文本文件作为参数:
30453092

30463093
- [ ] 把结果写入到文本文件中:

0 commit comments

Comments
 (0)