clock server with only main go routine



👍 go build clock1.go
👍 ./clock1 &
[1] 19694
👍 nc localhost 41766
15.20.02
15.20.03
15.20.04
15.20.05
15.20.06
^C

👍 killall clock1

👍 cat clock1.go 
package main

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

func main() {
  listener, err := net.Listen(
    "tcp", "localhost:41766")
  if err != nil {
    log.Fatal(err)
  }
  for {
    conn, err := listener.Accept()
    if err != nil {
      log.Print(err)
      continue
    }
    handleConn(conn)
  }
}

func handleConn(c net.Conn) {
  defer c.Close()
  for {
    _, err := io.WriteString(
      c, time.Now().Format("15.04.05\n"))
    if err != nil {
      return
    }
    time.Sleep(1 * time.Second)
  }
}

    

ch8.1 Goroutines p218/237 of