[Golang] Writer Interface

goland writer interface

  • Writer Interface
  • 長這樣
  • type Writer interface {
        Write(p []byte) (n int, err error)
    }

     

  • 今天package os 裡面的 type File 有 attach method 
  • func (f *File) Write(b []byte) (n int, err error)

     

  • 就符合 type Writer interface的要求,那 type File就也同時是 type Writer
  • 例子
    func main() {
    	fmt.Println("Hello, playground")
    	fmt.Fprintln(os.Stdout, "Hello, playground")
    	io.WriteString(os.Stdout, "Hello, playground")
    }
    

     

  • End