go os/exec执行外部程序
2016-03-09 17:07
597 查看
Go提供的os/exec包可以执行外部程序,比如调用系统命令等。
最简单的代码,调用pwd命令显示程序当前所在目录:
package main
import (
"fmt"
"os/exec"
)
func main() {
pwdCmd := exec.Command("pwd")
pwdOutput, _ := pwdCmd.Output()
fmt.Println(string(pwdOutput))
}
执行后会输出当前程序所在的目录。
如果要执行复杂参数的命令,可以这样:
exec.Command("bash", "-c", "ls -la")
或者这样:
exec.Command("ls", "-la")
或者这样:
pwdCmd := exec.Command("ls", "-l", "-a")
最简单的代码,调用pwd命令显示程序当前所在目录:
package main
import (
"fmt"
"os/exec"
)
func main() {
pwdCmd := exec.Command("pwd")
pwdOutput, _ := pwdCmd.Output()
fmt.Println(string(pwdOutput))
}
执行后会输出当前程序所在的目录。
如果要执行复杂参数的命令,可以这样:
exec.Command("bash", "-c", "ls -la")
或者这样:
exec.Command("ls", "-la")
或者这样:
pwdCmd := exec.Command("ls", "-l", "-a")
相关文章推荐
- 条形码、二维码以及二维码添加logo
- mongod启动时候几个参数说明
- Go随机数
- golan 3ff8 g 编译笔记,如何在指定系统下编译
- 53-题目1440:Goldbach's Conjecture
- 新浪SAE部署django博客
- 初识Golang
- [golang note] 匿名组合
- 我与小娜(30):AlphaGo赢了!
- ORA-01017: invalid username/password; logon denied
- alphago战胜李世石
- django mongoengine 判断mongoengine定义的字段类型
- 为网页添加logo图标
- Go一致性哈希库consistent
- Codeforces 86D. Powerful array (MO's Algorithm(区间查询的离线优化))
- golang中调用C
- golang中函数闭包遇到的坑
- django1.8 增加注册用户其他字段
- 李世石和AlphaGo人机围棋大赛
- Go语言中Restful模式与Routes路由框架