Golang Channel string Signal garbled

Source: Internet
Author: User
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)
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.