草庐IT

Golang 推断接口(interface)

coder 2023-06-27 原文

这里只是一个简单的语言设计相关问题。 在像 Swift 这样的语言中,为了使结构/类符合协议(protocol)/接口(interface),需要明确声明它

struct Dog: Animal {
    // implementation of Animal protocols here
}

但为什么在 Go 中,没有明确的方法来显示结构符合哪些接口(interface)?

这不是说不清楚还是另有原因?

最佳答案

这允许进化为甚至为遗留类型(从未明确声明尊重所述接口(interface))提供接口(interface)

如果这些类型已经提供了正确的方法(即与您的接口(interface)指定的 API 相同的 API),它们将满足该接口(interface)。

这允许在那些遗留类型(不知道您的接口(interface)存在)和您的程序之间保持低耦合,您的程序接受该接口(interface)的实例。

参见“What are some examples of Go interfaces?

Go's interfaces aren't a variant on Java or C# interfaces, they're much more.
They are a key to large-scale programming and adaptable, evolutionary design.

"It's the fact that I don't have to spend time up front designing some sort of type hierarchy and then rearranging it two or three times before I finish.
It's not even the fact that it's easy to do it right -
it's the fact that I just don't have to worry about it and can get on with the actual algorithm."

这些引述来自 2012 article , 也来自 this thread ,您可以在其中阅读 Rob Pike's comment :

"It's always bothered me in OO languages that we can make Circle, Square, etc. subclasses of Shape (say), but that's the one design decision we get to make.
What if we want to align those things along other axes (so to speak), like topological genus or plant genus, if you're a landscaper? You might lose your way or dig yourself into a hole with multiple inheritance."

关于Golang 推断接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41437046/

有关Golang 推断接口(interface)的更多相关文章

  1. postman接口测试工具-基础使用教程 - 2

    1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,

  2. ruby-on-rails - 如何在 RubyOnRails 中使用 'acts as nested set' 创建一个可排序的接口(interface) - 2

    我一直在为使用acts_as_list的模型实现一些不错的交互界面,这些界面可以对我的mRails应用程序中的列表进行排序。我有一个排序函数,在每次拖放之后使用sortable_elementscript.aculo.us函数调用并设置每条记录的位置。这是在拖放完成后处理排序的Controller操作示例:defsortparams[:documents].each_with_indexdo|id,index|Document.update_all(['position=?',index+1],['id=?',id])endend现在我正在尝试对嵌套集模型(acts_as_nested

  3. 你真正了解什么是接口测试么?接口实战一“篇”入魂 - 2

    最近在工作中,看到一些新手测试同学,对接口测试存在很多疑问,甚至包括一些从事软件测试3,5年的同学,在聊到接口时,也是一知半解;今天借着这个机会,对接口测试做个实战教学,顺便总结一下经验,分享给大家。计划拆分成4个模块跟大家做一个分享,(接口测试、接口基础知识、接口自动化、接口进阶)感兴趣的小伙伴记得关注,希望对你的日常工作和求职面试,带来一些帮助。注:文章较长有5000多字,希望小伙伴们认真看完,当然有些内容对小白同学不是太友好,如果你需要详细了解其中的一些概念或者名词,请在文章之后留言,后续我将针对大家的疑问,整理输出一些大家感兴趣的文章。随着开发模式的迭代更新,前后端分离已不是新的概念,

  4. ruby-on-rails - 在 Ruby on Rails 中为由外部 API 支持的模型使用 ActiveRecord 接口(interface) - 2

    我正在尝试在我的Rails应用程序中使用模型来从外部API检索信息。我想做的是以类似于ActiveRecord模型提供的方式(特别是关联,以及相同风格的可链接查询方法)访问我的数据模型(可能包含来自多个API调用的信息)。我最初的直觉是重新创建我想要的ActiveRecord部分并合并此API。不想“重新发明轮子”并确切地看到添加更多功能需要多少工作让我退后一步并重新评估如何处理这个问题。我找到了在没有表的情况下使用ActiveRecord的方法(请参阅:Railscast#193TablelessModel和博客文章here)并研究了ActiveRecord。因为ActiveMode

  5. ruby - Interface Builder 看不到 MacRuby 的 socket - 2

    我正在尝试使用XCode和InterfaceBuilder构建一个基本的helloworld应用程序。但是,在InterfaceBuilder中,我看不到我的socket可以连接起来。我转到对象检查器Pane的连接选项卡,它显示“NewReferencingOutlet”。我想知道我的代码是否有误。在这里classHelloWorldControllerattr_accessor:hello_label,:hello_button,:hellodefawakeFromNib@hello=trueenddefchangeLabel(sender)if@hello@hello_label.

  6. [译]在C#中使用IComparable和IComparer接口 - 2

    原文:UsetheIComparableandIComparerinterfacesinVisualCSharp本文介绍了在VisualC#中如何使用IComparer和IComparable接口。概要本文同时讨论了IComparable和IComparer接口,原因有两点。这两个接口经常一起使用。虽然接口类似且名称相似,但它们却有不同的用途。如果你有一个支持IComparer的类型数组(例如字符串或整数),你可以对它进行排序而不需要提供任何对IComparer的显式引用(译注:意思是把一个IComparer的实现类作为参数传递给排序方法)。在这种情况下,数组元素会被转换为IComparer的

  7. API淘宝数据接口 - 2

    如果你想在自己的应用中使用淘宝的数据,那么对接淘宝数据接口是必不可少的一步。本文将介绍如何对接API淘宝数据接口,以便你能够顺利获取和使用淘宝的数据。步骤一:​​获取AppKey和AppSecret​​首先,在淘宝开放平台申请API接口之前,需要先注册为淘宝开发者并创建应用。创建应用后,你将得到一个AppKey和AppSecret,这两个参数需要在调用API接口时用于身份验证。步骤二:确定需要调用的API接口在淘宝开放平台的开发文档中,你将找到所有可用的API接口。你需要根据你的需求找到需要调用的API接口,例如查询商品、店铺等信息。在找到需要的API接口后,你需要了解该接口的请求参数和返回结

  8. ruby - 如何使用 Devise 来保护 delayed_job_web 接口(interface)? - 2

    我正在使用delayed_job_webgem来监控延迟的作业。https://github.com/ejschmitt/delayed_job_web可以使用我的routes.rb中的这一行访问它:match"/delayed_job"=>DelayedJobWeb,:anchor=>false我站点的每个其他区域都需要使用Devisegem登录。我如何使它也需要登录?在自述文件中,他们建议将以下内容添加到config.rb:ifRails.env.production?DelayedJobWeb.useRack::Auth::Basicdo|username,password|us

  9. ruby - 在 Ruby 中编写 C 接口(interface)比在 Perl 中更容易吗? - 2

    根据officialrubyAboutpage用C扩展Ruby比用Perl更容易。我不是(perl)XS专家,但我发现使用Inline::C快速简单地编写一些东西非常简单,那么为什么在Ruby中更容易呢?WritingCextensionsinRubyiseasierthaninPerlorPython,withaveryelegantAPIforcallingRubyfromC.ThisincludescallsforembeddingRubyinsoftware,foruseasascriptinglanguage.ASWIGinterfaceisalsoavailable.那些做

  10. ruby - 为什么 Ruby 人说他们不需要接口(interface)? - 2

    Ruby是否与其他OOP语言(例如:PHP)不同,使接口(interface)变得无用?它有某种替代品吗?编辑:一些说明:在其他语言中(例如:PHP),您并不“需要”接口(interface)(它们在代码级别不是强制性的)。你用它们来订立契约(Contract),改进软件的架构。因此,'在ruby​​中你不需要接口(interface)/在其他语言中你需要接口(interface)因为XXX'的断言是错误的。不,混合不是接口(interface),它们是完全不同的东西(PHP5.4实现了混合)。你用过接口(interface)吗?是的,PHP是OOP。语言不断发展,欢迎来到现在。

随机推荐