Agreed. Depending on your application, maybe using sync.Map would be a 
drop-in replacement, especially if this is a sparse set.

On Wednesday, 9 February 2022 at 08:35:09 UTC axel.wa...@googlemail.com 
wrote:

> I would fix it.
>
> One particular reason to do so is that, even if it works fine, it adds 
> noise to the race detector. If someone uses your package and they test 
> *their* code using the race detector, they will get spurious race detector 
> messages. The same goes, of course, for your own tests as well. If race 
> detector warnings are expected, you can't really use it to find *other* 
> races.
>
> To me, this alone is a good enough reason to fix the warning.
>
> On Wed, Feb 9, 2022 at 9:23 AM Pelen Li <penglo...@gmail.com> wrote:
>
>> I want to set a value with the index of the slice. I don't really care if 
>> there are multiple goroutines cover the value with each other, because the 
>> value is same.
>>
>> Can i just ignore this DataRace Warning? I don't know if this will cause 
>> panic.
>>
>> *Here's my example:*
>> I defined a structure with slice, and a Add() function for it. sample 
>> like this:
>> ```go
>> package test_slice
>>
>> type SliceObj struct {
>>     set []uint
>> }
>>
>> func New(length int64) *SliceObj {
>>     return &SliceObj{
>>         set: make([]uint, length),
>>     }
>> }
>>
>> func (b *SliceObj) Add(i uint) {
>>     b.set[i] = i
>> }
>> ```
>>
>> And then i make a main file to test it, like this:
>> ```go
>> package main
>>
>> import (
>>     "time"
>>
>>     "test_slice"
>> )
>>
>> func main() {
>>     s := test_slice.New(1000000)
>>     go func() {
>>         s.Add(10)
>>     }()
>>     s.Add(10)
>>
>>     time.Sleep(3 * time.Second)
>> }
>> ```
>>
>> *And data race is detected:*
>> *(*I know the reason of this warning, but I don't know if I can ignore it
>> *)*
>> ==================
>> WARNING: DATA RACE
>> Write at 0x00c000180050 by goroutine 18:
>>   test_slice.(*SliceObj).Add()
>>       test_slice.go:27 +0x68
>>   main.main.func1()
>>       test.go:25 +0x36
>>
>> Previous write at 0x00c000180050 by main goroutine:
>>   test_slice.(*SliceObj).Add()
>>       test_slice.go:27 +0xfd
>>   main.main()
>>       test.go:27 +0xcb
>>
>> Goroutine 18 (running) created at:
>>   main.main()
>>       test.go:24 +0xca
>> ==================
>> Found 1 data race(s)
>> exit status 66
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/0b0b0178-987f-42bc-b8ef-47516764ffcen%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/0b0b0178-987f-42bc-b8ef-47516764ffcen%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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 on the web visit 
https://groups.google.com/d/msgid/golang-nuts/602b7dec-dd55-454e-9581-4ac96a1252cfn%40googlegroups.com.

Reply via email to