草庐IT

golang 同步/原子包?

我写了一段代码记录请求数。packagemainimport("log""net/http""runtime""sync/atomic")varcountint32=0functest(whttp.ResponseWriter,r*http.Request){count=atomic.LoadInt32(&count)atomic.AddInt32(&count,1)log.Println("count:",count)}funcmain(){runtime.GOMAXPROCS(runtime.NumCPU()-1)http.HandleFunc("/",test)http.List

go - 如何通过 Golang 中的短变量声明将返回值分配给函数输入?

考虑以下代码片段...f,err:=os.OpenFile(".",os.O_RDONLY,0666)为了检查错误并继续,我们需要有类似的东西..funccheckerr(errerror){iferr!=nil{panic(err)}}然后调用这个小函数来验证错误。例如..f,err:=os.OpenFile(".",os.O_RDONLY,0666)checkerr(err)files,err:=f.Readdirnames(0)checkerr(err)虽然这可行,但我正在寻找一种在初始化阶段本身直接调用checkerr()的快捷方式。见下文...f,checkerr(err):

json - 在 Golang 中解析 JSON 以绘制多边形

我正在用Go解析一个大型JSON文件,我只需要从JSON中提取特定的项目。它通常是一个非常大的文件,但最终,它变成了这样:"textAnnotations":[{"boundingPoly":{"vertices":[{"x":136,"y":119},{"x":5606,"y":119},{"x":5606,"y":3985},{"x":136,"y":3985}]},"description":"Description","locale":"en"},{"boundingPoly":{"vertices":[{"x":3420,"y":122},{"x":3439,"y":122}

golang udp客户端无法从服务器接收消息

我可以成功地从客户端向服务器发送消息。但是当我尝试回复消息给客户端时,客户端却无法接收到消息。客户:conn,_:=net.Dial("udp",serv_addr:port)deferconn.close()buf:=[]byte("Hey,server")conn.Write(buf)recv:=make([]byte,1024)fmt.Println("Reading...\n")conn.Read(recv)服务器:addr,_:=net.ResolveUDPAddr("udp",addr:port)msg:=make([]byte,1024)conn,_:net.Listen

unit-testing - 在 golang 中模拟远程 api 调用

我正在尝试更好地编写调用远程api的模拟golang测试我可以使用httptest库很容易地模拟单个调用,但我是在处理多次调用单个端点调用的其他函数时卡住了。例如给定一个简单的创建函数funccreateItem(urlstring,productProduct)(int,error){//makerequestreturncreatedId,nil}我可以写一些像这样的测试funcTestCreateItem(t*testing.T){mock_ts:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*ht

go - 在 golang 中,可以使用任何类型的 slice 变量的结构吗?

简单的golang应用给出以下错误.\test.go:13:cannotuseds(typeData_A)astype[]interface{}infieldvalue下面的代码packagemaintypeData_Astruct{astring}typeDTResponsestruct{Data[]interface{}`json:"data"`}funcmain(){ds:=Data_A{"1"}dtResp:=&DTResponse{Data:ds}print(dtResp)}我想要一个带有任何类型slice变量的结构。使用struct{}会产生同样的错误。在Java中,我可以

go - 如何在 golang 中正确使用 crypto/rsa Decrypt 函数?

func(priv*PrivateKey)Decrypt(randio.Reader,ciphertext[]byte,optscrypto.DecrypterOpts)(plaintext[]byte,errerror)以上是golangcrypto/rsa库中的函数。我不太明白这里对参数opts的解释。Decryptdecryptsciphertextwithpriv.Ifoptsisniloroftype*PKCS1v15DecryptOptionsthenPKCS#1v1.5decryptionisperformed.Otherwiseoptsmusthavetype*OAEP

unit-testing - 如何在 Golang 中模拟结构方法

我正在阅读这个页面(我没有使用亚马逊,只是为了golang教育而阅读)https://aws.amazon.com/blogs/developer/mocking-out-then-aws-sdk-for-go-for-unit-testing/当我自己尝试时,我遇到了类型错误。typeQueuestruct{ClientThirdPartyStructURLstring}typemockedReceiveMsgsstruct{ThirdPartyStructRespValueIWantToMock}q:=Queue{Client:mockedReceiveMsgs{}}当我尝试做完全

golang 包执行问题

我在使用goexec包时遇到问题。我收到这个错误:'\"C:\ProgramFiles\SublimeText3\sublime_text.exe\"'isnotrecognizedasaninternalorexternalcommand,operableprogramorbatchfile.当我尝试在Windows机器上用golang运行这段代码时:packagemainimport("os/exec""fmt")funcmain(){varCMDstring="\"C:\\ProgramFiles\\SublimeText3\\sublime_text.exe\"C:\\User

mongodb - 如何使用golang将数组对象数据转换为字符串格式数据?

有一个数组对象,它是从mongodb中检索到的。数据如下所示:-[{1fruitsAppleAppleismyfavoritefruit.}{2colorsRedRedcolorisalwayscharming.}{3flowersLotusItisoneofthemostbeautifulflowersinthisworld.}]这是获取上述数据的代码结构是:typeItemstruct{Idint`json:"id"`Categorystring`json:"category"`Namestring`json:"name"`Descriptionstring`json:"descr