二维切片的线程安全性,其中每个线程在第一维上都有自己的索引

I am trying to ultimately end up with a 2D slice where the first dimension is equal to the number of go-routines used and the second dimension is a list of structs that each go-routine is responsible for writing to.

Type:

[][]*Node

Each go-routine will be given an index for the first dimension in the 2d slice and will only append to the list at it's index.

My first instinct was to think this was ok, but if two go-routines simultaneously need to append to their respective lists, this means I am concurrently writing to the first dimension slice.

I was thinking pointers to the second list instead of using raw lists might mitigate this concurrent write... something like:

workCollector := make([]*[]*Node, 5)
for i ; i < 5; i++ {
    go.Process(i)
}

func Process(threadNum int){
    localList := workCollector[threadNum]
    *localList = append(*localList, NewNode(UUID.()))
}

Is this enough to allow each go-routine to append to their own list or should the append happen in a mutex?

Provided that your number of goroutines is constant and each goroutine is only changing its "own" data, then your original approach is thread-safe.

My first instinct was to think this was ok, but if two go-routines simultaneously need to append to their respective lists, this means I am concurrently writing to the first dimension slice.

You may be concurrently writing to that slice but you're updating different indices in the backing array and provided you don't need to grow that slice, the backing array won't move.