go语言自定义结构体实现io.Write接口
go语言自定义结构体实现io.Write接口,并测试func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) 函数。
package mainimport "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))}
输出:
以上是 go语言自定义结构体实现io.Write接口 的全部内容, 来源链接: utcz.com/a/74691.html