os.File 是如何实现 io.Writer 的?
How does os.File implement io.Writer?
我能做到:
f, err := os.Create("file")
if err != nil {
....
}
by := bufio.NewWriter(f)
还有这个:
var _ io.Writer = &os.File{}
os.File leads to this source file 的包文档确实包含 unexported 写入函数,但是当我尝试使用未导出函数实现接口时出现错误。
var _ Disease = &Scratch{} // *Scratch doesn't implement Disease have spread() want Spread()
type Disease interface {
Spread()
}
type Scratch struct {
....
}
func (s* Scratch) spread() {
....
}
我错过了什么?
更新: os.File did need cleaning up
您缺少在 *os.File
上定义的导出的 Write([]byte)
:https://golang.org/src/os/file.go?s=4417:4466#L128
我能做到:
f, err := os.Create("file")
if err != nil {
....
}
by := bufio.NewWriter(f)
还有这个:
var _ io.Writer = &os.File{}
os.File leads to this source file 的包文档确实包含 unexported 写入函数,但是当我尝试使用未导出函数实现接口时出现错误。
var _ Disease = &Scratch{} // *Scratch doesn't implement Disease have spread() want Spread()
type Disease interface {
Spread()
}
type Scratch struct {
....
}
func (s* Scratch) spread() {
....
}
我错过了什么?
更新: os.File did need cleaning up
您缺少在 *os.File
上定义的导出的 Write([]byte)
:https://golang.org/src/os/file.go?s=4417:4466#L128