>As a total beginner (learning programming by myself since 2 or 3 years), i am always asking myself, how often "little" things like race conditions break something in production
Rookie mistake (and a shoot-yourself-in-the-foot-at-2-am) mistake coming up:
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
fmt.Printf("i= %d\n", i)
wg.Done()
}()
}
wg.Wait()
On my multi-core computer (though not on playground), len(intArray) could be as low as 500!
Why?
Because a = append isn't atomic.
Go, which is so pedantic about "stupid" mistakes (including something, changing your mind, and not "unincluding" it), didn't catch this. Not an error and no warning.
But the worst part is that when you loop until 10, it works. You can unit test it, integrate test it, and have it break at any point in time.
Rookie mistake (and a shoot-yourself-in-the-foot-at-2-am) mistake coming up:
package main
import ( "fmt" "sync" )
func main() { var wg sync.WaitGroup
for i := 0; i < 10; i++ { wg.Add(1)
}https://play.golang.org/p/XDzFq_XK_1
And what about this code:
package main
import "fmt"
func main() {
var intArray []int for i := 0; i < 1000; i++ {
}https://play.golang.org/p/UuI4uESZ_f
If you don't care if intArray is in the proper order, you might do something like this:
package main
import ( "fmt" "sync" )
func main() { var wg sync.WaitGroup var intArray []int
for i := 0; i < 1000; i++ { wg.Add(1)
}What's wrong?
On my multi-core computer (though not on playground), len(intArray) could be as low as 500!
Why?
Because a = append isn't atomic.
Go, which is so pedantic about "stupid" mistakes (including something, changing your mind, and not "unincluding" it), didn't catch this. Not an error and no warning.
But the worst part is that when you loop until 10, it works. You can unit test it, integrate test it, and have it break at any point in time.