Search code examples
gogo-packages

os.Read() How is work? 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.

Solution

  • 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".