News for hackers. Stuff that matters.
Re:Data Race Patterns in Go by aaronbee on Friday June 10, 2022 @06:34PM (#31700109)
I believe they made a mistake with that example. It doesn't look unsafe to me because the myResults sliced passed to the goroutine is not used. Or perhaps the racy part was left out of their snippet.

Below is what might be what they have meant. This code snippet is racy because an unsafe read of myResults is done to pass it to the goroutine and then that version of myResults is passed to safeAppend:

  func ProcessAll(uuids []string) {
    var myResults []string
    var mutex sync.Mutex
    safeAppend := func(results []string, res string) {
      mutex.Lock()
      myResults = append(myResults, res)
      mutex.Unlock()
    }

    for _, uuid := range uuids {
      go func(id string, results []string) {
        res := Foo(id)
        safeAppend(myResults, id)
      }(uuid, myResults) # <<< unsafe read of myResults
    } 
  }
EDIT: Formatting and clarity
Re:Data Race Patterns in Go by collinvandyck76 on Friday June 10, 2022 @10:27PM (#31701419)
Yea I think your read on the intent here is correct. I re-read that part five times wondering what I wasn't getting.