Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Go中的通道定义接口_Go - Fatal编程技术网

使用Go中的通道定义接口

使用Go中的通道定义接口,go,Go,我可以在Go中定义具有通道的接口吗?我想定义一个接口,允许我使用不同类型的对象,这些对象都定义了相同的通道。EXE: type I interface { chan Communications []byte otherMethod() } 这将导致语法错误:意外的令牌更改。我尝试了一些不同的语法和一些谷歌搜索,但没有结果。一个接口不包含数据,它定义了一些东西的实现 您可以有一个返回通道的方法。例如: type I interface { getChannel() (c

我可以在Go中定义具有通道的接口吗?我想定义一个接口,允许我使用不同类型的对象,这些对象都定义了相同的通道。EXE:

type I interface {
    chan Communications []byte
    otherMethod()
}

这将导致语法错误:意外的令牌更改。我尝试了一些不同的语法和一些谷歌搜索,但没有结果。

一个接口不包含数据,它定义了一些东西的实现

您可以有一个返回通道的方法。例如:

type I interface {
    getChannel() (chan []byte)
    otherMethod()
}

请仔细阅读接口。这会有所帮助。

您可能应该先查看文档,然后再问:谢谢您的快速回复。在发布之前,我完成了整个巡演,但我希望有更多的功能。我想最近我编程的C++太多了。您的方法可以工作,它只意味着额外的函数调用,而直接访问接口中的通道会更快,并且代码看起来更简单。