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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算