草庐IT

module_function

全部标签

function - 如何在golang中仅显示多值结果的一个输出

最近在研究Golang一个函数可以返回多个结果。所以我写了一个函数:funcstore(x,yint)(int,int){returnx+y,x-y}在这之后我写了下面的代码:funcmain(){a,b:=store(6,4)fmt.Println(a,b)}结果是:102这工作正常。但是如果我想只打印一个,那我该怎么做呢?funcmain(){a,b:=store(6,4)fmt.Println(a)}结果:tmp/sandbox683412938/main.go:12:19:bdeclaredandnotused还有,为什么我不会写:funcmain(){a:=store(6,4

function - 计算函数被调用了多少次

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭5年前。Improvethisquestion如何在go中优雅地完成它?在python中我可以使用这样的属性:deffunction():function.counter+=1function.counter=0go是否有同样的机会?

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

Go Modules - 本地包不可访问?

所以,我是Go的新手。我创建了一个go.mod文件modulegithub.com/austin/test-project我在最新的git提交中添加了一个标签v0.0.1。根据我试图理解的一些示例,我将这些导入添加到我的go文件中,其中common是一个包,dynamo是另一个包,而导入来自名为main的第三个包。import("github.com/austin/test-project/common""github.com/austin/test-project/db/dynamo/playerstateddb""github.com/austin/test-project/db/

Golang : Recursive function for reconnecting a TCP client. .. 坏主意?

我有这个有效的TCP客户端代码。当它在TCP连接上写入或读取失败时,它会使用递归函数tcpReconnect()创建一个新连接。这安全吗?它会填满RAM吗?它可能会在几天(周末或节假日)后尝试重新连接。此代码是监视工业机器状态的驱动程序的一部分。也许这个问题有更好的解决方案。我找不到。PS:我不喜欢投票packagemainimport("fmt""net""time")varpollTime=1000//msvarhost="127.0.0.1"varport="11000"funcmain(){finished:=make(chanbool)goDriver()

function - 结构嵌入、函数输入、多态性

我有一个父结构:typeBigPolystruct{Value[]*ring.Poly}还有两个子结构:typePlaintextBigPolytypeCiphertextBigPoly我想要有接受明文和密文的函数。我的解决方案是使用以下形式的函数:funcAdd(a*Ciphertext,binterface{})(*Ciphertext)并使用switch-case来决定要做什么,但我发现这很麻烦,如果输入的数量增加,它会导致非常复杂的情况。然而,由于Plaintext和Ciphertext具有完全相同的结构和内部变量,只是名称不同,是否可以创建一个以更简洁的方式同时接受Plain

转到库 : cannot find module providing package lib

我正在创建3个独立的go项目:ace、aces-client和两个项目共享的库aceslib。根据go-documentation(https://golang.org/doc/code.html#Library),我在包含共享库时遇到了问题aceslib中的所有go文件共享包名aceslib。我使用importlib"aceslib"将库包含在ace和aces-client中。我可以使用gobuild构建库,并使用goinstall安装它,在目录列表中可以看到文件go/pkg/windows_amd64/aceslib.a被创建。但是当我尝试构建ace或ace-clientgo时提示

function - 将函数体/定义作为参数传递给 golang 中的函数调用

最近在使用golang的过程中遇到了如下问题。将函数体传递给函数调用是否可以,例如javascript。例如setTimeout(function(i){console.log("input:",i)},1000).在javascript中将匿名函数传递给另一个函数是很常见的。我想知道在go中是否相同?packagemainimport("fmt")typeHandlerFuncfunc(int)funcmain(){//defineafunctionasobject/variable?hnd:=func(inint){fmt.Println("funchandlerreturnsin

function - 在 Go 中将函数作为参数传递

我尝试在Go中创建一个函数,用于重试任何失败的查询函数(通常是因为序列化问题)。funcretryer(functionAfunc(interface{})(interface{},[]error),maxRetryint,waitBetweentime.Duration)interface{}{//whennoerrorinfunctionA,retryerreturnswhateverfunctionAreturns//whenmaxRetryisreached,returnsnil}我想重试的函数看起来像这样funcGetTopStudent(classIdstring)([]S

戈朗 : Passing structs as parameters of a function

尝试通过在线类(class)自学围棋。而且我正在尝试稍微偏离路线以扩展我的学习。该类(class)让我们使用几个变量编写一个简单的函数,该函数将获取这两个变量并打印出一行。所以我有:funcmain(){vargreeting:="hello"varname:="cleveland"message:=printMessage(greeting,name)fmt.Println(message)}funcprintMessage(greetingstring,namestring)(messagestring){returngreeting+""+name+"!"}稍后类(class)介