doubeng3216 2018-11-21 23:07
浏览 53

鸭子输入:如何在Go中从一个接口隐式转换为另一个接口

I'm new to go and I wish to make two packages with (very) loosely coupled API between a uses and a provider. For this, I wish to use go's ability to implicitly implement interfaces and implicit conversion.

Both the provider and the user have their own defined interfaces (for exemple provider returns a provider.A and user accepts a user.A). With this pattern, I can convert from one type to the other instead of importing the interfaces from another package.

This works fine with simple interfaces, but as soon as a method takes an interfaces as an input, the conversion from one type to the other become impossible.

Why doesn't go allow this kind of conversion? Is there any workaround?

working example:

package main

// Provider

type A interface{
    AddString(string)
}


type a struct{
    b string
}
func (a *a) AddString(b string) {
    a.b = b
}
func NewA() A {
    return &a{nil}
}


// User

type A2 interface{
    AddString(string)
}

func Main() {
    var _ A2 = NewA()
}

example that cause problems:

package main

// Provider

type A interface{
    AddB(B)
}
type B interface{}


type a struct{
    b B
}
func (a *a) AddB(b B) {
    a.b = b
}
func NewA() A {
    return &a{nil}
}


// User

type A2 interface{
    AddB(B2)
}
type B2 interface{}

func Main() {
    var _ A2 = NewA() // error..
}
  • 写回答

1条回答 默认 最新

  • duanguo7021 2018-11-22 02:08
    关注

    Go provides type checking as a feature, not a bug. When you declare B and B2 to be separate types, the compiler respects this distinction and treats them differently.

    A familiar example is time.Duration, which is just an int64 to count nanoseconds, but you cannot mix and match variables that are int64 and time.Duration without an explicit type conversion. see time.Duration docs

    Rather than trying to work around Go's type checking feature, you should try to learn how to express your algorithm using idiomatic Go.

    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口