go语言自定义结构体实现io.Write接口,并测试func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) 函数。
package main import "fmt" type Demo struct { ByteBuffer []byte } func (demo *Demo)Write(p []byte) (n int, err error) { for _,v:=range p { demo.ByteBuffer = append(demo.ByteBuffer, v) } return len(p),nil } func main() { var demo Demo fmt.Printf("%s\n",string(demo.ByteBuffer)) fmt.Fprintf(&demo,"Hello, %s","世界!") fmt.Printf("%s\n",string(demo.ByteBuffer)) }
输出: