Decorate types to implement io.Reader interface

The io package has provided a bunch of handy read functions and methods, but unfortunately, they all require the arguments satisfy io.Reader interface. See the following example: package main import ( "fmt" "io" ) func main() { s := "Hello, world!" p := make([]byte, len(s)) if _, err := io.ReadFull(s, p); err != nil { fmt.Println(err) } else { fmt.Printf("%s\n", p) } } Compile above program and an error is generated: [Read More]

io.Reader interface

io.Reader interface is a basic and very frequently-used interface: type Reader interface { Read(p []byte) (n int, err error) } For every type who satisfies the io.Reader interface, you can imagine it’s a pipe. Someone writes contents into one end of the pipe, and you can use Read() method which the type has provided to read content from the other end of the pipe. No matter it is a common file, a network socket, and so on. [Read More]