duanliexi1052 2017-08-05 21:12
浏览 36
已采纳

在Go中,我可以返回符合接口的结构但无法访问该接口吗?

I think the best way to explain this is by example, so here it is:

package main

import (
    "fmt"
)

// Greeter greets with a Greeting.
type Greeter interface {
    Greet() Greeting
}

// A Greeting has a string representation.
type Greeting interface {
    String() string
}

type Hello struct {}

// Hello greets by returning itself...
func (h *Hello) Greet() *Hello {
    return h
}

// ...because Hello also has a string representation.
func (h *Hello) String() string {
    return "Hello"
}

// But Go says Hello doesn't implement Greeter.
func main() {
    var g interface{} = &Hello{}
    g, ok := g.(Greeter)
    fmt.Println(ok)
}

This prints false. You can run and play with it: https://play.golang.org/p/A_2k_ku_Q2

In my real case the struct Hello and the interfaces for Greeter and Greeting are in different packages that do not import each other and I wanted to keep it that way. I'm perhaps missing some understanding of interfaces in Go but after reading so much about it I still can't put my finger on it. Would you guys have any pointers for me? Maybe another approach for the problem? Thanks!

  • 写回答

1条回答 默认 最新

  • douchai7891 2017-08-06 01:50
    关注

    As stated in the comments, problem here is once your interface has this signature:

    type Greeter interface {
        Greet() Greeting
    }
    

    The any valid implementation must use exactly Greeting as the return type.

    But, as the documentation shows, you don't need to give the interface a name:

    https://golang.org/ref/spec#Interface_types

    In order to be able to implement what you need, you might declare the interface directly in the return value, without giving it a name.

    // Greeter greets with anything that has a String() method
    type Greeter interface {
        Greet() interface{ String() string }
    }
    

    Then your Greet() function for Hello can do this:

    // Hello greets by returning itself...
    func (h *Hello) Greet() interface{ String() string } {
       return h
    }
    

    Find here a modified playground showing the working example:

    https://play.golang.org/p/HteA_9jFd4

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法