草庐IT

go - 如何使用 golang 在 git repo 中 check out 特定的 SHA

我需要帮助使用golang将代码checkout到特定SHA编号的gitrepo 最佳答案 这实际上是一个Go问题,因为它指的是go-gitlibrary.因此,您可以执行以下操作:packagemainimport("fmt"git"gopkg.in/src-d/go-git.v4""gopkg.in/src-d/go-git.v4/plumbing")//Basicexampleofhowtocheckoutaspecificcommit.funcmain(){//Clonethegivenrepositorytothegive

Go time.Parse() 得到 “month out of range” 错误

我是Go的新手,我正在解析一个nginx时间格式字符串。你可以在这里查看我的代码:packagemainimport( "time" "log" "fmt")funcmain(){ //nginxtimeformat nginx_time:="03/Apr/2017:08:29:05+0800" t,err:=time.Parse("02/Jan/2016:15:04:05MST",nginx_time) iferr!=nil{ log.Fatal(err) } fmt.Println(t.Format("2006-01-0215:04:05"))}我收到以下错误:GOROOT=/u

go - 我收到错误 fatal error : runtime: out of memory while downloading video using 'go-ipfs-api'

我使用go-ipfs-api从ipfs下载了一个大文件,web访问下载。我收到一个fatalerror:runtime:outofmemory.如何修改我的代码?funcmain(){http.HandleFunc("/",download)http.ListenAndServe(":8080",nil)}funcdownload(whttp.ResponseWriter,r*http.Request){client:=shell.NewShell("http://127.0.0.1:5001")fd,err:=client.Cat("QmTcj7SfRf4vnLnCqnxMT7kut

windows - 有没有办法为 "DIR"命令转义空格

我知道dir要求您双引号包含空格的目录名称,但我不得不使用cmd/C不尊重双引号现在列出名称中有空格的目录似乎是不可能的,而CD命令根本不关心空格,执行>CDNewfolder会将您转到Newfolder没有任何问题。编辑我正在尝试从Go中调用它程序packagemainimport("bytes""fmt""os/exec")//thisfunctionwrapsup`exec.Command`funcCommandRunner(cmdstring)([]byte,error){//makestdoutandstderrbufferstosavetheoutputtovarstdou

go - 分配错误 : runtime: out of memory

我写了这段代码:packagemainimport("log")funcmain(){varc[]int64fori:=0;i此代码内存不足:fatalerror:运行时:内存不足。在每次迭代中,c都会被分配一个新的slice。所以上一个slice是不可达的。为什么GC似乎没有收集无法访问的内存? 最佳答案 每个c=make([]int64,10000000000都试图分配80GB(8*10,000,000,000字节)的内存。使用合理大小的分配(相对于实际内存的大小)和一切都按预期工作。例如,packagemainimport("

go - 为什么在这种情况下 go 不报告 "slice bounds out of range"?

这个问题在这里已经有了答案:Whydoesgoallowslicingfromlen(slice)?(3个答案)关闭4年前。这里是重现的代码:packagemainimport"fmt"funcmain(){varv[]intv=append(v,1)v=append(v,v[1:]...)fmt.Println("hi",v)}v[1]会报indexoutofrange,而v[1:]...不会,为什么呢?

memory - Golang, fatal error : out of memory on 1 TB RAM machine

我的代码包含一个巨大的uint8slice,其中包含近5.9亿个元素。我将此数组保留在代码中以使其尽可能快地运行。代码的最终大小为1.3GB。当我尝试编译它时,它引发了fatalerror:内存不足。与以下#command-line-argumentsfatalerror:outofmemoryruntimestack:runtime.throw(0x8fb3f2,0xd)/usr/local/go/src/runtime/panic.go:566+0x95runtime.(*mcache).refill(0x7f5c2afa3ba8,0x1440000000a,0x7f57dc46d

multithreading - goto out of main 和 print threads 的定义

我有两个关于Go代码的问题。这是我的程序。该程序控制昵称在“数据库”中的存在。packagemainimport"fmt"funcrcv(){ifr:=recover();r!=nil{fmt.Println("retry.")main()}}funcmain(){deferrcv()INSERT:fmt.Println("Insertanickname:")varsstringfmt.Scanln(&s)switch{caselen(s)我的问题是:使用恢复函数(rcv()),在它结束时,调用的main()像另一个线程一样运行?主体main()函数在第二个执行时结束,或者每当引发pa

go - 为什么可以将参数传递给 http.Dir ("."中的类型)?

在http包中,它定义了一个自定义类型(typeDirstring),然后向其添加了一个方法Open(https://golang.org/src/net/http/fs.go#L34)。当在别处调用时,它是由http.Dir(".")完成的,因为它是一个函数或方法。有人可以向我解释为什么以及这里发生了什么吗? 最佳答案 http.Dir(".")是类型转换。它将字符串"."转换为类型http.Dir。 关于go-为什么可以将参数传递给http.Dir("."中的类型)?,我们在Stac

go - 在 : panic: runtime error: index out of range 中转换数据结构

我在go中有一个数据结构:typeAPIMainstruct{CodeConvstring`json:"codeConv"`Starttime.Time`json:"start"`Endtime.Time`json:"end"`Details[]struct{IDPrmstring`json:"idPrm"`Keys[]struct{Timestamptime.Time`json:"timestamp"`Valuefloat64`json:"value"`}`json:"keys"`}`json:"details"`}我需要转换为:typeDataGroupedByTSstruct{C