It's not clear from your example why you're writing to the "response" variable in the first place. One simple solution would be to create the test server within the subtest, then there's no sharing at all.
There are only a few ways to fix a data race and they don't really depend on the specifics of the application: * Stop performing concurrent reads and writes (that is, stop racing) * Protect the racy data with a mutex or use atomics * Communicate the data over a channel rather than using shared state across goroutines On Friday, January 31, 2025 at 10:22:38 AM UTC-5 Chris Burkert wrote: > I meanwhile found a way by using a sync.Map for the responses like this: > https://go.dev/play/p/KMZ7v63Ht6t > thanks - Chris > > Am Fr., 31. Jan. 2025 um 09:06 Uhr schrieb Chris Burkert < > burker...@gmail.com>: > >> Dear all, >> >> I have the following test: https://go.dev/play/p/fQgnvbomlhz (you have >> to run it locally as it does not work in the playground and it expects >> /usr/bin/curl as I was not able to reproduce the race using http.Get()). >> >> When I run this with *go test -race -v -count=1 .* I get the following >> race: >> >> WARNING: DATA RACE >> Read at 0x00c0000922e0 by goroutine 10: >> race_test.TestRace.func1() >> /Users/D055539/race/race_test.go:16 +0x130 >> ... >> Previous write at 0x00c0000922e0 by goroutine 9: >> race_test.TestRace.func2() >> /Users/D055539/race/race_test.go:34 +0x44 >> >> This is because I write variable response in each test case function and >> read the same variable in the handler function. >> >> My goal is to define the response of the handler for each test case in >> the table. How can I achieve that without a race? >> >> thanks - Chris >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/a03f0927-2fae-4a9f-99c6-6c505ac45f18n%40googlegroups.com.