为了账号安全,请及时绑定邮箱和手机立即绑定

关于接口“所有已知实现”的文档

关于接口“所有已知实现”的文档

Go
HUX布斯 2021-10-25 19:56:03
在学习 Go 几个月后,我才发现通过实现函数来os.File实现io.Reader接口Read(b []byte) (n int, err error)。这允许我使用缓冲阅读器通过执行以下操作来读取文件:f, err := os.Open("myfile.txt")bufReader := bufio.NewReader(f)除非我错过它,否则在接口的 Go 文档中似乎没有“所有已知的实现类”,就像在 Java 接口文档中找到的那样。有什么方法可以识别在 Go 中实现接口的类型吗?
查看完整描述

3 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

为了你vim的吸毒者在那里,VIM-GO使用支持先进的代码分析:GoImplements,:GoCallees,:GoChannelPeers,:GoReferrers等Oracle命令。


例如,如果我有一个如下所示的Calculator接口和实现:


type Arithmetic interface{

  add(float64, float64) float64 

}


type Calculator struct{}


func (c *calculator) add(o1, o2 float64) float64 {

  // ... stuff

}

然后:GoImplements用我的光标在 vim 中运行type Arithmetic interface将产生如下结果:


calculator.go|8 col 6| interface type Arithmetic

calculator.go|3 col 6| is implemented by pointer type *calculator

现在,如果我将光标移动到该type Calculator struct{}行并运行:GoImplements,我将得到如下结果:


calculator.go|3 col 6| pointer type *calculator

calculator.go|8 col 6| implements Arithmetic

注意:如果您收到“未知命令”错误,请:GoInstallBinaries在重试之前先尝试执行。


查看完整回答
反对 回复 2021-10-25
  • 3 回答
  • 0 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信