os/exec and bytes.Buffer clarity

os/exec and bytes.Buffer clarity

I was looking at the os/exec package and I found myself a little confused with the example for exec.Command and the use of the bytes.Buffer.

The example in the documentation is throwing me off a little as I'm not really understanding how the output of cmd.Run is put into the buffer variable.

Example from the docs:

package main import ( "bytes" "fmt" "log" "os/exec" "strings" ) func main() { cmd := exec.Command("tr", "a-z", "A-Z") cmd.Stdin = strings.NewReader("some input") var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { log.Fatal(err) } fmt.Printf("in all caps: %q\n", out.String()) } 

Is this because stdout is being assigned the value of the pointer to "out"? That seems backwards to me. I tested it with my own code and it works I'm just not understanding how I guess, and the docs are a little confusing to me to read.

Submitted January 23, 2018 at 02:29AM by Dangle76
via reddit http://ift.tt/2Dwz4o5