func TestAppend(t *testing.T){ x := make([]string, 0, 6)
wg := sync.WaitGroup{} wg.Add(2) go func() { defer wg.Done() y := append(x, "hello", "world") t.Log(cap(y), len(y)) }() go func() { defer wg.Done() z := append(x, "goodbye", "bob") t.Log(cap(z), len(z)) }() wg.Wait() }
我们再次做测试,看一下测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ go test --race ================== WARNING: DATA RACE Write at 0x00c0000a4120 by goroutine 8: test/test12.TestAppend.func2() /Users/shuai/go/src/test/test12/aaa_test.go:20 +0xbe
Previous write at 0x00c0000a4120 by goroutine 7: // ... --- FAIL: TestAppend (0.00s) aaa_test.go:16: 62 aaa_test.go:21: 62 testing.go:809: race detected during execution of test FAIL exit status 1 FAIL test/test12 0.012s
func TestAppend(t *testing.T){ x := make([]string, 0, 6)
wg := sync.WaitGroup{} wg.Add(2) go func() { defer wg.Done() y := append(x, "hello", "world") t.Log(cap(y), len(y)) }() go func() { defer wg.Done() z := append(x, "goodbye", "bob") t.Log(cap(z), len(z)) }() wg.Wait() }
我们再次做测试,看一下测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ go test --race ================== WARNING: DATA RACE Write at 0x00c0000a4120 by goroutine 8: test/test12.TestAppend.func2() /Users/shuai/go/src/test/test12/aaa_test.go:20 +0xbe
Previous write at 0x00c0000a4120 by goroutine 7: // ... --- FAIL: TestAppend (0.00s) aaa_test.go:16: 62 aaa_test.go:21: 62 testing.go:809: race detected during execution of test FAIL exit status 1 FAIL test/test12 0.012s