douxie3625 2018-06-04 05:51
浏览 34
已采纳

在处理接口时,类型声明是返回结构指针的唯一方法吗?

Consider the following code:

package main

import "fmt"
// 5
type I interface {
    Foo() string
}

type a struct {
    i int
}

func (s a) Foo() string {
    return "We are here!"
}

func (s a) Bar() string {
    return "Major Tom!"
}
// 20
func main() {
    var x I = &a{i: 42}
    fmt.Println(x.Foo())
    fmt.Println(x.(*a).Bar())
}

The last statement of main gives me back the underlying struct but i need to export this struct to get back at it.

If I am using a package in a library where the only symbol exported is the interface (big I, little a in our example above between lines 5-20), then I am unable to cast the interface to the original type when I am in another package or file.

Because the original struct is stored in the interface, is there a simple way to get back its reference and use methods that haven't been declared in the interface and only attached to the struct.

  • 写回答

2条回答 默认 最新

  • duanliao6789 2018-06-04 07:42
    关注

    Yes, in the general case you need type assertion (or a type switch) to get back the wrapped value in an interface value.

    But you do not need to get back the stored, concrete value from an interface to be able to call other methods it has (and which are not part of the interface type).

    You may type-assert another interface value from an interface value, another interface value whose type contains the methods you do want to call.

    See this example:

    type Foo interface {
        Bar() string
    }
    
    type fooImpl struct{}
    
    func (fooImpl) Bar() string { return "Bar from fooImpl" }
    
    func (fooImpl) Baz() string { return "Baz from fooImpl" }
    
    func main() {
        var f Foo = &fooImpl{}
    
        if x, ok := f.(interface{ Baz() string }); ok {
            fmt.Println(x.Baz())
        } else {
            fmt.Println("f does not have a Baz() method!")
        }
    }
    

    Foo only has a Bar() method. We have a variable f of type Foo, the concrete type it stores is *fooImpl, which also has another method: fooImpl.Baz().

    So we can type-assert a value of type interface{ Baz() string } from it, and simply call Baz() on the result.

    The output of the above is (try it on the Go Playground):

    Baz from fooImpl
    

    Type-asserting an interface from another interface value does not require to export the type of the wrapped value.

    You may also create a new type for the interface type you type assert, anonymous type is not a requirement:

    type MyFoo interface{ Baz() string }
    
    if x, ok := f.(MyFoo); ok {
        fmt.Println(x.Baz())
    } else {
        fmt.Println("f does not have a Baz() method!")
    }
    

    Output is the same (try it on the Go Playground).

    Hell, you can even "extend" Foo with the additional methods, and type-assert an "extended" interface:

    type MyFoo interface {
        Foo
        Baz() string
    }
    
    if x, ok := f.(MyFoo); ok {
        fmt.Println(x.Bar())
        fmt.Println(x.Baz())
    } else {
        fmt.Println("f does not have a Baz() method!")
    }
    

    Now in this example x is both a Foo and a value that has a Baz() method. Output (try it on the Go Playground):

    Bar from fooImpl
    Baz from fooImpl
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛