草庐IT

戈朗 : Parse bit values from a byte

全部标签

戈朗 : How do you use a pointer on a struct that hasn't been initialized yet

所以我在看filehere.他们调用record:=&accessLog但他们从来没有首先将其初始化为变量,如果他们这样做,如果有多个同时连接,记录是否有可能被覆盖用别人的数据?typeaccessLogstruct{ip,method,uri,protocol,hoststringelapsedTimetime.Duration}funcLogAccess(whttp.ResponseWriter,req*http.Request,durationtime.Duration){clientIP:=req.RemoteAddrifcolon:=strings.LastIndex(cli

戈朗错误 : 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

arrays - 戈朗 : using nested structs

我有两个结构:typepersonstruct{namestringageint}typeclassstruct{students[]person}假设在main函数中,我创建并填充了两个person变量,然后我想将它们添加到具有类类型的变量中。我该怎么做?即s:=person{name:"Sean",age:50}t:=person{name:"Nicola",age:35}我如何将s和t放入:lab:=class? 最佳答案 下面应该实现你想要的:lab:=class{[]person{s,t}}测试一下here.在您继续您的项

戈朗 : Wait x amount of time before looping again without starting new goroutine

我有一个循环,需要等待一段随机时间才能再次循环。我有什么:for{rand.Seed(time.Now().UnixNano())r:=rand.Int()//Dostufft,_:=time.ParseDuration(string(r)+"ms")time.Sleep(t)}不幸的是,循环会运行多次,就像time.Sleep不工作一样。 最佳答案 您应该检查当前从t,_:=time.ParseDuration中丢弃的错误:您传递给Sleep的time.Duration处于零值,这会导致函数休眠0纳秒。更改#1:处理错误t,err

戈朗 : struct embedding vs "aliasing"

我知道结构嵌入typenewTypestruct{someStruct}我知道类型别名:typenewType=someStruct但是有什么实用的理由可以使用typenewTypesomeStruct原始类型呢?typenewTypeint这样的定义的正确名称是什么? 最佳答案 命名:所有片段都是typedeclarations。其中一个声明是typealias(带=的那个)。剩下的声明是typedefinitions。这些定义中的第一个使用带有嵌入式字段的结构。代码typenewTypesomeStruct在想要定义一个与其他结

戈朗 : sha256 returns two different values for the same input

我正在努力实现一些需要哈希操作的加密函数(我想计算自定义结构的哈希)所以我想使用crypto中的SHA256哈希函数golang包。但是,我注意到,当我针对同一输入多次运行哈希函数时,有时它会返回不同的值。我的理解是SHA函数为单个输入值返回相同的哈希输出。下面是我对哈希函数的实现:funcmyHash(sMyStruct)[]byte{bytes:=[]byte(fmt.Sprintf("%v",s))h:=sha256.New()h.Write(bytes)returnh.Sum(nil)}myStruct有以下字段:typeMyStructstruct{elliptic.Curv

regex - 戈朗 : Remove all characters except | from string

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭5年前。Improvethisquestion我需要删除除“|”以外的所有字符和字符串中的空格。我不明白如何在Go中执行此操作。请帮忙。字符串可能如下所示:|||||||||||||||||||||||||hello|我需要它来返回这个:||||||||||||||||||||||||||提前致谢!

csv - 戈朗 : convert text dump to CSV

我有一个txt文件转储,其中包含以下行格式的数据:2015/01/01-01:00:00,{'a50':15.5,'a95':14.5,'a99':21.5}我想提取值并将其转换为以下格式的CSV:2015/01/01,15.5,14.5,21.5到目前为止,我可以读取文件并提取数据,我还可以使用正则表达式获取时间戳值(但我知道Go中的正则表达式非常慢),我避免做更多的正则表达式来提取剩余的值。有什么指点吗? 最佳答案 如果时间允许,我可能会根据需要使用代码示例跟进此问题,但这里是我将如何处理该问题的基本纲要。1)创建一个类似下面的

戈朗 : Hello world doesn't print to screen and program doesnt exit

当我使用命令“go.exerunmain.go”运行以下代码时,程序不会在屏幕上打印文本或退出。packagemainimport"fmt"funcmain(){fmt.Println("Helloworld")}go.exe版本=go版本go.1.5.1windows/amd64设置GOARCH=386设置GOBIN=设置GOEXE=.exe设置GOHOSTARCH=386设置GOHOSTOS=windows设置GOOS=windows设置GOPATH=C:\project设置GORACE=设置GOROOT=C:\Go设置GOTOOLDIR=C:\Go\pkg\tool\window

戈朗 : How to run the same logic at the beginning of every struct member functions?

例如,我想打印出某个结构的每个函数的函数名。除了我在每个成员函数的开头使用fmt.Println,还有什么更好的方法吗? 最佳答案 packagemainimport"fmt"import"runtime"funcmain(){pc,_,_,_:=runtime.Caller(0)fmt.Println("Nameoffunction:"+runtime.FuncForPC(pc).Name())fmt.Println()//or,defineafunctionforitfmt.Println("Nameoffunction:"+f