The Art of How To Program A Computer — Concurrently
Primanata Ardhitama
161

variable inside go anonymous function seems take last loop i value because its outside of it context.

Go lang code should be like this to get correct value:

package main

import (
“fmt”
“time”
)

func main() {
 for i := 0; i < 10; i++ {
 go func(j int) {
 fmt.Println(j)
 }(i)
 }
 time.Sleep(1 * time.Second)
}

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.