Hello.Help me please.
I have got MAP with values,and I need change one random value every 200 milliseconds.
The code is works, but when I release the program with the -race parameter, it is produces the following errors:
C:\gopath\src\Taxi>go run -race Taxi.go
WARNING: DATA RACE
Read at 0x00c042076960 by goroutine 8:
runtime.mapaccess1_fast64()
C:/Go/src/runtime/hashmap_fast.go:110 +0x0
main.cabbie()
C:/gopath/src/Taxi/Taxi.go:95 +0x15c
net/http.HandlerFunc.ServeHTTP()
C:/Go/src/net/http/server.go:1942 +0x58
github.com/gorilla/mux.(*Router).ServeHTTP()
C:/Go/src/github.com/gorilla/mux/mux.go:114 +0x18e
net/http.serverHandler.ServeHTTP()
C:/Go/src/net/http/server.go:2568 +0xc3
net/http.(*conn).serve()
C:/Go/src/net/http/server.go:1825 +0x721
Previous write at 0x00c042076960 by goroutine 6:
runtime.mapassign()
C:/Go/src/runtime/hashmap.go:485 +0x0
main.replacement()
C:/gopath/src/Taxi/Taxi.go:76 +0x1f9
Goroutine 8 (running) created at:
net/http.(*Server).Serve()
C:/Go/src/net/http/server.go:2668 +0x366
net/http.(*Server).ListenAndServe()
C:/Go/src/net/http/server.go:2585 +0xe7
net/http.ListenAndServe()
C:/Go/src/net/http/server.go:2787 +0xf5
main.main()
C:/gopath/src/Taxi/Taxi.go:87 +0x2ca
Goroutine 6 (running) created at:
main.main()
C:/gopath/src/Taxi/Taxi.go:83 +0x63
Program code:
//активные заявки
var request map[int]string = make(map[int]string)
//map с символами,для создания заявок
var symbol map[int]string = map[int]string{0: “a”, 1: “b”, 2: “c”, 3: “d”, 4: “e”,
5: “f”, 6: “j”, 7: “h”, 8: “i”, 9: “g”,
10: “k”, 11: “l”, 12: “m”, 13: “n”, 14: “o”,
15: “p”, 16: “q”, 17: “r”, 18: “s”, 19: “t”,
20: “u”, 21: “v”, 22: “w”, 23: “x”, 24: “y”,
25: “z”}
func replacement() { //функция замещения заявок раз в 200мс
for {
var number int = rand.Intn(sizeRequest)
firstPart = rand.Intn(sizeSymbol)
secondPart = rand.Intn(sizeSymbol)
muxLock.Lock()
var x string = symbol[firstPart] + symbol[secondPart]
request[number] = x
muxLock.Unlock()
time.Sleep(time.Millisecond * 200)
}
}
func mapFilling() { //функция начального заполнения мапа заявками
rand.Seed(time.Now().UTC().UnixNano())
for i := 0; i < sizeRequest; i++ {
firstPart = rand.Intn(sizeSymbol)
secondPart = rand.Intn(sizeSymbol)
muxLock.Lock()
request[i] = symbol[firstPart] + symbol[secondPart]
muxLock.Unlock()
}
}
func main() {
mapFilling()
go replacement()
}