badman
badman

Reputation: 339

Golang how would you pipe the stdout of a function to another function that executes fzf

Say we have a the following function:

func example() {
   fmt.Println("January")
   fmt.Println("February")
   fmt.Println("March")
}

Now we need another function which takes the output of the above function to bash command fzf, how would you achieve this?

I know i have to redirect stdout to stdin, and there is a whole concept of os.Pipe

So i tried to capture the stdout first:

func capture(f func()) string {
  out := os.Stdout
  r, w, _ := os.Pipe()
  os.Stdout = w

  w.Close()
  os.Stdout = out

  var buf bytes.Buffer
  io.Copy(&buf, r)
  return buf.String()
}

Didn't work out and i haven't figured the part of sending stdout to the function that executes fzf

Upvotes: 1

Views: 463

Answers (0)

Related Questions