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
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
}
}
}

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
// 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