草庐IT

create_command

全部标签

bash - Golang exec.Command() bash 命令不工作

这个问题在这里已经有了答案:execgitcommandrefusestoredirectedtofileinGo(1个回答)exec.commandforpatchcommand(1个回答)exec.Commandwithinputredirection(3个答案)关闭4年前。我想使用golang的exec.Command()运行以下bash命令ls>sample.txt为此我写_,err:=exec.Command("ls",">","sample.txt").Output()但这似乎行不通。我知道我可以使用写入文件exec.Command().StdoutPipe()但我想用那种

戈朗错误 : Package command not found

我在运行.go文件时遇到此错误。./instance.go:line1:package:commandnotfound./instance.go:line3:syntaxerrornearunexpectedtoken`newline'./instance.go:line3:`import('到目前为止,我已经看到错误通常意味着未设置GOPATH,但是,echo"$GOPATH"输出/root/go并且我的路径是当前/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/go/bin:/ro

go - "too many open files"与 os.Create

我要创建大约220,000个图像文件(.png)。我在尝试创建第1'081个文件时遇到此错误消息:panic:打开/media/Snaps/pics/image1081_0.png:打开的文件太多我添加了deferw.Close()行,但它并没有改变错误。i:=1fori当然可以绕过这个限制吗?也许我没有正确关闭文件? 最佳答案 TheGoProgrammingLanguageSpecificationDeferstatementsA"defer"statementinvokesafunctionwhoseexecutionisde

go - 带有 O_CREATE 标志的 os.OpenFile 抛出没有这样的文件或目录

我正在尝试使用os.OpenFile函数打开带有os包的日志文件,如果此类文件不存在,它应该使用适当的标志创建文件。但它返回非零错误:没有这样的文件或目录。顺便说一句,我正在使用linux并使用1.6。日志文件打开发生在init函数中。这是我的代码:packagemainimport("encoding/json""fmt""log""net/http""os""github.com/gorilla/mux""github.com/gorilla/sessions")//Structsholdsserverconfigurationdatafromconfigjson.typeserv

unit-testing - 如何使用 os.O_RDWR 和 os.O_CREATE 标志测试 file.Open?

我正在尝试为结构构造函数编写单元测试,如果在file.Open期间发生错误,它也可能返回nil。我不知道如何使用标志测试/模拟文件错误:os.O_RDWR|os.O_CREATE|os.O_APPEND我试图在测试中检查nil值,但失败了。构造函数:typeAppstruct{someFieldstringlog*log.Logger}funcNew()*App{app:=&App{}f,err:=os.OpenFile("info.log",os.O_RDWR|os.O_CREATE|os.O_APPEND,0666)iferr!=nil{fmt.Printf("erroropeni

json - exec.Command 使用 JSON 负载转义变量

预先感谢您,因为我已经在这上面花了2天时间。这是一个有效的curl命令。curl-kulogin:pass-XPOST-H'Content-Type:application/json'-d'{"type":"page","title":"Testpage","space":{"key":"ITDept"},"body":{"storage":{"value":"BlankPage.","representation":"storage"}}}'https://confluence/rest/api/content我需要使用exec.Command来执行它。考虑到现在在Go中,我已经尝试

go - 使用 exec.Command 运行 sqlite3 .backup

我正在尝试按照此SO答案https://stackoverflow.com/a/25684912/426853给出的方式使用sqlite3.backup命令从Go备份数据库.以下始终在命令行中运行:sqlite3/home/pi/pgclogs/smartlog.db".backup'/home/pi/pgcdata/smartlog.db.bak'"我在Go中将其编码如下:funcDbBackup()(errerror){dbpath:="/home/pi/pgclogs/smartlog.db"bakpath:="/home/pi/pgcdata/smartlog.db.bak"c

go - 在 Go 脚本中运行 'syntax error: A unknown token can’ 时如何修复 `exec.Command` t go here'

我正在尝试在我的Go脚本中使用osascript运行此AppleScript命令,但出现错误0:1:syntaxerror:Aunknowntokencan'tgohere.(-2740)。这是在终端中运行时效果很好的命令!/usr/bin/osascript-e'onrun{f,c}'-e'告诉应用程序“Finder”将(POSIX文件f作为别名)的注释设置为c'-eend"/Users/computerman/Desktop/testfile.png""Hello,World"我下面的Go脚本实际上输出了上面的字符串,我可以直接在终端中剪切和粘贴它并且它可以工作。但是,运行Go脚本

go - 如何使用 exec.Command() 打开 Chrome 窗口?

我想编写一个Go程序来打开一个Chrome窗口,并将URL作为参数传递给它。所以我写了这样的东西。packagemainimport"os"import"os/exec"import"strings"funcmain(){args:="--app="+strings.Join(os.Args\[1:\],"")exec.Command("\\"C:\\\\ProgramFiles(x86)\\\\Google\\\\ChromeDev\\\\Application\\\\chrome.exe\\"",args).Start()}但是当我运行程序gorun".\abc.go""https

shell - Golang exec.Command 在退出代码不为零时返回 nil 错误

我正在尝试在Golang中运行一个命令,但看起来它丢失了退出代码,因为错误为nil:funcrunCommand()[]byte,error{cmd:=exec.Command("/bin/bash","-c","KUBECONFIG=/tmp/.kube/confighelmversion")cmd.Stdin=os.Stdincmd.Stderr=os.StderrstdOut,err:=cmd.StdoutPipe()iferr!=nil{returnnil,err}iferr:=cmd.Start();err!=nil{returnnil,err}bytes,err:=iout