teivah / 100-go-mistakes

📖 100 Go Mistakes and How to Avoid Them
https://100go.co/
Other
6.82k stars 417 forks source link

Correction needed in Data types chapter #94

Open mohammadhsn opened 3 months ago

mohammadhsn commented 3 months ago

Thank you for such an incredible book. While reading the fourth season (control structures), I noticed a small mistake. In section 4.2.2 (Arrays), there are three examples demonstrating range loop behavior. The second example is:

a := [3]int{0, 1, 2}
    for i := range a {
        a[2] = 10
        if i == 2 {
            fmt.Printf(a[2])
        }
    }

The description says:

Because we access the original array, this code prints 2 instead of 10.

However, if you run the code, the number 10 will be shown in the console, which makes sense.

ngoctd314 commented 2 months ago

original source: a := [3]int{0, 1, 2} for i, v := range a { a[2] = 10 if i == 2 { fmt.Println(v) } } a[2] is different with v