title: Check if there is something to read on STDIN in Golang
body: <p>I need a command line utility to behave different if some string is piped into its STDIN. Here's some minimal example:</p>
  
  <pre><code>package main // file test.go
  
  import (
      "fmt"
      "io/ioutil"
      "os"
  )
  
  func main() {
      bytes, _ := ioutil.ReadAll(os.Stdin)
  
      if len(bytes) &gt; 0 {
          fmt.Println("Something on STDIN: " + string(bytes))
      } else {
          fmt.Println("Nothing on STDIN")
      }
  }
  </code></pre>
  
  <p>This works fine if you call it like that:</p>
  
  <pre><code>echo foo | go run test.go
  </code></pre>
  
  <p>If <code>test.go</code> is called without anything on STDIN, the thing stucks at... </p>
  
  <pre><code>bytes, _ := ioutil.ReadAll(os.Stdin)
  </code></pre>
  
  <p>... waiting for <code>EOF</code>.</p>
  
  <p>What do I need to do to get this going?</p>
  
  <p>Thanks in advance!</p>