Go提供的os/exec包能夠執行外部程序,好比調用系統命令等。bash
最簡單的代碼,調用pwd命令顯示程序當前所在目錄:spa
package main import ( "fmt" "os/exec" ) func main() { pwdCmd := exec.Command("pwd") pwdOutput, _ := pwdCmd.Output() fmt.Println(string(pwdOutput)) }
執行後會輸出當前程序所在的目錄。code
若是要執行復雜參數的命令,能夠這樣:blog
exec.Command("bash", "-c", "ls -la")string
或者這樣:class
exec.Command("ls", "-la")import
或者這樣:程序
pwdCmd := exec.Command("ls", "-l", "-a")im