Source

go-play / echo.go

Full commit
package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func handle(conn net.Conn) {
	defer conn.Close()

	reader := bufio.NewReader(conn)
	message, err := reader.ReadBytes('\n')
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: can't read (%s)\n", err)
		return
	}
	conn.Write([]byte(message))
}

func main() {
	listener, err := net.Listen("tcp", ":8053")
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: can't listen (%s)\n", err)
		os.Exit(1)
	}

	defer listener.Close()

	for {
		conn, err := listener.Accept()
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: can't accept (%s)\n", err)
			os.Exit(1)
		}
		go handle(conn)
	}
}