Search code examples
filegowriter

How does os.File implement io.Writer?


I'm able to do this:

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

And this:

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

The package documentation for os.File leads to this source file which does contain an unexported write function but I get an error when I try to implement an interface with an unexported function.

var _ Disease = &Scratch{}  // *Scratch doesn't implement Disease have spread() want Spread()
type Disease interface {
    Spread()
}
type Scratch struct {
    ....
}
func (s* Scratch) spread() {
    ....
}

What am I missing ?

Update: os.File did need cleaning up


Solution

  • You are missing the exported Write([]byte) defined on *os.File here: https://golang.org/src/os/file.go?s=4417:4466#L128