草庐IT

multiple-interface-implem

全部标签

go - 如何在 go 中实现可比较的接口(interface)?

这个问题在这里已经有了答案:Structdoesnotimplementinterfaceifithasamethodwhoseparameterimplementsinterface(2个答案)关闭上个月。我最近开始学习Go并面临下一个问题。我想实现Comparable接口(interface)。我有下一个代码:typeComparableinterface{compare(Comparable)int}typeTstruct{valueint}func(itemT)compare(otherT)int{ifitem.value所以我得到了错误cannotuseTliteral(ty

go - 从不同的包golang实现接口(interface)

我在尝试实现一个在golang的不同包中定义的接口(interface)时遇到了一些问题。我对下面的问题进行了最少的重建接口(interface):packageinterfacestypeInterfaceinterface{do(paramint)int}实现:packageimplementationstypeImplementationstruct{}func(implementation*Implementation)do(paramint)int{returnparam}Main.go:packagemainimport("test/implementing-interfa

go - 从不同的包golang实现接口(interface)

我在尝试实现一个在golang的不同包中定义的接口(interface)时遇到了一些问题。我对下面的问题进行了最少的重建接口(interface):packageinterfacestypeInterfaceinterface{do(paramint)int}实现:packageimplementationstypeImplementationstruct{}func(implementation*Implementation)do(paramint)int{returnparam}Main.go:packagemainimport("test/implementing-interfa

syntax - 为什么 Go 方法接收类型不能是接口(interface)?

来自Godocumentationonmethoddeclarations:ThereceivertypemustbeoftheformTor*TwhereTisatypename.Tiscalledthereceiverbasetypeorjustbasetype.Thebasetypemustnotbeapointerorinterfacetypeandmustbedeclaredinthesamepackageasthemethod.谁能告诉我为什么会这样?是否有任何其他(静态类型)语言允许这样做?我真的很想在接口(interface)上定义方法,这样我就可以将给定接口(inte

syntax - 为什么 Go 方法接收类型不能是接口(interface)?

来自Godocumentationonmethoddeclarations:ThereceivertypemustbeoftheformTor*TwhereTisatypename.Tiscalledthereceiverbasetypeorjustbasetype.Thebasetypemustnotbeapointerorinterfacetypeandmustbedeclaredinthesamepackageasthemethod.谁能告诉我为什么会这样?是否有任何其他(静态类型)语言允许这样做?我真的很想在接口(interface)上定义方法,这样我就可以将给定接口(inte

interface - 具有接口(interface){}和类型断言的多个返回类型(在 Go 中)

我想知道调用具有多个返回值的函数的正确语法是什么,其中一个(或多个)类型为interface{}。返回interface{}的函数可以这样调用:foobar,ok:=myfunc().(string)ifok{fmt.Println(foobar)}但以下代码失败并出现错误multiple-valuefoobar()insingle-valuecontext:funcfoobar()(interface{},string){return"foo","bar"}funcmain(){a,b,ok:=foobar().(string)ifok{fmt.Printf(a+""+b+"\n")

interface - 具有接口(interface){}和类型断言的多个返回类型(在 Go 中)

我想知道调用具有多个返回值的函数的正确语法是什么,其中一个(或多个)类型为interface{}。返回interface{}的函数可以这样调用:foobar,ok:=myfunc().(string)ifok{fmt.Println(foobar)}但以下代码失败并出现错误multiple-valuefoobar()insingle-valuecontext:funcfoobar()(interface{},string){return"foo","bar"}funcmain(){a,b,ok:=foobar().(string)ifok{fmt.Printf(a+""+b+"\n")

go - 部分实现接口(interface)的最佳实践

部分提供默认实现的方法是什么?为了说明,以下切换开关驱动程序的简单示例是我遵循我的OO直觉而结束的死胡同......当然它不会编译(我知道为什么)而且我不一定愿意这样做所以。任何其他解决方案更好地适应go哲学实际上会更好地正确理解这种常见需求的go-way。完整的示例也可以在https://play.golang.org/p/MYED1PB-dS找到给定以下界面:typeToggleSwitchinterface{TurnOn()TurnOff()IsOn()boolToggle()}Toggle()是提供默认实现(即根据当前状态打开或关闭开关)的一个很好的候选者://TheToggl

go - 部分实现接口(interface)的最佳实践

部分提供默认实现的方法是什么?为了说明,以下切换开关驱动程序的简单示例是我遵循我的OO直觉而结束的死胡同......当然它不会编译(我知道为什么)而且我不一定愿意这样做所以。任何其他解决方案更好地适应go哲学实际上会更好地正确理解这种常见需求的go-way。完整的示例也可以在https://play.golang.org/p/MYED1PB-dS找到给定以下界面:typeToggleSwitchinterface{TurnOn()TurnOff()IsOn()boolToggle()}Toggle()是提供默认实现(即根据当前状态打开或关闭开关)的一个很好的候选者://TheToggl

pointers - 在 Golang 中将接口(interface)设置为 nil

我正在尝试将接口(interface)的内部值设置为nil,如下所示:typ:=&TYP{InternalState:"filled"}setNil(typ)fmt.Printf("Expectingthat%vtobenil",typ)而且我需要知道如何实现setNil(typinterface{})方法。更多详情seethiscodeinplay.golang.org. 最佳答案 问题是您没有接口(interface)值。你有一个指针值,一个指向具体类型的指针。这与接口(interface)值不同。如果你想改变任何类型变量的值,