title: Reading data just written to a temp file
body: <p>In Go, I am trying to write data to a temp file that I then turn around and read but have not been successful.  Below is a stripped down test program.  I have verified that the data are being written to the file by inspecting the temporary file.  So, at least I know that data are making it into the file.  I just am then unable to read it out.</p>
  <p>Thank you for your help in advance</p>
  <pre><code>package main
  
  import (
      &quot;bufio&quot;
      &quot;fmt&quot;
      &quot;io/ioutil&quot;
      &quot;log&quot;
      &quot;os&quot;
      &quot;path/filepath&quot;
  )
  
  func main() {
  
      tmpFile, err := ioutil.TempFile(&quot;&quot;, fmt.Sprintf(&quot;%s-&quot;, filepath.Base(os.Args[0])))
      if err != nil {
          log.Fatal(&quot;Could not create temporary file&quot;, err)
      }
      fmt.Println(&quot;Created temp file: &quot;, tmpFile.Name())
      //  defer os.Remove(tmpFile.Name())
  
      fmt.Println(&quot;Writing some data to the temp file&quot;)
      if _, err = tmpFile.WriteString(&quot;test data&quot;); err != nil {
          log.Fatal(&quot;Unable to write to temporary file&quot;, err)
      } else {
          fmt.Println(&quot;data should have been written&quot;)
      }
  
      fmt.Println(&quot;Trying to read the temp file now&quot;)
      s := bufio.NewScanner(tmpFile)
      for s.Scan() {
          fmt.Println(s.Text())
      }
      err = s.Err()
      if err != nil {
          log.Fatal(&quot;error reading temp file&quot;, err)
      }
  }
  </code></pre>