Created
August 3, 2011 17:21
-
-
Save kylelemons/1123209 to your computer and use it in GitHub Desktop.
A demonstration listener in Go
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
package main | |
import ( | |
"net" | |
"fmt" | |
"flag" | |
"log" | |
) | |
var port = flag.Int("port", 1234, "port to listen on") | |
func main() { | |
flag.Parse() | |
listener, err := net.ListenTCP("tcp", &net.TCPAddr{ | |
Port: *port, | |
}) | |
if err != nil { | |
log.Fatalf("listen: %s", err) | |
} | |
defer listener.Close() | |
log.Printf("Listening on %s...\n", listener.Addr()) | |
process := func(conn *net.TCPConn) { | |
defer conn.Close() | |
log.Printf("Accepted connection: %s <- %s\n", conn.LocalAddr(), conn.RemoteAddr()) | |
if _, err = fmt.Fprintf(conn, "You connected to: %s\n", conn.LocalAddr()); err != nil { | |
log.Fatalf("fprintf: %s", err) | |
} | |
} | |
for { | |
conn, err := listener.AcceptTCP() | |
if err != nil { | |
log.Fatalf("accept: %s", err) | |
} | |
go process(conn) | |
} | |
} |
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
include $(GOROOT)/src/Make.inc | |
TARG=l | |
DEP= | |
GOFILES=\ | |
main.go\ | |
pkgdir=$(GOROOT)/pkg/$(GOOS)_$(GOARCH) | |
PREREQ+=$(foreach PKG,$(DEP),$(pkgdir)/$(PKG).a) | |
include $(GOROOT)/src/Make.cmd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment