From af8dfdb6f330d306c7c98979b5624dc39cc74894 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sat, 19 Dec 2009 15:19:28 +0000 Subject: [PATCH] change channel reads to use 'for v := range ch {}' idiom --- client.go | 28 +++++++++------------------- irc/connection.go | 14 ++------------ 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/client.go b/client.go index 2bd8278..448a758 100644 --- a/client.go +++ b/client.go @@ -41,11 +41,7 @@ func main() { // set up a goroutine to do parsey things with the stuff from stdin go func() { - for { - if closed(in) { - break - } - cmd := <-in + for cmd := range in { if cmd[0] == ':' { switch idx := strings.Index(cmd, " "); { case idx == -1: @@ -68,22 +64,16 @@ func main() { // stall here waiting for asplode on error channel for { - if closed(c.Err) { - // c.Err being closed indicates we've been disconnected from the - // server for some reason (e.g. quit, kill or ping timeout) - // if we don't really want to quit, reconnect! - if !reallyquit { - fmt.Println("Reconnecting...") - if err := c.Connect("irc.freenode.net", ""); err != nil { - fmt.Printf("Connection error: %s\n", err) - break - } - continue - } + for err := range c.Err { + fmt.Printf("goirc error: %s\n", err) + } + if reallyquit { break } - if err := <-c.Err; err != nil { - fmt.Printf("goirc error: %s\n", err) + fmt.Println("Reconnecting...") + if err := c.Connect("irc.freenode.net", ""); err != nil { + fmt.Printf("Connection error: %s\n", err) + break } } } diff --git a/irc/connection.go b/irc/connection.go index 0510974..0950a23 100644 --- a/irc/connection.go +++ b/irc/connection.go @@ -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 // flood controlled using hybrid's algorithm if conn.Flood is true func (conn *Conn) send() { - for { - if closed(conn.out) { - break - } - line := <-conn.out + for line := range conn.out { if err := conn.io.WriteString(line + "\r\n"); err != nil { conn.error("irc.send(): %s", err.String()) conn.shutdown() @@ -191,14 +187,8 @@ func (conn *Conn) recv() { } func (conn *Conn) runLoop() { - for { - if closed(conn.in) { - break - } - select { - case line := <-conn.in: + for line := range conn.in { conn.dispatchEvent(line) - } } // if we fall off the end here due to shutdown, // reinit everything once the runloop is done