jjzjj

sysfs-interface

全部标签

go - 如果不是隐式调用接口(interface),如何访问函数?

下面是一个结构Config,它包含一个匿名函数ReturnNewAddress,它返回一个net.Conn接口(interface)。ReturnNewAddress然后用于返回“地址”。typestructConfig{ReturnNewAddressfunc(net.Conn,error)}在下面调用匿名函数ReturnnewAddress的地方,请注意cfg是Config的一个实例。addr,err:=cfg.ReturnNewAddress()所以我的问题来了-考虑到接口(interface)拥有许多不同的功能,接口(interface)net.Conn如何知道要使用什么功能?

go - 如何在接口(interface)上使用 strings.Contains

我收到以下代码行的panic。接口(interface)转换:interface{}是[]string,不是string我的界面是一个字符串映射。查找图形界面是否包含特定字符串的最佳方法是什么。ifstrings.Contains(figure["figure1"].(string),"one"){} 最佳答案 这里你的类型断言是错误的,你正试图从你界面上的键访问一个值,它还没有作为映射访问,所以这不能作为interface{}工作。不可索引。相反,您要做的是将整个界面转换为map[string]string像这样stringMap

go - 鸭子打字 : How to implicitly convert from an interface to another interface in go

我是新手,我希望在用户和提供者之间使用(非常)松散耦合的API制作两个包。为此,我希望利用go的隐式实现接口(interface)和隐式转换的能力。提供者和用户都有自己定义的接口(interface)(例如,提供者返回一个提供者.A,用户接受一个用户.A)。使用这种模式,我可以从一种类型转换为另一种类型,而不是从另一个包中导入接口(interface)。这适用于简单的接口(interface),但一旦方法将接口(interface)作为输入,从一种类型到另一种类型的转换就变得不可能了。为什么go不允许这种转换?有什么解决方法吗?工作示例:packagemain//Providertyp

string - 如何在 Golang 中使用 Append 方法。语法要求接口(interface)?

我正在使用Split方法从两个单独的字符串(str1,str2)中检索单词,并将它们全部append到另一个数组(str)中packagemainimport("fmt""strings")funcmain(){Name:="RedBlueGreen"Address:="NewYorkParisFrance"str1:=strings.Split(Name,"")str2:=strings.Split(Address,"")str:=append(str1,str2)fmt.Println(str)}我收到了错误:不能在追加中使用str2(type[]string)作为类型字符串去Pl

go - 将接口(interface)与 nil 进行比较

在Google表格中example底部有一段代码循环遍历电子表格中的行:for_,row:=rangeresp.Values{//PrintcolumnsAandE,whichcorrespondtoindices0and4.fmt.Printf("%s,%s\n",row[0],row[4])}但是,如果由于引用row[0]而导调用子表格中存在空行,则此代码会出错什么时候row是大小为二的空接口(interface)(length:0,cap:0)一个简单的if语句来检查是否row为空不能作为row==nil显示false.我如何检查row是空的吗? 最佳

go - 如何模拟接口(interface)实现

我的界面.gotypeMyInterfaceinterface{fun1()stringfun2()intfun3()bool}funcFoo(miMyInterface)string{returnmi.fun1()}我的接口(interface)测试.gotypeMyInterfaceImplementationstruct{}func(miMyInterfaceImplementation)fun1()string{return"foobar"}func(miMyInterfaceImplementation)fun2()int{returnint(100)}func(miMyIn

go - 如何使用构建器模式构造动态实现接口(interface)的结构

我正在尝试使用builderpatterns(从Java借来的)允许结构实现接口(interface)。例如,理想情况下我会喜欢这种代码模式:packagemainimport"fmt"typeOnerinterface{One()int}typeTwoerinterface{Two()int}funcmain(){s:=NewObject().WithOne(1).Build()_,ok:=s.(Oner)fmt.Println(ok)//Printstrue_,ok=s.(Twoer)fmt.Println(ok)//Printsfalset:=NewObject().WithOn

go - 关于接口(interface)分配的困惑

这个问题在这里已经有了答案:MethodSets(PointervsValueReceiver)(3个答案)关闭3年前。我对下面的Go代码很困惑。谁能告诉我为什么worker=u和work=&u是否有效?worker=p有效吗?worker=&p无效?User和People有什么区别?packagemainimport("fmt")typeWorkerinterface{Work()}typeUserstruct{namestring}func(uUser)Work(){}typePeoplestruct{namestring}func(p*People)Work(){}funcmai

go - 传播时 "Cannot use variable of type []struct as []interface"

这个问题在这里已经有了答案:sliceofstruct!=sliceofinterfaceitimplements?(6个答案)关闭8个月前。原型(prototype)函数functest(i...interface{}){//Codehere}预期用途typefoostruct{//Fields}foos:=[]foo{//foo1,foo2...}test(foos...)//ERRORtest(foos[1],foos[2],...)//OK错误cannotusefoos(variableoftype[]foos)as[]interface{}valueinargumenttot

oop - Golang 在具有私有(private)访问权限的结构中嵌入接口(interface)

我想以尽可能最惯用的方式在Golang中复制以下Java代码:publicclassHandler{privateStoragestorage;privateMappermapper;publicHandler(Storagestorage,Mappermapper){this.storage=storage;this.mapper=mapper;}publicvoidhandleKey(Stringk){storage.put(k,mapper.map(k));}}interfaceStorage{publicvoidput(Stringk,Stringv);publicString