stdio.go
```go
package io
import (
"fmt"
"os"
)
type FileReader struct {
FileName string
}
func (file *FileReader) Read(buf []byte) (length int, err error) {
fd, err := os.Open(file.FileName)
fd.Read(buf)
return len(buf), err
}
```
stdio_test.go
```go
package io
import (
"fmt"
"io"
"testing"
)
func TestFileReader(t *testing.T) {
var fileReader io.Reader = &FileReader{"testfile.md"}
var buf []byte = make([]byte, 1024)
fmt.Printf("value buf [] byte after passed addr = %p \n", &buf)
length, err := fileReader.Read(buf)
if err == nil {
fmt.Printf("length==%d,err==%v,file==%s \n", length, err, string(buf))
} else {
fmt.Printf("length==%d,err==%v,file==%s \n", length, err, string(buf))
}
}
```
builtin.go 关于 make的说明 ,返回的是一个value 类型而不是一个指针
```go
// The make built-in function allocates and initializes an object of type
// slice, map, or chan (only). Like new, the first argument is a type, not a
// value. Unlike new, make's return type is the same as the type of its
// argument, not a pointer to it.
func make(t Type, size ...IntegerType) Type
```
输出结果:
```
value buf [] byte addr = 0xc00005a420
value buf [] byte after passed addr = 0xc00005a460
length==1024,err==<nil>,file==# this is Test file
```
而golang 里面 对于 value 类型的值在函数调用时,会传递这个的值的副本 指针的值会变(输出会说明)
那么我想问的是最终buf里面会什么会有值 就是文档里面的文本 。。不应该是全0么?
go是怎么操作的?
有疑问加站长微信联系(非本文作者)