file - os.File 是如何实现 io.Writer 的?

标签 file go writer

我能做到:

f, err := os.Create("file")
if err != nil {
    ....
}
by := bufio.NewWriter(f)

还有这个:

var _ io.Writer = &os.File{}

os.File 的包文档导致 this source file它确实包含一个未导出的写函数,但是当我尝试使用未导出的函数实现接口(interface)时出现错误。

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

关于file - os.File 是如何实现 io.Writer 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39311307/

相关文章:

java - 使用索引迭代 Java Map

java - 创建使用 .get 方法命名的 .txt 文件

python - 使用 Panda df.to_excel(...) 后获取 excel 文件

python - “无效参数”错误打开文件(而不是读取文件)

PHP - 获取上传文件传输完成的时间戳

perl - 测试文件是否不是Perl中的目录

go - 在 Go 中存储/访问嵌套数据 - 使用结构?

go - 如何使用 Go Gorm 创建 TEXT 列

sqlite - 使用文件或 sqlite 数据库缓存数据哪个更好?

go - 未知运营商 : "$or" in mgo