jjzjj

interface-design

全部标签

go - 不能将接口(interface)的实现用作需要接口(interface)的 func 的参数

我收到以下错误:./main.go:31:cannotusetelegramService(typemessaging.TelegramService)astypemypackage.MessagingServiceinargumenttomypackage.RegisterMessagingService:messaging.TelegramServicedoesnotimplementmypackage.MessagingService(wrongtypeforHandleIncomingMessagemethod)haveHandleIncomingMessage(telegra

go - 将指针传递给接口(interface)时函数抛出错误?

这个问题在这里已经有了答案:"ispointertointerface,notinterface"confusion(2个答案)关闭4年前。所以这就是我遇到的,我不明白为什么会出错:packagemainimport("fmt")//defineabasicinterfacetypeIinterface{get_name()string}//defineastructthatimplementsthe"I"interfacetypeFoostruct{Namestring}func(f*Foo)get_name()string{returnf.Name}//definetwoprint

go - 接口(interface)的结构嵌入, panic : runtime error

我正在尝试一个与接口(interface)的结构嵌入相关的示例//https://talks.golang.org/2014/go4java.slide#52//Structembeddingofinterfaces//https://play.golang.org/p/SYiZ7M1OEhUpackagemainimport("bytes""fmt""net")//net.ConnhasReadandWritetypeloopBackstruct{net.Connbufbytes.Buffer}func(c*loopBack)Read(b[]byte)(int,error){fmt.

go - 如何将接口(interface)映射的映射转换为字符串

我是golang的新手,我有一种如下所示的变量:typeResultDatamap[string]map[string][]interface{}当我收到此变量中的数据时,如何在Go中将整个数据转换为单个字符串? 最佳答案 你可以使用类似Sprintf的东西:funcmain(){d1:=map[string][]interface{}{"a":[]interface{}{20,"hello"},"b":[]interface{}{100}}d2:=map[string][]interface{}{"x":[]interface{}

go - 与具有 interface{} 参数的方法的接口(interface)不起作用

我正在尝试在Go中实现一些接口(interface)。我有接口(interface)A:typeInterfaceAinterface{read(interface{})string}然后我有InterfaceB:typeInterfaceBinterface{fetch()}我有一个函数:funcRead(aInterfaceA){}我有StructA,它通过它的方法满足InterfaceA,但它没有变量“interface{}”,而是像这样传递给InterfaceB:typeStructAstruct{}func(a*StructA)read(bInterfaceB)string{

user-interface - 为什么找不到来自 andlabs/ui 包的组件

我正在尝试按照简单的代码(修改自here)来创建一个窗口:packagemainimport("github.com/andlabs/ui")funcmain(){ui.Main(makeMainWin)}funcmakeMainWin(){varbmiButton=ui.NewButton("First")varotherButton=ui.NewButton("Second")//ui.NewVerticalStackdoesnotwork;stack:=ui.NewVerticalStack(ui.NewLabel("Selectmodule"),bmiButton,otherB

go - 将接口(interface)指针片转换为结构指针片的最有效方法是哪种?

这个问题已经有了答案:Typeconvertingslicesofinterfaces5答最基本的问题是我得到了一个[]*interface{},我需要把它转换成[]*MyStruct。我在尝试这样的方法,但速度不快。在我的例子中,在示例代码中,lines片段包含映射,因此硬转换不起作用。varlines[]*interface{}varresults[]*MyStructfor_,s:=rangelines{ifs!=nil{someJson,err:=json.Marshal(s)iferr!=nil{continue}v:=MyStruct{}iferr:=json.Unmars

syntax - 戈朗 : how to get sub element from a []interface{} alias

我为[]interface{}定义了一个别名:typestate[]interface{}如何获取状态中的子项:functest(sstate){//Howtoget1stelementins?//orHowtoconvertsbackto[]interface{}?}test([]interface{1,2,3}) 最佳答案 test([]interface{1,2,3})是错误的,应该是test(state{1,2,3}).您还可以像访问任何slice一样访问s中的第一个元素,使用s[x]:typestate[]interfac

struct - 在将它作为接口(interface)传递并运行它的方法后,让你将结构恢复为它的数据类型?

我有一个结构如下typeMyStruct{EmbeddedFooBar}func(m*MyStruct)Foo(b*http.Request){//Doingsomething}funcfn(args...interfaces){//It'shereIwanttogetmystructbackandrunthe"Get"method//PleasekeepinmindIamtoopassapointerparamintothestructmethodstrt:=args[0]....getstructbacktostaticdatatypeMyStructandrun"Get()",d

php - 高语 : create a function that accept an interface (I came from PHP)

在PHP中我可以创建一个接口(interface)interfaceHello{publicfunctionbar();}以及一些实现它的类finalclassFooimplementsHello{publicfunctionbar(){//dosomething}}finalclassBarimplementsHello{publicfunctionbar(){//dosomething}}然后,我还可以创建一个接受该接口(interface)的NewClass::bar()方法。finalclassNewClass{publicfunctionbar(Hello$hello){//