没有并发时的并发映射读写

The following go play example shows in a simplistic way what I have defined. I am passing a map as a copied value to a function (not a reference) as well as there is a recursion in my function which I assume passes by value as well.

https://play.golang.org/p/na6y6Wih4M

// this function has no write operations to dataMap, just reads
// dataMap, in fact, has no write operations since it was copied
func findParentAncestors(ID int, dataMap map[int]Data) []Data {
    results := []Data{}
    if _, ok := dataMap[ID]; ok {
        if parentData, ok := dataMap[dataMap[ID].ParentID]; ok {
            results = append(results, parentData)
            // recursion
            results = append(results, findParentAncestors(parentData.ID, dataMap)...)
        }
    }
    return results
}

PROBLEM: somehow along my program execution, which involves much more data than this example (obviusly), an error "fatal error: concurrent map read and map write" points function findParentAncestors():

main.findParentAncestors(0x39e3, 0xc82013ac90, 0x0, 0x0, 0x0)
    /opt/test/src/test.go:17 +0xa6 fp=0xc820269fb8 sp=0xc820269bd0
main.findParentAncestors(0x5d25, 0xc82013ac90, 0x0, 0x0, 0x0)
    /opt/test/src/test.go:21 +0x239 fp=0xc82026a3a0 sp=0xc820269fb8

From your example, https://play.golang.org/p/na6y6Wih4M:

// the orignalMap is defined elsewhere in the program (here represented)
originalMap := map[int]Data{}
originalMap[0] = Data{ID: 0, ParentID: -1, Name: "zero"}
originalMap[1] = Data{ID: 1, ParentID: 0, Name: "one"}
originalMap[2] = Data{ID: 2, ParentID: 1, Name: "two"}
// copies the original map from a global location (here represented)
copiedMap := originalMap
// identifies ancestors unsing the copied map
parents := findParentAncestors(2, copiedMap)

This is a misnomer, copiedMap := originalMap, you are not copying the map.

In Go all arguments are passed by value. It's equivalent to assigning each argument to each parameter. For a map, assignment, copiedMap := originalMap, or passing by value, findParentAncestors(2, copiedMap), copies the map descriptor which is a pointer to the map descriptor struct which contains a pointer to the map key-value data. Obviously you have a potential race condition if there are any writes to the map.

You are using go version go1.6.3 linux/amd64, so run the race detector.

Go 1.6 Release Notes

Runtime

The runtime has added lightweight, best-effort detection of concurrent misuse of maps. As always, if one goroutine is writing to a map, no other goroutine should be reading or writing the map concurrently. If the runtime detects this condition, it prints a diagnosis and crashes the program. The best way to find out more about the problem is to run the program under the race detector, which will more reliably identify the race and give more detail.

Command go

Compile packages and dependencies

-race
  enable data race detection.
  Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.

Also, compile and run your program using Go 1.8, the current release of Go, which significantly improves concurrent map misuse.

Go 1.8 Release Notes

Concurrent Map Misuse

In Go 1.6, the runtime added lightweight, best-effort detection of concurrent misuse of maps. This release improves that detector with support for detecting programs that concurrently write to and iterate over a map.

As always, if one goroutine is writing to a map, no other goroutine should be reading (which includes iterating) or writing the map concurrently. If the runtime detects this condition, it prints a diagnosis and crashes the program. The best way to find out more about the problem is to run the program under the race detector, which will more reliably identify the race and give more detail.