title: Setting timeout via SetDeadline() for TCP listener
body: <p>I'm trying to implement non-blocking <code>Accept()</code> and the best I've come so far is the following code snippet (it's a working Go v1.6.2 program):</p>
  
  <pre><code>package main
  
  import (
      "net"
      "log"
      "time"
  )
  
  func createClient() {
      tcpConn, err := net.DialTCP("tcp4", nil, &amp;net.TCPAddr{
          IP:     net.IPv4(127, 0, 0, 1),
          Port:   12819,
      })
      if err != nil {
          log.Fatalln("Error connecting to the server!")
      }
      log.Println("Managed to dial!")
      tcpConn.Close()
  }
  
  func main() {
      go createClient()
      l, err := net.ListenTCP("tcp4", &amp;net.TCPAddr{
          IP:     net.IPv4(127, 0, 0, 1),
          Port:   12819,
      })
      if err != nil {
          log.Fatalln("Can't listen on provided IP/port!")
      }
      defer l.Close()
      if err = l.SetDeadline(time.Now().Add(time.Nanosecond)); err != nil {
          log.Fatalln("Can't set appropriate deadline!")
      }
      tcpConn, err := l.AcceptTCP()
      if err != nil {
          if opError, ok := err.(*net.OpError); ok &amp;&amp; opError.Timeout() {
              log.Fatalln("Timeout error!")
          }
          log.Fatalln("Error while accepting connection!")
      }
      log.Println("Accepted new connection!")
      tcpConn.Close()
  }
  </code></pre>
  
  <p>The problem is that I always get <code>Timeout error!</code>. As far as I understand that's because by the time listener's <code>AcceptTCP()</code> gets called the deadline previously set will have already expired. Try changing it to <code>time.Microsecond</code> and you'd probably get the same result (unless you have CPU slower than mine). Things start to change only when the deadline gets at least <code>time.Second</code>. That's when I start to get <code>Accepted new connection!</code>/<code>Managed to dial!</code>.</p>
  
  <p>So I think I nailed the problem. Any thoughts?</p>