change channel reads to use 'for v := range ch {}' idiom

This commit is contained in:
Alex Bramley 2009-12-19 15:19:28 +00:00
parent 7f6c8fc232
commit af8dfdb6f3
2 changed files with 11 additions and 31 deletions

View File

@ -41,11 +41,7 @@ func main() {
// set up a goroutine to do parsey things with the stuff from stdin // set up a goroutine to do parsey things with the stuff from stdin
go func() { go func() {
for { for cmd := range in {
if closed(in) {
break
}
cmd := <-in
if cmd[0] == ':' { if cmd[0] == ':' {
switch idx := strings.Index(cmd, " "); { switch idx := strings.Index(cmd, " "); {
case idx == -1: case idx == -1:
@ -68,22 +64,16 @@ func main() {
// stall here waiting for asplode on error channel // stall here waiting for asplode on error channel
for { for {
if closed(c.Err) { for err := range c.Err {
// c.Err being closed indicates we've been disconnected from the fmt.Printf("goirc error: %s\n", err)
// server for some reason (e.g. quit, kill or ping timeout) }
// if we don't really want to quit, reconnect! if reallyquit {
if !reallyquit { break
}
fmt.Println("Reconnecting...") fmt.Println("Reconnecting...")
if err := c.Connect("irc.freenode.net", ""); err != nil { if err := c.Connect("irc.freenode.net", ""); err != nil {
fmt.Printf("Connection error: %s\n", err) fmt.Printf("Connection error: %s\n", err)
break break
} }
continue
}
break
}
if err := <-c.Err; err != nil {
fmt.Printf("goirc error: %s\n", err)
}
} }
} }

View File

@ -115,11 +115,7 @@ func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastInd
// dispatch input from channel as \r\n terminated line to peer // dispatch input from channel as \r\n terminated line to peer
// flood controlled using hybrid's algorithm if conn.Flood is true // flood controlled using hybrid's algorithm if conn.Flood is true
func (conn *Conn) send() { func (conn *Conn) send() {
for { for line := range conn.out {
if closed(conn.out) {
break
}
line := <-conn.out
if err := conn.io.WriteString(line + "\r\n"); err != nil { if err := conn.io.WriteString(line + "\r\n"); err != nil {
conn.error("irc.send(): %s", err.String()) conn.error("irc.send(): %s", err.String())
conn.shutdown() conn.shutdown()
@ -191,15 +187,9 @@ func (conn *Conn) recv() {
} }
func (conn *Conn) runLoop() { func (conn *Conn) runLoop() {
for { for line := range conn.in {
if closed(conn.in) {
break
}
select {
case line := <-conn.in:
conn.dispatchEvent(line) conn.dispatchEvent(line)
} }
}
// if we fall off the end here due to shutdown, // if we fall off the end here due to shutdown,
// reinit everything once the runloop is done // reinit everything once the runloop is done
// so that Connect() can be called again. // so that Connect() can be called again.