This is a creation in Article, where the information may have evolved or changed.
Cause Golang code is allowed to use the same variable name in the same for select code structure, which will cause the runtime Chan sent the content garbled phenomenon, the garbled rate is about 98%, so this is a pit, I hope we do not repeat the pit. Here is the code description:
var delhubs chan string = Make (Chan string)
var Delhub chan string = Make (Chan string)
Go func () {
Deller.delhub <-Key
}()
Garbled code, I defined and used two identically named variables in two different Chan, which is where the garbled problem is.
Func Delchan () {
for {
Select {
Case Ukey, OK: = <-deller.delhubs:
If OK {
HS, Err: = Hubstartwith (Ukey)
If err! = Nil {
Break
}
For _, V: = Range HS {
Ukeyhid: = Ukey + ":" + StrConv. Formatint (V.id, 10)
Del (Ukeyhid)
Go Deldos (Ukeyhid)
}
}
Case Ukeyhid, OK: = <-deller.delhub:
If OK {
Del (Ukeyhid)
Go Deldos (Ukeyhid)
}
}
}
}
Solution, it is easy to change the name of each variable to
func Delchan () {
for {
Select {
Case ukey, OK: = <-deller.delhubs:
If OK {
HS, err: = Hubstartwith (ukey)
I F Err! = Nil {
Break
}
For _, V: = Range HS {
Delhubs: = Ukey + ":" + StrConv. Formatint (V.id, Ten)
del (delhubs)
Go Deldos (del Hubs)
}
}
Case Delhub, OK: = <-deller.delhub: if OK {
del (delhub)
Go Deldos (Delhub)
}
}
}
}