Skip to content

Instantly share code, notes, and snippets.

@deckarep
Last active December 28, 2015 17:59
Show Gist options
  • Save deckarep/7539418 to your computer and use it in GitHub Desktop.
Save deckarep/7539418 to your computer and use it in GitHub Desktop.
From Andrew Gerrand's talk: Go: code that grows with grace http://vimeo.com/53221560 Notice that because the connection is a reader/writer io.Copy can be used. Short and sweet. Also notice that io.Copy runs in an infinite for loop so long as EOF or an error has not occurred.
//As seen in: http://vimeo.com/53221560 (Andrew Gerrand's talk: Go: code that grows with grace)
//A concurrent Echo Server
package main
import (
"io"
"log"
"net"
)
const listenAddr = "localhost:4000"
func main(){
l, err := net.Listen("tcp", listenAddr)
if err != nil{
log.Fatal(err)
}
for {
c, err := l.Accept()
if err != nil{
log.Fatal(err)
}
go io.Copy(c, c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment