知识卡片
定位到开头不会截断旧尾巴
文件含12345时,从开头写abc只覆盖前三字节,旧尾巴45仍保留;Seek改变游标,不改变长度。完整替换需处理旧长度,本例先Truncate(0)、再Seek到0、再写入。截断、定位与写入各自可能失败,不能只检查最后Write。
结构图:
类图
classDiagram
class Writer {<<interface>>
+Write(p []byte) (n int, err error)
}
class tape {
-file *os.File
+Write(p []byte) (n int, err error)
}
class File {
+Truncate(size int64) error
+Seek(offset int64, whence int) (int64, error)
+Write(b []byte) (n int, err error)
}
Writer <|.. tape : 隐式实现
tape --> File : 截断定位后写入
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
- · IO 和排序
原书出处 · 1 处
- IO 和排序 · 查看原书依据
rite (0.00s) tape_test.go:23: got 'abc45' want 'abc'
参考来源
- 位置:《通过测试学习 Go 编程》IO 和排序,spine025(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/24.html)。
- 结论依据:原文Tape测试得到abc45,随后增加Truncate。
- 原始内容:rite (0.00s) tape_test.go:23: got 'abc45' want 'abc'