问个问题 文件读写的,buf [] byte 里面不应该有值啊。。

weixin_38092506 2019-09-20 10:52:02
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是怎么操作的?
...全文
33 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38093910 2019-09-20
  • 打赏
  • 举报
回复
放我没说....不是数组。。。

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧