Thanks for the hint.
I run the test with "go test -race -run TestRace", and* it reports no
error.*
Only one goroutine is listening on "RequestSucessChannel" and
"ClearStatsChannel", *hence only one goroutine is reading / writing to the
map.*
Here is my test code.
package boomer
import (
"time"
"testing"
)
func TestRace(t *testing.T) {
for i:=0; i < 1000; i++ {
go func(){
for {
time.Sleep(10 * time.Millisecond)
RequestSuccessChannel <- &RequestSuccess{
requestType: "test",
name: "test",
responseTime: float64(0.1),
responseLength: int64(100),
}
}
}()
}
go func(){
for {
time.Sleep(100 * time.Millisecond)
ClearStatsChannel <- true
}
}()
time.Sleep(1000 * time.Second)
}
$ go version
go version go1.7.3 linux/amd64
在 2017年3月3日星期五 UTC+8下午10:30:15,Ian Lance Taylor写道:
>
> On Fri, Mar 3, 2017 at 2:34 AM, <[email protected] <javascript:>>
> wrote:
> > Hey guys,
> >
> > Here is my code,
> >
> https://github.com/myzhan/boomer/blob/dc6893a72c2b32c27ff6d7646a55b135493ba7e1/stats.go#L33
>
> >
> > I'm trying to figure out why I got nil from "this.Entries(a map)", and
> I'm
> > really confused.
> >
> > I hava checked if the key exists, and if not, I put a new key in
> > "this.Entries".
> >
> > If "clearAll" is called, "this.Entries" will be repalced by a new map.
> >
> > "init()" starts a goroutine to do all the things, get a entry from
> > "this.Entries" or call "clearAll".
> >
> > Is it a concurrency issue?
>
> I haven't looked at your code, but have you run it with the race
> detector? https://blog.golang.org/race-detector .
>
> Ian
>
--
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.