title: How to continuously receive data from channel of goroutine
body: <p>I am a beginner of the Golang.  I had made a practice about Go channel. Which I open and read data from a file in the main goroutine, then pass the data to the second goroutine to save to another file with channel.
      My code is as flows</p>
  
  <pre><code>  func main() {
     f, err := os.OpenFile("test.go", os.O_RDONLY, 0600)
     ch := make(chan []byte)
     buf := make([]byte, 10)
     bytes_len, err := f.Read(buf)
     fmt.Println("ReadLen:", bytes_len)
     if err != nil {
        fmt.Println("Error: ", err)
        return
     }
     go WriteFile(ch)
     for {
        ch&lt;-buf
        bytes_len, err = f.Read(buf)
        if err != nil {
            fmt.Println("error=", err)
            break
        }
        if bytes_len &lt; 10 {
            ch&lt;-buf[:bytes_len]
            fmt.Println("Finished!")
            break
        }
     }
     time.Sleep(1e9)
     f.Close()
   }
  
    func WriteFile(ch &lt;-chan []byte) {
      fmt.Println("* begin!")
      f, err := os.OpenFile("/home/GoProgram/test/test.file",  os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
      if err != nil {
         fmt.Println("* Error:", err)
         return
      }
      /* Method1:  use the "select" will write to target file OK, but it is too slow!!!
      for {
        select {
           case bytes, ok:= &lt;-ch:
              if ok {
                f.Write(bytes)
              } else {
                fmt.Println("* file closed!")
                break
              }
           default:
              fmt.Println("* waiting data!")
        }
      } \*/
      // Method 2: use "for {if}", this will get messed text in target file, not identical with the source file.
      for {
        if bytes, ok := &lt;-ch; ok {
              f.Write(bytes)
              fmt.Println("* buff=", string(bytes))
              bytes = nil
              ok = false
        } else {
          fmt.Println("** End ", string(bytes), "  ", ok)
          break
        }
      }
  
      /* Method 3: use "for range", this will get messed text like in method2
      for data:= range ch {
           f.Write(data)
         //fmt.Println("* Data:", string(data))
      }
      \*/
      f.Close()
  }
  </code></pre>
  
  <p>My question is why the Method2 and Method3 will get messed text in the target file? How can I fix it?</p>