Undefined Title

Undefined Title

Go言語 os/exec sed実行

標準入力に書き込む場合。StdinPipeを使う。

package main

import (
  "fmt"
  "os/exec"
)

func main() {
  path, err := exec.LookPath("sed")
  if err != nil {
    panic(err)
  }

  cmd := exec.Command(path, "s/a/A/g")

  stdin, _ := cmd.StdinPipe()
  stdin.Write([]byte("aaa"))
  stdin.Close()

  out, _ := cmd.Output()
  fmt.Print(string(out))
}