首页 > 其他分享 >Go 100 mistakes - #7: Returning interfaces

Go 100 mistakes - #7: Returning interfaces

时间:2024-02-13 16:23:15浏览次数:18  
标签:abstraction Returning interfaces clients shouldn mistakes Go 100

 

 

 

All in all, in most cases, we shouldn’t return interfaces but concrete implementa-tions. Otherwise, it can make our design more complex due to package dependencies and can restrict flexibility because all the clients would have to rely on the same abstraction. Again, the conclusion is similar to the previous sections: if we know (not foresee) that an abstraction will be helpful for clients, we can consider returning an interface. Otherwise, we shouldn’t force abstractions; they should be discovered by clients. If a client needs to abstract an implementation for whatever reason, it can still do that on the client’s side.

标签:abstraction,Returning,interfaces,clients,shouldn,mistakes,Go,100
From: https://www.cnblogs.com/zhangzhihui/p/18014630

相关文章

  • Go语言精进之路读书笔记第25条——了解变长参数函数的妙用
    25.1什么是变长参数变长参数函数:调用时可以接受零个、一个或多个实际参数的函数。funcPrintln(a...interface{})(nint,errerror)只能有一个“...T”类型形式参数,且该形式参数应该为函数参数列表中的最后一个形式参数。“...T”类型形式参数在函数内呈现为[]T类型的变......
  • 三十五、Django实践的笔记
    Django时间时区datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S'),得到的是标准时区的时间字符串https://blog.csdn.net/qiaominghe/article/details/86593744https://blog.csdn.net/qq_42778001/article/details/111088130https://zhuanlan.zhihu.com/p/24246164fro......
  • Go 100 mistakes - #6: Interface on the producer side
        Aninterfaceshouldliveontheconsumersideinmostcases.However,inparticu-larcontexts(forexample,whenweknow—notforesee—thatanabstractionwillbe helpfulforconsumers),wemaywanttohaveitontheproducerside.Ifwedo,w......
  • Go - When to use interfaces?
    CommonbehaviorDecouplingRestrictingbehavior Commonbehavior: Decoupling:Restrictingbehavior:Thelastusecasewewilldiscusscanbeprettycounterintuitiveatfirstsight.It’sabout restrictingatypetoaspecificbehavior.Let’simagine......
  • Go语言精进之路读书笔记第24条——方法集合决定接口实现
    24.1方法集合方法决定接口实现:如果某个自定义类型T的方法集合是某个接口类型的方法集合的超集,那么就说类型T实现了该接口,并且类型T的变量可以赋值给该接口类型的变量Go语言规范,对于非接口类型的自定义类型T:类型T,方法集合由所有receiver为T类型的方法组成类型*T,方法集合由所......
  • Go语言精进之路读书笔记第23条——理解方法的本质以选择正确的receiver类型
    和函数相比,Go语言中的方法在声明形式上仅仅多了一个参数,Go称之为receiver参数。receiver参数是方法与类型之间的纽带。Go方法特点:方法名的首字母是否大写决定了该方法是不是导出方法。方法定义要与类型定义放在同一个包内。由此可以推出,不能为原生类型(如int/float64/map等)添加......
  • C1. Good Subarrays (Easy Version)
    找子数组的个数双指针#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintN=2e5+10;inta[N];voidsolve(){ intn; cin>>n; for(inti=1;i<=n;i++)cin>>a[i]; intl=1,r=1; intans=0; while(l<=r){ if(l>n||r>......
  • Go语言精进之路读书笔记第22条——使用defer让函数更简介、更健壮
    22.1defer的运行机制在Go中,只有在函数和方法内部才能使用defer。defer关键字后面只能接函数或方法,这些函数被成为deferred函数。defer将它们注册到其所在goroutine用于存放deferred函数的栈数据结构中。在执行defer的函数退出前,按后进先出(LIFO)的顺序调度执行。22.2defer的......
  • Google Earth Pro谷歌地球专业版
    GoogleEarthPro谷歌地球专业版,标准版,在国内可以用的,常见的黑屏问题可以解决的需要解决黑屏问题的可以找我(V×:F2233F) ......
  • Go语言精进之路读书笔记第21条——让自己习惯于函数是"一等公民"
    21.1什么是"一等公民"(1)正常创建//$GOROOT/src/fmt/print.gofuncnewPrinter()*pp{p:=ppFree.Get().(*pp)p.panicking=falsep.erroring=falsep.wrapErrs=falsep.fmt.init(&p.buf)returnp}(2)在函数内创建,定义匿名函数赋值给......