package main
import (
"fmt"
"log"
"os/exec"
"bytes"
"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())
}
strings.NewReader("some input") => cmd.Stdin ----> cmd.Run() => cmd.Stdout ----> out bytes.Buffer --> out.String() -->printf
import (
"fmt"
"log"
"os/exec"
"bytes"
"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())
}
作用:把小写转成大写
输出:in all caps: "SOME INPUT"
strings.NewReader("some input") => cmd.Stdin ----> cmd.Run() => cmd.Stdout ----> out bytes.Buffer --> out.String() -->printf
有疑问加站长微信联系(非本文作者)