title: How to get output from stdout into a string in golang
body: <p>I have the following code which outputs data from stdout to a file:</p>
  
  <pre><code>cmd := exec.Command("ls","lh")
  outfile, err := os.Create("./out.txt")
  if err != nil {
      panic(err)
  }
  defer outfile.Close()
  
  stdoutPipe, err := cmd.StdoutPipe()
  if err != nil {
      panic(err)
  }
  
  writer := bufio.NewWriter(outfile)
  defer writer.Flush()
  
  err = cmd.Start()
  if err != nil {
      panic(err)
  }
  
  go io.Copy(writer, stdoutPipe)
  cmd.Wait()
  </code></pre>
  
  <p>I need to get the output from stdout into a string value instead of a file. How do I achieve that?</p>
  
  <p>Is there perhaps another function that will allow me to change the io.Copy line to go io.Copy(myStringVariable, stdoutPipe) as I need to read the output of the command and apply some processing to it?</p>
  
  <p>Thanks in advance</p>