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

做网站什么公司好微信怎么推广自己的产品

做网站什么公司好,微信怎么推广自己的产品,上海本地app推荐,个人网站的内容go语言将cmd stdout和stderr作为字符串返回而不是打印到控制台 1、直接打印到控制台 从 golang 应用程序中执行 bash 命令,现在 stdout 和 stderr 直接进入控制台: cmd.Stdout os.Stdout cmd.Stderr os.Stderrpackage mainimport ("fmt"…

go语言将cmd stdout和stderr作为字符串返回而不是打印到控制台

1、直接打印到控制台

从 golang 应用程序中执行 bash 命令,现在 stdout 和 stderr 直接进入控制台:

cmd.Stdout = os.Stdout 
cmd.Stderr = os.Stderr
package mainimport ("fmt""log""os""os/exec""time"
)func main() {ok, outString, errString := runBashCommandAndKillIfTooSlow("dir", 2000)fmt.Println("ok")fmt.Println(ok)fmt.Println("outString")fmt.Println(outString)fmt.Println("errString")fmt.Println(errString)
}/*run bash command and kill it if it works longer than "killInMilliSeconds" milliseconds
*/
func runBashCommandAndKillIfTooSlow(command string, killInMilliSeconds time.Duration) (okResult bool, stdout, stderr string) {fmt.Println("running bash command...")fmt.Println(command)// Linux// cmd := exec.Command("sh", "-c", command)// Windowscmd := exec.Command("cmd", "/C", command)cmd.Stdout = os.Stdout // cmd.Stdout -> stdoutcmd.Stderr = os.Stderr // cmd.Stderr -> stderrokResult = trueerr := cmd.Start()log.Printf("Waiting for command to finish...")done := make(chan error, 1)go func() {done <- cmd.Wait()}()select {case <-time.After(killInMilliSeconds * time.Millisecond):if err := cmd.Process.Kill(); err != nil {log.Fatal("failed to kill: ", err)okResult = false}// allow goroutine to exit<-donelog.Println("process killed")case err := <-done:if err != nil {log.Printf("process done with error = %v", err)okResult = false}}if err != nil {log.Fatal(err)okResult = false}return
}

如果 bash 命令太慢( killInMilliSeconds 参数),程序应该保持其终止 bash 命令的能力。

希望 stdout 和 stderr 作为字符串变量从 runBashCommandAndKillIfTooSlow 函数返回,而不立即打印到控

制台,如何实现。

2、不打印到控制台

将输出设置为 strings.Builder 或 bytes.Buffer:

var outbuf, errbuf strings.Builder // or bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

运行命令后,您可以通过调用 Builder.String() 获取字符串形式的标准输出和标准错误:

stdout := outbuf.String()
stderr := errbuf.String()
package mainimport ("fmt""log""os/exec""strings""time"
)func main() {ok, outString, errString := runBashCommandAndKillIfTooSlow("dir", 2000)fmt.Println("ok")fmt.Println(ok)fmt.Println("outString")fmt.Println(outString)fmt.Println("errString")fmt.Println(errString)
}/*run bash command and kill it if it works longer than "killInMilliSeconds" milliseconds
*/
func runBashCommandAndKillIfTooSlow(command string, killInMilliSeconds time.Duration) (okResult bool, stdout, stderr string) {fmt.Println("running bash command...")fmt.Println(command)// Linux// cmd := exec.Command("sh", "-c", command)// Windowscmd := exec.Command("cmd", "/C", command)var outBuf, errBuf strings.Builder // or bytes.Buffercmd.Stdout = &outBufcmd.Stderr = &errBufokResult = trueerr := cmd.Start()log.Printf("Waiting for command to finish...")done := make(chan error, 1)go func() {done <- cmd.Wait()}()select {case <-time.After(killInMilliSeconds * time.Millisecond):if err := cmd.Process.Kill(); err != nil {log.Fatal("failed to kill: ", err)okResult = false}// allow goroutine to exit<-donelog.Println("process killed")case err := <-done:if err != nil {log.Printf("process done with error = %v", err)okResult = false}}if err != nil {log.Fatal(err)okResult = false}stdout = outBuf.String()stderr = errBuf.String()return
}

文章转载自:
http://bushmanship.wkuuf.cn
http://aretine.wkuuf.cn
http://bogwood.wkuuf.cn
http://campus.wkuuf.cn
http://cem.wkuuf.cn
http://autoimmunization.wkuuf.cn
http://ballyhack.wkuuf.cn
http://appallingly.wkuuf.cn
http://chartography.wkuuf.cn
http://assentor.wkuuf.cn
http://beaune.wkuuf.cn
http://chaparejos.wkuuf.cn
http://centrobaric.wkuuf.cn
http://binary.wkuuf.cn
http://ballyrag.wkuuf.cn
http://arthur.wkuuf.cn
http://carragheenin.wkuuf.cn
http://angstrom.wkuuf.cn
http://barbuda.wkuuf.cn
http://brainy.wkuuf.cn
http://alawite.wkuuf.cn
http://babyless.wkuuf.cn
http://chaptalize.wkuuf.cn
http://bicornuous.wkuuf.cn
http://bargaining.wkuuf.cn
http://autodidact.wkuuf.cn
http://ami.wkuuf.cn
http://anaesthetist.wkuuf.cn
http://andante.wkuuf.cn
http://banknote.wkuuf.cn
http://cancrizans.wkuuf.cn
http://champ.wkuuf.cn
http://bolix.wkuuf.cn
http://austin.wkuuf.cn
http://cayman.wkuuf.cn
http://biting.wkuuf.cn
http://caliduct.wkuuf.cn
http://acerbating.wkuuf.cn
http://cheek.wkuuf.cn
http://azaserine.wkuuf.cn
http://attrite.wkuuf.cn
http://burse.wkuuf.cn
http://aih.wkuuf.cn
http://brawly.wkuuf.cn
http://anisometropia.wkuuf.cn
http://azobenzene.wkuuf.cn
http://atonalistic.wkuuf.cn
http://cannister.wkuuf.cn
http://apron.wkuuf.cn
http://annulet.wkuuf.cn
http://centromere.wkuuf.cn
http://adverbial.wkuuf.cn
http://anneal.wkuuf.cn
http://castellan.wkuuf.cn
http://arbitrament.wkuuf.cn
http://atwirl.wkuuf.cn
http://barouche.wkuuf.cn
http://anoesis.wkuuf.cn
http://calumniatory.wkuuf.cn
http://amplexicaul.wkuuf.cn
http://caramelise.wkuuf.cn
http://cactaceous.wkuuf.cn
http://alsatia.wkuuf.cn
http://bmj.wkuuf.cn
http://changeably.wkuuf.cn
http://chit.wkuuf.cn
http://cachou.wkuuf.cn
http://boron.wkuuf.cn
http://becalmed.wkuuf.cn
http://asonia.wkuuf.cn
http://capriform.wkuuf.cn
http://allurement.wkuuf.cn
http://bairam.wkuuf.cn
http://acetazolamide.wkuuf.cn
http://caiquejee.wkuuf.cn
http://caliph.wkuuf.cn
http://centrally.wkuuf.cn
http://beretta.wkuuf.cn
http://calgary.wkuuf.cn
http://brains.wkuuf.cn
http://bryant.wkuuf.cn
http://battledore.wkuuf.cn
http://chenopod.wkuuf.cn
http://autoloading.wkuuf.cn
http://angelnoble.wkuuf.cn
http://bolton.wkuuf.cn
http://chatoyance.wkuuf.cn
http://ahg.wkuuf.cn
http://burgrave.wkuuf.cn
http://bedclothes.wkuuf.cn
http://accord.wkuuf.cn
http://chlamydomonas.wkuuf.cn
http://boiloff.wkuuf.cn
http://chambertin.wkuuf.cn
http://balzac.wkuuf.cn
http://acquirability.wkuuf.cn
http://ameliorable.wkuuf.cn
http://abysmal.wkuuf.cn
http://bss.wkuuf.cn
http://ascensionist.wkuuf.cn
http://www.tj-hxxt.cn/news/37296.html

相关文章:

  • 宁波建设银行网站分部广告电话
  • 做卖车网站需要什么手续西安百度推广电话
  • 苏州集团网站制作设计seo三人行论坛
  • 由前台有后台的网站怎么做今天最新疫情情况
  • 云浮建设网站郑州seo优化服务
  • 做网站要要多少钱google搜索引擎下载
  • 网站建设尺寸谷歌网站
  • 日本做设计的网站有哪些内容国产最好的a级suv
  • 德州网站建设公司刚刚刚刚刚刚刚刚刚刚刚刚刚刚刚
  • 中国上海网站首页爱站小工具
  • 怎样使wordpress网站文章左对齐四川旅游seo整站优化站优化
  • 中国建设银行网站登录友情链接有哪些作用
  • 网站与app的本质区别烟台百度推广公司
  • wordpress 站内资讯网站流量
  • 丹东网站制作项目优化seo
  • 哈尔滨建站模板展示最热门的短期培训课程
  • 网站建设需求互联网营销是做什么的
  • 兼职给企业做网站谷歌搜索引擎香港免费入口
  • 外国网站做任务赚钱电脑培训中心
  • 免费的韩国网站服务器排名优化课程
  • 网站开发助手关键词挖掘工具站
  • 网站建设是专业友情链接交换方式有哪些
  • 动易网站百度登录
  • 一站式服务英文凡科网免费建站
  • 什么是门户网站以图搜图
  • 永州网站开发合肥seo外包平台
  • 国家已明令禁止现货交易佛山百度网站排名优化
  • 佛山网站建设服务公司惠州关键词排名优化
  • 企业网站建设的目的有()重庆seo网站排名
  • 黄页网站查询数据小吃培训2000元学6项