首页 > 其他分享 >Go 100 mistakes - #8: any says nothing

Go 100 mistakes - #8: any says nothing

时间:2024-02-13 17:22:42浏览次数:29  
标签:code says methods Go interface mistakes nothing type any

With Go 1.18, the predeclared type any became an alias for an empty interface; hence, all the interface{} occurrences can be replaced by any.

 

If future developers need to use the Store struct, they will probably have to dig into the documentation or read the code to understand how to use these methods. Hence, accepting or returning an any type doesn’t convey meaningful information. Also, because there is no safeguard at compile time, nothing prevents a caller from calling these methods with whatever data type.

By using any, we lose some of the benefits of Go as a statically typed language. Instead, we should avoid any types and make our signatures explicit as much as possible. Regarding our example, this could mean duplicating the Get and Set methods per type:

In this version, the methods are expressive, reducing the risk of incomprehension. Having more methods isn’t necessarily a problem because clients can also create their own abstraction using an interface. For example, if a client is interested only in the Contract methods, it could write something like this:

In summary, any can be helpful if there is a genuine need for accepting or returning any possible type (for instance, when it comes to marshaling or formatting). In general, we should avoid overgeneralizing the code we write at all costs. Perhaps a little bit of duplicated code might occasionally be better if it improves other aspects such as code expressiveness.

标签:code,says,methods,Go,interface,mistakes,nothing,type,any
From: https://www.cnblogs.com/zhangzhihui/p/18014666

相关文章

  • Go 100 mistakes - #7: Returning interfaces
       Allinall,inmostcases,weshouldn’treturninterfacesbutconcreteimplementa-tions.Otherwise,itcanmakeourdesignmorecomplexduetopackagedependencies andcanrestrictflexibilitybecausealltheclientswouldhavetorelyonthesam......
  • Go 100 mistakes - #6: Interface on the producer side
        Aninterfaceshouldliveontheconsumersideinmostcases.However,inparticu-larcontexts(forexample,whenweknow—notforesee—thatanabstractionwillbe helpfulforconsumers),wemaywanttohaveitontheproducerside.Ifwedo,w......
  • NebulaGraph is nothing without you | 社区 2023 年度人物合集
    在去年的年度人物回顾中,我们看到了形形色色的人们,他们当中有帮NebulaGraph捉bug的小能手,也有通过用回复来解答他人疑惑的启蒙者…在今年(2023年),我们这个整点不一样的,将镜头推进,看清他们的姓氏和脸庞,聚焦在每位NebulaGrpah技术社区作出贡献的小伙伴。每年的人物盘点,像是翻......
  • 刷题记录——MISTAKES 慢慢更新
    刷题记录——MISTAKES慢慢更新截止到:20231020(有时会忘记改日期)。信友队——CSP-S2023复赛模拟赛T2忘了取模和二分了,直接爆longlong和TLE然后\(0\text{pts}\).CF1065CMakeItEqual桶桶桶桶桶!!!\(2e5\)你不用桶难道还要二分的吗?洛谷CSP-S2023模拟赛模\(9982443......
  • [ABC141E] Who Says a Pun?
    WhoSaysaPunの传送门看到两个完全相同的子串,考虑dp。设\(f_{i,j}\)为从第\(i\)项和第\(j\)项开始的最长相同子串,则有\(s_i=s_j\)时,\(f_{i,j}=\max({f_{i-1,j-1}+1},f_{i,j})\)。注意,因为两个子串互不重叠,所以\(f_{i-1,j-1}<j-i\)时才可以转移状态。#include......
  • mockito5.4.0单元测试(11) --do when家族的方法们:doReturn()|doThrow()| doAnswer()|
    mockito官方文档地址:https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#do_family_methods_stubs//mock一个对象HashMapmockMap=mock(HashMap.class);  doCallRealMethod方法示例://当mock对象调用put和size方法时,都调用真实的方......
  • java8随手记(包含idea连接远程分支出现Nothing to update问题)
    Steam流一、映射1.map()和.flatMap()map将数据放入集合中,返回Steam流中。例如:map集合{1,2,3},返回Steam流[a,b,c,{1,2,3}]flatMap将将集合中的数据,返回Steam流中.例如:flatMap集合{1,2,3},返回Steam流中[a,b,c,1,2,3]注意:add与addAll有同样的效果。   ......
  • Kotlin中的Any、Unit、Nothing
    AnyKotlin中的Any即相当于Java中的Object类,是一切其他非空类型的父类型,Any?则表示一切可空类型的父类型。Any拥有三个方法:equals()hashCode()toString()事实上Any在编译成字节码后正是会转成java.lang.Object类。UnitKotlin中的Unit即相当于Java中的void关键字,用于表示......
  • cannot configure a CMake target with CLion (Nothing to run on)?
    ERROR:打开之前的stm32项目Clion无法识别CMakeLists.txt,编译按钮变成灰色,点击run按钮会产生cannotbuildxxx.elf 或者Nothingtorunon,在run配置target和executable......
  • Nothing to lose 最高难度存档
    存档位置为%localappdata%\TWD\Saved\SaveGames 到达了通关前选择结局的分支Nothingtolose存档(自行重命名为TWDGame.sav)......