package main
import (
"log"
"os/exec"
)
func main() {
cmd := exec.Command(`C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`)
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
}
go run后就可以马上打开谷歌浏览器了
shell命令
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("bash", "-c", "whereis rclone")
out, err := cmd.Output()
if err != nil {
fmt.Printf("error is: %s\n", out)
return
}
fmt.Printf("%s\n", out)
}
这里把whereis rclone换成rclone config就会报错,为什么呢?因为rclone config后会要求输入,这样就会报错,可以直接忽略err就好了
执行windows的cmd命令
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("cmd.exe", "/c", `E:\rclone\RcloneBrowser\rclone.exe config file`)
out, err := cmd.Output()
if err != nil {
fmt.Printf("error is: %s\n", out)
return
}
fmt.Printf("%s\n", out)
}