当前位置: 首页 > news >正文

手机应用商店下载安装北京推广优化公司

手机应用商店下载安装,北京推广优化公司,wordpress 输入ftp,南京广告公司户外广告1.摘要 在很多场合, 使用Go语言需要调用外部命令来完成一些特定的任务, 例如: 使用Go语言调用Linux命令来获取执行的结果,又或者调用第三方程序执行来完成额外的任务。在go的标准库中, 专门提供了os/exec包来对调用外部程序提供支持, 本文将对调用外部命令的几种使用方法进行总…

1.摘要

在很多场合, 使用Go语言需要调用外部命令来完成一些特定的任务, 例如: 使用Go语言调用Linux命令来获取执行的结果,又或者调用第三方程序执行来完成额外的任务。在go的标准库中, 专门提供了os/exec包来对调用外部程序提供支持, 本文将对调用外部命令的几种使用方法进行总结。

2.直接调用函数

先用Linux上的一个简单命令执行看一下效果, 执行cal命令, 会打印当前月的日期信息,如图:

如果要使用Go代码调用该命令, 可以使用以下代码:

func main(){cmd := exec.Command("cal")err := cmd.Run()if err != nil {fmt.Println(err.Error())}
}

首先, 调用"os/exec"包中的Command函数,并传入命令名称作为参数, Command函数会返回一个exec.Cmd的命令对象。接着调用该命令对象的Run()方法运行命令。

如果此时运行程序, 会发现什么都没有出现, 这是因为我们没有处理标准输出, 调用os/exec执行命令, 标准输出和标准错误默认会被丢弃。

这里将cmd结构中的Stdout和Stderr分别设置为os.stdout和os.Stderr, 代码如下:

func main(){cmd := exec.Command("cal")cmd.Stdout = os.Stdoutcmd.Stderr = os.Stderrerr := cmd.Run()if err != nil {fmt.Println(err.Error())}
}

运行程序后显示:

3.输出到文件

输出到文件的关键, 是将exec.Cmd对象的Stdout和Stderr赋值文件句柄, 代码如下:

func main(){f, err := os.OpenFile("sample.txt", os.O_WRONLY|os.O_CREATE, os.ModePerm)if err != nil {fmt.Println(err.Error())}cmd := exec.Command("cal")cmd.Stdout = fcmd.Stderr = ferr := cmd.Run()if err != nil {fmt.Println(err.Error())}
}

os.OpenFile打开一个文件, 指定os.0_CREATE标志让操作系统在文件不存在时自动创建, 返回文件对象*os.File, *os.File实现了io.Writer接口。

运行程序结果如下:

4.发送到网络

这里开启一个HTTP服务, 服务端接收两个参数:年和月, 在服务端通过执行系统命令返回结果,代码如下:

import ("fmt""net/http""os/exec"
)
​
func queryDate(w http.ResponseWriter, r *http.Request) {var err errorif r.Method == "GET" {year := r.URL.Query().Get("year")month := r.URL.Query().Get("month")
​cmd := exec.Command("cal", month, year)cmd.Stdout = wcmd.Stderr = w
​err = cmd.Run()if err != nil {fmt.Println(err.Error())}}
}
​
func main() {http.HandleFunc("/querydate", queryDate)http.ListenAndServe(":8001", nil)
}

打开浏览器,在地址栏中输入URL查询2023年10月份的日历:

http://localhost:8001/querydate?year=2023&month=10 , 结果如下:

5.输出到多个目标

如果要将执行命令的结果同时输出到文件、网络和内存对象, 可以使用io.MultiWriter满足需求, io.MultiWriter可以很方便的将多个io.Writer转换成一个io.Writer, 修改之前的Web服务端程序如下:

func queryDate(w http.ResponseWriter, r *http.Request) {var err errorif r.Method == "GET" {buffer := bytes.NewBuffer(nil)
​year := r.URL.Query().Get("year")month := r.URL.Query().Get("month")
​f, _ := os.OpenFile("sample.txt", os.O_WRONLY|os.O_CREATE, os.ModePerm)mw := io.MultiWriter(w, f, buffer)
​cmd := exec.Command("cal", month, year)cmd.Stdout = mwcmd.Stderr = mw
​err = cmd.Run()if err != nil {fmt.Println(err.Error())}
​fmt.Println(buffer.String())}
}
​
func main() {http.HandleFunc("/querydate", queryDate)http.ListenAndServe(":8001", nil)
}

6.分别获取输出内容和错误

这里我们封装一个常用函数, 输入接收命令和多个参数, 返回错误和命令返回信息, 函数代码如下:

func ExecCommandOneTimeOutput(name string, args ...string) (error, string) {var out bytes.Buffervar stderr bytes.Buffercmd := exec.Command(name, args...)cmd.Stdout = &outcmd.Stderr = &stderrerr := cmd.Run()if err != nil {fmt.Println(fmt.Sprint(err) + ": " + stderr.String())return err, ""}return nil, out.String()
}

该函数可以作为通用的命令执行返回结果的函数, 分别返回了错误和命令返回信息。

7.循环获取命令内容

在Linux系统中,有些命令运行后结果是动态持续更新的,例如: top命令,对于该场景,我们封装函数如下:

func ExecCommandLoopTimeOutput(name string, args ...string) <-chan struct{} {cmd := exec.Command(name, args...)closed := make(chan struct{})defer close(closed)
​stdoutPipe, err := cmd.StdoutPipe()if err != nil {fmt.Println(err.Error())}defer stdoutPipe.Close()go func() {scanner := bufio.NewScanner(stdoutPipe)for scanner.Scan() {fmt.Println(string(scanner.Bytes()))_, err := simplifiedchinese.GB18030.NewDecoder().Bytes(scanner.Bytes())if err != nil {continue}}}()
​if err := cmd.Run(); err != nil {fmt.Println(err.Error())}return closed
}

通过调用cmd对象的StdoutPipe()输出管理函数, 我们可以实现持续获取后台命令返回的结果,并保持程序不退出。

在调用该函数的时候, 调用方式如下:

<-ExecCommandLoopTimeOutput("top")

打印出的信息将是一个持续显示信息,如图:

8.总结

本章节介绍了使用os/exec这个标准库调用外部命令的各种场景。在实际应用中, 基本用的最多的还是封装好的:ExecCommandOneTimeOutput()和ExecCommandLoopTimeOutput()两个函数, 毕竟外部命令一般只会包含两种:一种是执行后马上获取结果,第二种就是常驻内存持续获取结果。


文章转载自:
http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn
http://www.morning.hsklc.cn.gov.cn.hsklc.cn
http://www.morning.ljygq.cn.gov.cn.ljygq.cn
http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn
http://www.morning.tjcgl.cn.gov.cn.tjcgl.cn
http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn
http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn
http://www.morning.rnmyw.cn.gov.cn.rnmyw.cn
http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn
http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn
http://www.morning.rbktw.cn.gov.cn.rbktw.cn
http://www.morning.pngph.cn.gov.cn.pngph.cn
http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn
http://www.morning.ptmch.com.gov.cn.ptmch.com
http://www.morning.xrhst.cn.gov.cn.xrhst.cn
http://www.morning.slmbg.cn.gov.cn.slmbg.cn
http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn
http://www.morning.gjssk.cn.gov.cn.gjssk.cn
http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn
http://www.morning.grlth.cn.gov.cn.grlth.cn
http://www.morning.wfysn.cn.gov.cn.wfysn.cn
http://www.morning.bsghk.cn.gov.cn.bsghk.cn
http://www.morning.qhln.cn.gov.cn.qhln.cn
http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn
http://www.morning.qnzld.cn.gov.cn.qnzld.cn
http://www.morning.nytpt.cn.gov.cn.nytpt.cn
http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn
http://www.morning.jyknk.cn.gov.cn.jyknk.cn
http://www.morning.bsxws.cn.gov.cn.bsxws.cn
http://www.morning.stprd.cn.gov.cn.stprd.cn
http://www.morning.xcdph.cn.gov.cn.xcdph.cn
http://www.morning.nd-test.com.gov.cn.nd-test.com
http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn
http://www.morning.qnftc.cn.gov.cn.qnftc.cn
http://www.morning.zttjs.cn.gov.cn.zttjs.cn
http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn
http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn
http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn
http://www.morning.ffrys.cn.gov.cn.ffrys.cn
http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn
http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn
http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn
http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn
http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn
http://www.morning.mnsts.cn.gov.cn.mnsts.cn
http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn
http://www.morning.wdshp.cn.gov.cn.wdshp.cn
http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn
http://www.morning.nckjk.cn.gov.cn.nckjk.cn
http://www.morning.dbqg.cn.gov.cn.dbqg.cn
http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn
http://www.morning.dhqg.cn.gov.cn.dhqg.cn
http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn
http://www.morning.msxhb.cn.gov.cn.msxhb.cn
http://www.morning.pmjw.cn.gov.cn.pmjw.cn
http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn
http://www.morning.rknsp.cn.gov.cn.rknsp.cn
http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn
http://www.morning.srbbh.cn.gov.cn.srbbh.cn
http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn
http://www.morning.btrfm.cn.gov.cn.btrfm.cn
http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn
http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn
http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn
http://www.morning.pccqr.cn.gov.cn.pccqr.cn
http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn
http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn
http://www.morning.rwfj.cn.gov.cn.rwfj.cn
http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn
http://www.morning.phjyb.cn.gov.cn.phjyb.cn
http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn
http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn
http://www.morning.wptrm.cn.gov.cn.wptrm.cn
http://www.morning.xyhql.cn.gov.cn.xyhql.cn
http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn
http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn
http://www.morning.ybgt.cn.gov.cn.ybgt.cn
http://www.morning.bfmq.cn.gov.cn.bfmq.cn
http://www.morning.bhznl.cn.gov.cn.bhznl.cn
http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn
http://www.tj-hxxt.cn/news/13836.html

相关文章:

  • 做网站 数据库北京网站优化站优化
  • 九江专业网站建设定制百度文库首页官网
  • 山西省网站备案要多久爱站工具包
  • 电子商务网站开发计划书企业培训体系搭建
  • 网站首页怎么做全屏swfseo推广具体做什么
  • 个人注册域名网站怎么做上海关键词优化报价
  • 帮朋友做网站 知乎推广网页
  • 新乡公司网站建设站长工具seo综合查询工具
  • 网站域名验证快速网站排名提升
  • 房山 网站建设培训学校招生营销方案
  • dreamweaver网站模板百度首页百度
  • 那个网站教宝妈做辅食站长之家ppt模板
  • 做外贸有哪些免费的网站有哪些讯展网站优化推广
  • wordpress建站毕业论文百度网站快速排名公司
  • 做门户网站国外免费域名申请
  • 江苏建设人才的网站世界足球排名前100名
  • 在线做h5 的网站网页制作基础教程
  • 做暖暖在线获取网站seo模拟点击工具
  • 如何网站全部结构鞍山seo外包
  • 以学校为目标做网站策划书搜索引擎优化是做什么的
  • 怎么给网站加代码百度广告价格
  • 做精酿啤酒购买的网站找合作项目app平台
  • 网站标题字数推广软文平台
  • html手机网站开发教程个人网页免费域名注册入口
  • 甘肃做网站的公司有哪些seo建站公司
  • wordpress评论贴图seol英文啥意思
  • 成都 网站建设 公司定制网站建设电话
  • 今天广西紧急通知最新江苏关键词推广seo
  • 做视频必须知道的一些网站企业seo网站营销推广
  • 兄弟们有没有没封的网站关键字挖掘机爱站网