Golang Go语言中 bytes.Buffer 包的疑惑
bytes.Buffer 包的疑惑 请教各位一个问题,bytes.Buffer 包,在使用 bytes.NewBuffer 时候是如何调用到这个 func (b *Buffer) String() string 这个方法呢? 因为看源码 bytes.NewBuffer 要求传入一个[]byte 类型,返回一个 Buffer 类型,这个是如何调用到 String()这个方法,把[]byte 转换为 string 的呢? s1 是一个字符串类型 s2 := ([]byte(s1)) x := bytes.NewBuffer(s2) fmt.Println(x) //显示出来是字符串,肯定是上面方法的作用,但是不知道是如果调用的 研究了半天,可能是我太菜,所以来请教各位
Golang Go语言中 bytes.Buffer 包的疑惑
更多关于Golang Go语言中 bytes.Buffer 包的疑惑的实战教程也可以访问 https://www.itying.com/category-94-b0.html
fmt 提供了 String()这个接口,bytes.Buffer 实现了这个接口,Println 时就会自动调用
更多关于Golang Go语言中 bytes.Buffer 包的疑惑的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
One of the most ubiquitous interfaces is Stringer defined by the fmt package.
type Stringer interface {
String() string
}
A Stringer is a type that can describe itself as a string. The fmt package (and many others) look for this interface to print values.
感谢回答
thanks