Last active
December 28, 2015 17:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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