This is a created article in which the information may have evolved or changed.
Golang run the cmd command to output the execution step-by-line, note that not all the content is output at once, but follow the execution of the process, line-by-line real-time display
The code is as follows:
Package Mainimport ("Bufio" "FMT" "io" "os/exec") func main () {command: = "ls" params: = []string{"-L"}//execute cmd command: LS- Lexeccommand (command, params)}func execcommand (CommandName string, params []string) bool {cmd: = exec. Command (CommandName, params ...) Displays the command to run FMT. Println (cmd. Args) stdout, err: = cmd. Stdoutpipe () if err! = Nil {fmt. PRINTLN (ERR) return false}cmd. Start () Reader: = Bufio. Newreader (STDOUT)//real-time loop reads a line in the output stream for {lines, err2: = Reader. ReadString (' \ n ') if err2! = Nil | | Io. EOF = = err2 {break}fmt. Println (line)}cmd. Wait () return True}
Compile run effect