Can't say for sure without seeing additional code. But it looks like you have a race condition.
It appears that the fmt.Println() is trying to print the map at the same time that you are modifying it. Your locks may prevent ForEachRemov() and FindAndRemove() from accessing the map at the same time, but the fmt.Println() is done without any protection. So it could be reading the map (for printing) while either ForEachRemov() or FindAndRemove() are modifying (writing to) the map. On Sunday, March 17, 2019 at 7:28:10 PM UTC-4, [email protected] wrote: > > Hi, > I got a panic when i run my test code > my go version is 'go version go1.12 linux/amd64' > > panic: reflect: call of reflect.Value.Type on zero Value > > goroutine 6 [running]: > reflect.Value.Type(0x0, 0x0, 0x0, 0x4db0c0, 0xc0000183d0) > /usr/local/go/src/reflect/value.go:1813 +0x169 > internal/fmtsort.compare(0x0, 0x0, 0x0, 0x4e34c0, 0xc0000b8110, 0xb4, 0x1) > /usr/local/go/src/internal/fmtsort/sort.go:76 +0x5a > internal/fmtsort.(*SortedMap).Less(0xc0000c2000, 0x9, 0x8, 0x100c0000b8100) > /usr/local/go/src/internal/fmtsort/sort.go:27 +0x87 > sort.insertionSort(0x525540, 0xc0000c2000, 0x0, 0xa) > /usr/local/go/src/sort/sort.go:27 +0xab > sort.stable(0x525540, 0xc0000c2000, 0xa) > /usr/local/go/src/sort/sort.go:368 +0x86 > sort.Stable(0x525540, 0xc0000c2000) > /usr/local/go/src/sort/sort.go:357 +0x53 > internal/fmtsort.Sort(0x4e3a00, 0xc000012678, 0x1b5, 0x20) > /usr/local/go/src/internal/fmtsort/sort.go:67 +0x2e3 > fmt.(*pp).printValue(0xc0000b2000, 0x4e3a00, 0xc000012678, 0x1b5, 0x76, > 0x2) > /usr/local/go/src/fmt/print.go:759 +0xd1d > fmt.(*pp).printValue(0xc0000b2000, 0x4eb220, 0xc000012670, 0x199, > 0xc000000076, 0x1) > /usr/local/go/src/fmt/print.go:796 +0x1b52 > fmt.(*pp).printValue(0xc0000b2000, 0x4f89e0, 0xc000012670, 0x16, 0x76, 0x0) > /usr/local/go/src/fmt/print.go:866 +0x199f > fmt.(*pp).printArg(0xc0000b2000, 0x4f89e0, 0xc000012670, 0x76) > /usr/local/go/src/fmt/print.go:702 +0x2ba > fmt.(*pp).doPrintln(0xc0000b2000, 0xc0000a9fa0, 0x2, 0x2) > /usr/local/go/src/fmt/print.go:1159 +0xb2 > fmt.Fprintln(0x524b60, 0xc000010018, 0xc0000387a0, 0x2, 0x2, 0x0, 0x0, 0x0) > /usr/local/go/src/fmt/print.go:265 +0x58 > fmt.Println(...) > /usr/local/go/src/fmt/print.go:275 > main.testMapLock.func1(0xc000012670, 0xc000018350) > /home/lbs/go/src/demo/test/testmap/testmap.go:78 +0x95 > created by main.testMapLock > /home/lbs/go/src/demo/test/testmap/testmap.go:77 +0xfa > > > Piece of my code: > //ForEach range all the node in map and call the handler and then delete > the node if the handler return true > func (em *EMSmap) ForEachRemove ( h DoEachNode ,userData interface{}){ > em.m.Lock() > defer em.m.Unlock() > for key,value := range em.emap{ > userData := userData > if b := h(key, value, userData);b{ > delete(em.emap, key) > } > } > } > > //FindAndRemove store the node in a temporary var and then delete it from > the map and return the node which stored in the temporary var > func (em *EMSmap) FindAndRemove (key interface{}) interface{}{ > em.m.Lock() > defer em.m.Unlock() > if _,ok := em.emap[key]; !ok{ > return nil > } > value := em.emap[key] > delete(em.emap, key) > return value > } > > > func testMapLock(){ > var callMap *emsutil.EMSmap = context.CallMap > var wgt sync.WaitGroup > for i := 0; i < 10; i++{ > callMap.Put(i, i) > } > wgt.Add(2) > go func(){ > fmt.Println("ForEachRemove",callMap) > time.Sleep(time.Duration(10)*time.Millisecond) > callMap.ForEachRemove(func (key interface{}, value interface{}, userData > interface{})bool{ > fmt.Println("delete by ForEachRemove",key,value) > time.Sleep(time.Duration(2)*time.Millisecond) > return true}, nil) > wgt.Done() > }() > go func(){ > fmt.Println("FindAndRemove",callMap) > for i := 0; i < 10; i++{ > val := callMap.FindAndRemove(i) > fmt.Println("delete by for FindAndRemove",val) > time.Sleep(time.Duration(2)*time.Millisecond) > } > wgt.Done() > }() > wgt.Wait() > wg.Done() > } > -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
