Hello, I’m a bit new on Go, please I need your help, I’m working for a year in a socket programming wrote on PHP, and was working, but I heard a good comments about “Go” and I’ve decide migrated my project on this languaje, but I have this problem that I’ve describe…
I need to capture a lot of data in few ms, 0,8 ms and each frame ethernet has 1358 Bytes, and also I need to open the header of this packet and analize it…
each frame has 7 headers , diferents all, and I need to store this data into a “Map Key array”, but when I try to analyze all data they send me a problem with read and write map Key data
######################################################
2017/07/17 16:57:48 Old map: map[1fff:0 4011:a 4001:c 4000:c]
2017/07/17 16:57:48 Old map: map[4001:c 4000:c 1fff:0 4011:a]
fatal error: concurrent map read and map write
goroutine 820 [running]:
runtime.throw(0x1112bc, 0x21)
/usr/local/go/src/runtime/panic.go:596 +0x70 fp=0x1053095c sp=0x10530950
runtime.mapaccess2(0xf43a8, 0x104703e0, 0x106d12a0, 0x106d12b8, 0x104d4058)
/usr/local/go/src/runtime/hashmap.go:377 +0x248 fp=0x1053097c sp=0x1053095c
reflect.mapaccess(0xf43a8, 0x104703e0, 0x106d12a0, 0x104703e0)
/usr/local/go/src/runtime/hashmap.go:1128 +0x2c fp=0x10530994 sp=0x1053097c
reflect.Value.MapIndex(0xf43a8, 0x104703e0, 0x15, 0xf35b0, 0x106d12a0, 0x98, 0xf35b0, 0x106d12b8, 0x98)
/usr/local/go/src/reflect/value.go:1052 +0xc8 fp=0x105309d4 sp=0x10530994
fmt.(*pp).printValue(0x104e4000, 0xf43a8, 0x104703e0, 0x15, 0x76, 0x0)
/usr/local/go/src/fmt/print.go:752 +0xd0c fp=0x10530acc sp=0x105309d4
fmt.(*pp).printArg(0x104e4000, 0xf43a8, 0x104703e0, 0x76)
/usr/local/go/src/fmt/print.go:682 +0x1c4 fp=0x10530b0c sp=0x10530acc
fmt.(*pp).doPrintln(0x104e4000, 0x10530fd4, 0x2, 0x2)
/usr/local/go/src/fmt/print.go:1138 +0x8c fp=0x10530b48 sp=0x10530b0c
fmt.Sprintln(0x10530fd4, 0x2, 0x2, 0x106d1280, 0x10530e9c)
/usr/local/go/src/fmt/print.go:264 +0x3c fp=0x10530b70 sp=0x10530b48
##############################################################
I’ve read a lot of post relationship with map key on Go, and for Go solved used a library called sync, but when I use that the program working, but didn’t analyze any data is like blocked all key maps in my program,
I’ve use Map Key sync as this way:
"sync"
var (
PidCCerr = make(map[string]string)
PidCCerrLock = &sync.RWMutex{}
PidCCerrSequence int
)
// New pids value
var PidNew = map[string]string{}
and the I generate a switch for read all frame and get all headers,
if a heders is the same as old header , I need to remplace it with a new data , for this reason I delete a value of key map and re write it…
################################################
func CompareKeyValue(PIDOld string) {
//Extract Value from Map[key] , if value exists??
CCerr, ok := PidCCerr[PIDOld]
if ok {
log.Println("*********************** " )
log.Println("Found Ccerr Old " ,CCerr )
log.Println("Found PID " ,PIDOld )
log.Println("*********************** " )
//log.Println("Pid New: ", CCerrNew)
//fmt.Println("Substract: ", int (CCerrNew - value)
} else {
log.Println("key not found")
}
}
####################################################
if has the same value I’ ve delete it
switch i {
case 0:
log.Println("############################################")
log.Println("############################################")
log.Println("Begin ")
log.Println("Number i = ",i)
SplitLine := strings.Split(SplitData[i],"|")
Split := SplitLine[0]
PID,Scrambled,CCerr := GetHeaderTS(Split,8,16)
log.Println("Lock and Unlock ")
PidCCerrLock.RLock()
defer PidCCerrLock.RUnlock()
CompareKeyValue(PID)
PidCCerrLock.RLock()
//time.Sleep(1 * time.Millisecond)
PidCCerrLock.RLock()
defer PidCCerrLock.RUnlock()
log.Println("Old map:", PidCCerr)
delete(PidCCerr, PID);
//time.Sleep(1 * time.Millisecond)
PidCCerrLock.Lock()
defer PidCCerrLock.Unlock()
PidCCerr[PID] = CCerr
log.Println("New map:", PidCCerr)
//Header := Split[8:16]
//PidCCerrLock.RUnlock()
log.Println("Pid 0 data Hexa ",PID)
log.Println("is Scrambled?? ",Scrambled)
log.Println("CCerr => ",CCerr)
i+=11
log.Println("END ")
case 11:
//log.Println("Number i = ",i)
log.Println("############################################")
log.Println("Begin ")
log.Println("map:", PidCCerr)
SplitLine := strings.Split(SplitData[i],"|")
Split := SplitLine[0]
f = len(Split) - 8
l = f + 8
PID,Scrambled,CCerr := GetHeaderTS(Split,f,l)
log.Println("Lock and Unlock ")
PidCCerrLock.RLock()
defer PidCCerrLock.RUnlock()
CompareKeyValue(PID)
PidCCerrLock.RLock()
PidCCerrLock.RLock()
defer PidCCerrLock.RUnlock()
log.Println("Old map:", PidCCerr)
delete(PidCCerr, PID);
PidCCerrLock.Lock()
defer PidCCerrLock.Unlock()
PidCCerr[PID] = CCerr
log.Println("New map:", PidCCerr)
//PidCCerrLock.RUnlock()
log.Println("Pid 1 data Hexa ",PID)
log.Println("is Scrambled?? ",Scrambled)
log.Println("CCerr => ",CCerr)
i+=12
log.Println("END ")
log.Println("############################################")
what I did wrong with sync library?, why my program can’t read and write any data on my array map key strings.?, please help me to fix that…