douxihui8270 2018-06-06 19:21
浏览 35
已采纳

os.Read()工作如何? Golang

Why if I print bs, before calling Read(), it prints nothing, but after the call file.Read(bs), it shows the inside of test.txt file. Unless bs is only argument, how Read() can Change it?

package main
import (
"os"
"fmt"
)

func main() {
    file , err := os.Open("test.txt")
if err == nil {
} else {

}
stat , _ := file.Stat()


bs := make([]byte, stat.Size())
fmt.Println(string(bs))
bsf ,err := file.Read(bs)

if err != nil{
    fmt.Println(err)
    fmt.Println(bsf)
}
fmt.Println(string(bs))


} 

Output:

(Line1)                                           
(Line2)hi, This is Example text in test.txt file.
  • 写回答

1条回答 默认 最新

  • dongqiang1226 2018-06-14 06:57
    关注

    Unless bs is only argument, how Read() can Change it?

    It seems that you may be missing basic knowledge about programming languages in general. There are different kind of "values". There are pointers (or references) and there are the "usual values".

    For example:

    package main
    
    import (
        "fmt"
    )
    
    func changeIt(p *int) {
        *p = 9
    }
    
    func main() {
        a := 1
        fmt.Println(a)
        changeIt(&a)
        fmt.Println(a)
    }
    

    It'll print 1 9 not 1 1. *int is not an integer, but a pointer to an integer. A pointer is a value that points (references) another value. If you have a value of type pointer you get the actual value that the pointer points to by using * (which is called dereferencing):

    func main() {
        a := 1
        b := &a
        fmt.Println(b, *b)
    }
    

    b is a pointer (of type *int) that points to a. The println will print the location of a followed by the value of a which is usually something like uhm 0x10414020 1. We can also modify the value a pointer points to by using *p = ...:

    func main() {
        a := 1
        b := &a
        *b = 9
        fmt.Println(b, *b, a)
    }
    

    which will print 0x10414020 9 9.

    Now, []byte is a slice... slices are like pointers. When you do

    func changeIt(buf []byte) {
        buf[0] = 10
    }
    
    func main() {
        data := []byte{1,2,3}
        changeIt(data)
        fmt.Println(data)
    }
    

    You're not actually passing the values [1 2 3] to changeIt but a pointer to those values. Thus here the println will show [10 2 3]. Compare this to:

    func changeIt(buf [3]byte) {
        buf[0] = 10
    }
    
    func main() {
        data := [3]byte{1,2,3}
        changeIt(data)
        fmt.Println(data)
    }
    

    Which will print [1 2 3] and it will pass the values [1 2 3] and not a pointer so changeIt essentially works on a copy and the buf[0] = 10 has no effect. Remember: [n]T is an array, []T is a slice. [n]T is a "raw value" and []T is a "pointer value".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”