While goroutines are cheap, they aren't free. There is some, but little, overhead in creating them.
In your first program, the value of i
is preserved into the goroutines because you're passing it in as an argument. (Each goroutine gets its own copy of i
's value at that moment.)
In your second program, the value of i
is already 3 before the first goroutine has started. Remember that goroutines share the same memory space in a Go program, so in this case, each goroutine is looking at the same i
when it prints it out.