title: How to overwrite file content in golang
body: <p>I have a empty file called <code>a.txt</code>, I want to output a value(int) to it in a loop, and overwrite last content in file <code>a.txt</code>. For example, </p>
  
  <pre><code>    // open a file
      f, err := os.Open("test.txt")
      if err != nil {
          log.Fatal(err)
      }
      defer f.Close()
  
      // another file
      af, err := os.OpenFile("a.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
      if err != nil {
          log.Fatal(err)
      }
      defer af.Close()
  
      b := []byte{}
  
      scanner := bufio.NewScanner(f)
      for scanner.Scan() {
          b = append(b, scanner.Bytes()...)
          // how to output len(b) into a.txt?
      }
  </code></pre>