title: Sending file over SSH in go
body: <p><a href="https://stackoverflow.com/questions/38242598/golang-scp-file-using-crypto-ssh">I found this answer before posting this question but the answer is not clear to me.</a></p>
  
  <p>Here is the code of the answer:</p>
  
  <pre><code>conn, err := ssh.Dial("tcp", hostname+":22", config)
  if err != nil {
      return err
  }
  
  session, err := conn.NewSession()
  if err != nil {
      return err
  }
  defer session.Close()
  
  r, err := session.StdoutPipe()
  if err != nil {
      return err
  }
  
  name := fmt.Sprintf("%s/backup_folder_%v.tar.gz", path, time.Now().Unix())
  file, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
  if err != nil {
      return err
  }
  defer file.Close()
  
  if err := session.Start(cmd); err != nil {
      return err
  }
  
  n, err := io.Copy(file, r)
  if err != nil {
      return err
  }
  
  if err := session.Wait(); err != nil {
      return err
  }
  
  return nil
  </code></pre>
  
  <p>I don't understand relation between the cmd variable and io.Copy, where and how does it know which file to copy.
  I like the idea of using io.Copy but i do not know how to create the file via ssh and start sending content to it using io.Copy. </p>