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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件