4 ms·
> String is a pointer/slice type under the hood, even if you don't see the asterisk. So when you are passing v, you are passing a reference to a slice which get
by jhgb 5y ago
> String is a pointer/slice type under the hood, even if you don't see the asterisk. So when you are passing v, you are passing a reference to a slice which gets updated by the loop. Since the goroutines can run in parallel, you print whatever v is pointing to at the time.
You'll have the same problem with a simple integer (for example, as in https://go.dev/play/p/uqvRoO3ynZt https://go.dev/play/p/uqvRoO3ynZt). Nothing to do with v being a reference to something.