我可以在 Go 中定义一个带有 channel 的接口(interface)吗?我想定义一个接口(interface),允许我使用不同类型的对象,这些对象都定义了相同的 channel 。执行:
type I interface {
chan Communications []byte
otherMethod()
}
(这给出语法错误:意外的 token 陈。我尝试了几种不同的语法和一些谷歌搜索无济于事。)
最佳答案
接口(interface)不保存数据,它定义了实现的内容。
您可以有一个返回 channel 的方法。例如:
type I interface {
getChannel() (chan []byte)
otherMethod()
}
请阅读接口(interface)。 tour会有帮助。
关于go - 在 Go 中定义一个带有 channel 的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796079/