Minimal Server

The purpose of this code is to highlight what is needed to create a TCP server in Go.

I have implemented a simple tcp server without any error checking, logging, cleanup, or anything extra. There are two TCP servers in a single program. An echo server listening on port 2007 run in the background and a daytime server listening on port 2013 run in the foreground.

package main

import (
	"io"
	"net"
	"time"
)

func main() {
	// the echo server is put in the background
	go func() {
		echoListener, _ := net.Listen("tcp", ":2007")
		for {
			connection, _ := echoListener.Accept()
			go func(c net.Conn) {
				io.Copy(c, c)
				c.Close()
			}(connection)
		}
	}()
	// the daytime server is in the foreground so we don't exit early.
	daytimeListener, _ := net.Listen("tcp", ":2013")
	for {
		connection, _ := daytimeListener.Accept()
		go func(c net.Conn) {
			c.Write([]byte(time.Now().Format("Monday, January 2, 2006 15:04:05-MST\r\n")))
			c.Close()
		}(connection)
	}
}