首页 > 其他分享 >Go语言精进之路读书笔记第27条——尽量定义小接口

Go语言精进之路读书笔记第27条——尽量定义小接口

时间:2024-02-15 10:56:44浏览次数:29  
标签:27 定义 读书笔记 接口 接口类型 抽象 契约 Go

接口越大,抽象程度越低 —— Rob Pike,Go语言之父

27.1 Go推荐定义小接口

无论标准库还是社区项目,都遵循了“尽量定义小接口”的建议,方法数量在1~3个范围内的接口占了绝大多数。

27.2 小接口的优势

1.接口越小,抽象程度越高,被接纳度越高

抽象程度越高,对应的集合空间越大。无方法的空接口interface{},空接口的这个抽象对应的事物集合空间包含了Go语言世界的所有事物。

2.易于实现和测试

3.契约职责单一,易于复用组合

Go的设计原则推崇通过组合的方式构建程序,一般会先尝试通过嵌入其他已有接口类型的方式来构建新接口类型。

27.3 定义小接口可以遵循的一些点

1.抽象出接口

在定义小接口之前,我们需要首先深入理解问题域,聚焦抽象并发现接口。

初期不要在意接口的大小,因为对于问题域的理解是循序渐进的,期望在第一版代码中直接定义出小接口可能并不现实。

越偏向业务层,抽象难度越高。

2.将大接口拆分为小接口

分析哪些场合使用了接口的哪些方法,是否可以将这些场合使用的接口的方法提取出来放入一个新的小接口中。

3.接口的单一契约原则

选择那些新接口类型需要的契约职责,不要引入我们不需要的契约职责。

标签:27,定义,读书笔记,接口,接口类型,抽象,契约,Go
From: https://www.cnblogs.com/brynchen/p/18016024

相关文章

  • 快速部署最简单的 Git 服务 Gogs
    前面介绍了Gitlab的搭建,功能很强大,无论是cpu还是内存,要求机器的配置要高一些。如果没有比较高的机器配置,只使用最常用的Git代码托管功能,那么就使用Gogs来快速部署吧。Gogs是一款极易搭建的自助Git服务。旨在打造一个以最简便的方式搭建简单、稳定和可扩展的自助Git......
  • Go 100 mistakes - #21: Inefficient slice initialization
          ConvertingoneslicetypeintoanotherisafrequentoperationforGodevelopers.As wehaveseen,ifthelengthofthefuturesliceisalreadyknown,thereisnogoodreasontoallocateanemptyslicefirst.Ouroptionsaretoallocat......
  • Go - slice
     Theslicenowreferencesthenewbackingarray.Whatwillhappentotheprevious backingarray?Ifit’snolongerreferenced,it’seventuallyfreedbythegarbagecollector(GC)ifallocatedontheheap.    Tosummarize,theslicelengthi......
  • Go - floating points
    Notethatthere’saninfinitenumberof realvaluesbetweenmath.SmallestNonzeroFloat64(thefloat64minimum)and math.MaxFloat64(thefloat64maximum).Conversely,thefloat64typehasafinite numberofbits:64.Becausemakinginfinitevaluesfitinto......
  • Go - int overflow
     funcmain(){fmt.Printf("math.MaxInt32:%d\n",math.MaxInt32)fmt.Printf("math.MinInt32:%d\n",math.MinInt32)varcounterint32=math.MaxInt32counter++fmt.Pri......
  • Go 之烧脑的接口
    基本定义Go官方对于接口的定义是一句话:An interfacetypeisdefinedasasetofmethodsignatures.翻译过来就是,一个接口定义了一组方法的集合。这和Java和PHP的接口类似,定义一组方法而不定义方法的具体实现。但是与Java和PHP迥然不同的地方在于Go不需要显式的声......
  • Go 100 mistakes - #16: Not using linters
    Alinterisanautomatictooltoanalyzecodeandcatcherrors. Tounderstandwhylintersareimportant,let’stakeoneconcreteexample.Inmistake#1,“Unintendedvariableshadowing,”wediscussedpotentialerrorsrelatedto variableshadowing.Using......
  • Go 100 mistakes - #15: Missing code documentation
    Documentationisanimportantaspectofcoding.ItsimplifieshowclientscanconsumeanAPIbutcanalsohelpinmaintainingaproject.InGo,weshouldfollowsome rulestomakeourcodeidiomatic.First,everyexportedelementmustbedocumented.Wheth......
  • Go语言精进之路读书笔记第26条——了解接口类型变量的内部表示
    接口是Go这门静态语言中唯一“动静兼备”的语言特性接口的静态特性接口类型变量具有静态类型,比如:vareerror中变量e的静态类型为error支持在编译阶段的类型检查:当一个接口类型变量被赋值时,编译器会检查右值的类型是否实现了该接口方法集合中的所有方法接口的动态特性接......
  • Go - Project structure
    TheGolanguagemaintainerhasnostrongconventionaboutstructuringaprojectin Go.However,onelayouthasemergedovertheyears:project-layout(https://github.com/golang-standards/project-layout).Ifourprojectissmallenough(onlyafewfiles),......