草庐IT

Python函数知识点

全部标签

go - golang中的函数类型参数匹配

packagemaintypegensfunc(args...interface{})intfuncsum1(aint,bint,cint)int{returna+b+c}funcsum2(a...interface{})int{ret:=0for_,v:=rangea{ret=ret+v.(int)}returnret}funcmain(){varagens//a=sum1a=sum2println(a(1,2,3))}考虑上面的代码,sum2可以工作但sum1不行。编译器说“不能在赋值中使用sum1(类型func(int,int,int)int)作为类型gens”我问的原因是因为我

go - 如何使函数的参数是 "generic"结构

比如我想写这样一个方法:funcparseData(rawData[]json.RawMessage)[]interface{}{varmigrations[]interface{}for_,migration:=rangerawData{//thisisancustomstructcommand:=UserCommand{}json.Unmarshal(migration,&command)migrations=append(migrations,command)}returnmigrations}这段代码的问题是:如果我不想解析UserCommand,而是解析任何其他的,比如Pro

go - twoSum 函数对不同的数组输入有不同的行为

当我运行以下代码时,我得到了预期的答案[3,4],这是加起来成为我的目标变量的2个数字的索引。但是,当我将myArray输入更改为[]int{1,2,3,4,6,11,4,12}(我删除了最后6个)时,我感到panic。请帮助我理解为什么会这样。functwoSum(nums[]int,targetint)[]int{length:=len(nums)-1fori:=rangenums[:length]{forj:=rangenums[i+1:]{ifnums[i]+nums[j]==target{return[]int{i,j}break}}}panic("shouldneverha

go - 在golang中从另一个内部调用的模拟函数

我正在尝试stubos.Stat和ioutil.ReadFile(path)使用下面的代码或者如果你喜欢这里在goplayground[1]packagemainimport("fmt""io/ioutil""os""strings")funcAssignFileValueFrom(pathstring,val*string){var(tempValue[]byteerrerror)if_,err=os.Stat(path);err==nil{iferr!=nil{fmt.Println("Therewasaosstaterror:",err)}tempValue,err=ioutil

unit-testing - 用单元测试覆盖主函数中的代码

Golang显示我只有50%的覆盖代码,而且我看到main中的代码没有被覆盖,我尝试搜索但没有找到任何解释如何覆盖main中的代码的内容。ma​​in.gopackagemainfuncSum(xint,yint)int{returnx+y}funcmain(){Sum(5,5)}ma​​in_test.gopackagemainimport("testing")funcTestSum(t*testing.T){total:=Sum(5,5)iftotal!=10{t.Fail()}} 最佳答案 测试文件通常紧挨着他们测试的代码。根

go - 一段任意结构的接口(interface)用作函数参数(golang)

我的应用程序中有两种不同类型的结构。我将把它作为一个简化的例子来展示:typetypeAstruct{fieldA1intfieldA2string}typetypeBstruct{fieldB1float32fieldB2bool}首先我初始化它们的slice,然后我想将它们存储在数据库中。a:=[]typeA{{10,"foo"},{20,"boo"},}b:=[]typeB{{2.5,true},{3.5,false},}我的第一次尝试是迭代第一个slice,然后迭代第二个slice。它工作得很好,但看起来不像DRY.代码明显重复:printBothArrays(a,b)//..

go - 将项目附加到函数中的 slice 不会改变原始 slice

有人可以解释为什么两者不等同吗?后者确实构建了,但没有按预期工作。我认为slice会自动更改,因为包含指向数组的指针。//工作规范funcTestProcessRecords(t*testing.T){varmessageSent[]*sqs.SendMessageInputw:=&SQSWriter{queueURL:aws.String("aQueueURL"),service:&mock.SQS{SendMessageStub:func(input*sqs.SendMessageInput)(*sqs.SendMessageOutput,error){messageSent=ap

java - 如何使用 JNA 为具有多个返回值的 go 函数编写接口(interface)

我正在尝试导出一些Go函数并在Java中调用它们,使用JNA,但我不知道如何在Java中为具有多个返回值的Go函数定义接口(interface)。假设Go函数是://exportgenerateKeysfuncgenerateKeys()(privateKey,publicKey[]byte){return.....}返回值有两项,但在Java中,只允许有一项返回值。我能做什么? 最佳答案 cgo为多个返回值创建专用的C结构,并将各个返回值作为结构元素。在您的示例中,cgo将生成/*ReturntypeforgenerateKeys

python - 无法使用python客户端连接到go grpc服务器

我有一个在Go中运行的grpc服务器。我无法使用python客户端调用方法。不知道出了什么问题。我收到以下错误_RPC的会合以(StatusCode.UNIMPLEMENTED,method:/com.test/myMethod)>结束知道哪里出了问题吗?Go客户端能够正常通信。我还按照说明生成了stubhttps://grpc.io/docs/tutorials/basic/python.htmlpython-mgrpc_tools.protoc-I../../protos--python_out=.--grpc_python_out=.../../protos/route_guid

go - 如何找到函数类型的实现?

在Go中,可以像这样创建函数类型(https://golang.org/ref/spec#Function_types)typePrinterfunc(sstring)如何找到满足该类型的所有函数?例如,如果我有下面的文件,我如何才能发现consolePrinter是一个Printer?packagemainimport"fmt"funcmain(){printToScreen(consolePrinter)}//Printerdefinesafunctionthatprintsastring.typePrinterfunc(sstring)funcprintToScreen(pPri