草庐IT

INTERFACE

全部标签

list - 为什么将列表整数元素转换为字符串时得到 "interface conversion: interface is int32"?

Go新手...我编写了一个程序来删除存储在列表中的重复整数。当我为removeDuplicates函数运行以下测试时,我收到以下指向此行的错误:linked_test中的testString+=strconv.Itoa(e.Value.(int))。去吧。为什么会这样,我该如何解决?我将整数存储在testList中并使用e.Value获取它们并使用.(int)进行类型转换。panic:interfaceconversion:interfaceisint32,notint[recovered]panic:interfaceconversion:interfaceisint32,notin

sockets - 如何在 TLS 监听器中验证对等证书

我正在编写一个TLS监听器并接受新连接。在继续之前,必须根据SHA1列表验证新连接。问题是tls.Listen()返回接口(interface)net.Listener的监听器。这意味着Accept()将产生简单的net.Conn而不是提供ConnectionState()的tls.Conn。PeerCertificates.接受的net.Conn太基础了,但是当我转换为更具体的*tls.Conn类型时,我收到运行时异常。packagemainimport("crypto/tls""fmt""net")typeServicestruct{listenernet.Listener}fun

arrays - 数组在 Go 中的功能是否与在 Ruby 或 Python 中的功能相同?

在Ruby中,数组可以容纳字符串或整数,在Javascript和Python中似乎也是如此。但是在Go中,将整数和字符串放在一起似乎很困难,或者至少我无法弄清楚。在Go中,数组是否能够像Python和Ruby一样接受整数和字符串?ruby:a=[20,"tim"]putsapython:a=[20,"tim"]print(a)开始:? 最佳答案 因为Go是一种有类型的语言,所以在Go中创建多个类型的slice,需要指定一个多个类型都能满足的类型。要在Go中执行此操作,请创建一个空接口(interface)(interface{})的

pointers - 指向具有保存类型的接口(interface)的指针

解释我的问题的最短方法是thatcode:variinterface{}//Ican'tchangeit.Infactthisisafunction,i=Item{10}//thatreceivesinterface{},thatcontainobject(notpointertoobject!)fmt.Printf("%T%v\n",i,i)//fmt.Println(i.(NextValuer).NextVal())//won'tcompilei=&ifmt.Printf("%T%v\n",i,i)//thereiispointertointerface{}(nottoItem)/

go - 如何从接口(interface)获取 chan 值并在 reflect.Select(...) 中使用它

我举了一个反射的例子。选择自:https://www.socketloop.com/references/golang-reflect-select-and-selectcase-function-example它会尽其所能。但它是从一个简单的创建reflect.Value()“chan”:=make(chanint)设置。但我想使用来自作为接口(interface)传递的结构的channel{}。所以我修改了程序以创建一个结构并将其传递给处理接口(interface)参数。运行时我得到:panic:reflect:callofreflect.Value.ElemonstructVal

go - 如何使用 Golang 从一个函数返回不同类型的结构?

我有一个查询数据库的函数,然后,根据它的结果,可以创建结构OrderWithoutDetails或OrderWithDetails,具体取决于订单详细信息的存在。如何使函数能够返回两种类型的结果? 最佳答案 您可以使用接口(interface){}funcqueryDb()interface{}{}但是如果你的2类型的结构可以有一个共同的功能,可以满足一个共同的接口(interface),那就更好了,它会更干净。示例:types1struct{idintnamestring}types2struct{idintageint}type

go - 如何获取对接口(interface)值的引用

是否有可能在没有的情况下获得对接口(interface)值的引用反复反射(reflection)?如果不是,为什么不呢?我尝试过:packagefootypeFoostruct{a,bint}funcf(xinterface{}){varfoo*Foo=&x.(Foo)foo.a=2}funcg(fooFoo){f(foo)}但它失败了:./test.go:8:cannottaketheaddressofx.(Foo) 最佳答案 如果你按照断言的意思来消除你的疑虑stateafactorbeliefconfidentlyandfor

oop - 通过golang中的接口(interface)模拟功能

我正在尝试编写一个单元测试代码形式,其代码具有如下3级函数调用:主函数调用函数A(),然后函数A根据某些条件调用函数B()和C(),函数B调用函数E()和F(),而函数C调用函数G()和H()在某些条件下。上面就像我开发的代码,这里我想为函数B模拟函数E()和F(),为函数C模拟G()和H()。请建议我如何使用接口(interface)来实现。 最佳答案 Abstractfunctiontype您可以通过依赖注入(inject)而不是使用接口(interface)来做到这一点:import("fmt""math")typeafunc

go - 创建一个接口(interface),该接口(interface)是另一个接口(interface)的一部分

我想做如下的事情:typeModelinterface{EntityType()stringGetKey()*datastore.KeySetKey(*datastore.Key)errorPreSave(context.Context)errorPostSave(context.Context)errorPostLoad(context.Context)error}typeModels[]Modelinterface{Prepare(int)([]Model,error)}因此结构Models也是一个接口(interface),将由实现Model的结构的一部分实现。类似于以下内容:t

go - 返回接口(interface)时的类型断言

我是golang的新手;然而,根据我目前的知识,我知道value-type和reference-type都可以实现一个接口(interface)。但就类型断言而言,返回结构的方式似乎很重要。请参阅以下内容:packagemainimport("fmt")typeSomeErrorinterface{Error()string}typeConcreteErrorstruct{}func(ConcreteError)Error()string{return"?"}funcreturnPointer()SomeError{return&ConcreteError{}}funcreturnVa