title: How to set timeout in *os.File/io.Read in golang
body: <p>I know there is a function called <code>SetReadDeadline</code> that can set a timeout in socket(conn.net) reading, while io.Read not. There is a way that starts another routine as a timer to solve this problem, but it brings another problem that the reader routine(io.Read) still block:</p>
  
  <pre><code>func (self *TimeoutReader) Read(buf []byte) (n int, err error) { 
      ch := make(chan bool) 
      n = 0 
      err = nil 
      go func() { // this goroutime still exist even when timeout
              n, err = self.reader.Read(buf) 
              ch &lt;- true 
      }() 
      select { 
      case &lt;-ch: 
              return 
      case &lt;-time.After(self.timeout): 
              return 0, errors.New("Timeout") 
      } 
      return 
  } 
  </code></pre>
  
  <p>This question is similar in this <a href="https://groups.google.com/forum/#!msg/golang-nuts/Hmjf5Ws8g5w/cbj6JyaCpKMJ" rel="noreferrer">post</a>, but the answer is unclear.
  Do you guys have any good idea to solve this problem?</p>