在这段代码中。
package main
import (
"fmt"
"io"
"os"
)
type byteCounter struct {
w io.Writer
count int64
}
func countingWriter(w io.Writer) (io.Writer, *int64) {
var bc = byteCounter{w, 0}
// here why is &bc a "io.Writer" and not bc.w cause bc.count is int64
return &bc, &bc.count
}
func (bc *byteCounter) Write(p []byte) (n int, err error) {
ncount, err := bc.w.Write(p)
bc.count += int64(ncount)
return
}
func main() {
bc, count := countingWriter(os.Stdout)
bc.Write([]byte("Hello World"))
fmt.Println("\n", *count)
}
为什么是&bc“io.Writer”类型而不是bc.w我感到困惑,因为我看到 bc.count的是int64.
动漫人物
相关分类