首页 > 其他分享 >Go 100 mistakes - #10: Not being aware of the possible problems with type embedding

Go 100 mistakes - #10: Not being aware of the possible problems with type embedding

时间:2024-02-13 21:44:21浏览次数:29  
标签:10 methods embedded being clients problems mutex external type

 

Because the mutex is embedded, we can directly access the Lock and Unlock methods from the i receiver.
We mentioned that such an example is a wrong usage of type embedding. What’s the reason for this? Since sync.Mutex is an embedded type, the Lock and Unlock methods will be promoted. Therefore, both methods become visible to external clients using InMem:

This promotion is probably not desired. A mutex is, in most cases, something that we want to encapsulate within a struct and make invisible to external clients. Therefore, we shouldn’t make it an embedded field in this case:

Because the mutex isn’t embedded and is unexported, it can’t be accessed from external clients.

 

 

标签:10,methods,embedded,being,clients,problems,mutex,external,type
From: https://www.cnblogs.com/zhangzhihui/p/18014853

相关文章

  • Go 100 mistakes - #9: Being confused about when to use generics
    Go1.18addsgenericstothelanguage.Inanutshell,thisallowswritingcodewithtypes thatcanbespecifiedlaterandinstantiatedwhenneeded. Onelastthingtonoteabouttypeparametersisthattheycan’tbeusedwith methodarguments,onlywith......
  • day10_管道符与grep与find
    关于代码文件和图片文件的存放务必搞清楚,绝对路径和相对路径关于linux命令执行的结果,以及后续处理关于文件打开的底层流程(文件句柄的概念)tail-f用法1.要求被检测的文件,存在2.可以tail-f检测关于html乱码以及UTF-8编码表的概念字节和字符挂钩的简单记忆1.查......
  • 「杂题乱刷」洛谷 P10155
    题目链接P10155[LSOT-2]基于二分查找与插入的快速排序解题思路算法一:容易发现,当\(a_n\)不为\(n\)时,我们无论如何都无法将\(n\)这个值插入到最后一位,否则我们可以依次将所有数字从大到小插入,这样也可以保证失去最少的贡献。视写法获得\(40\)分或\(60\)分。算法二......
  • Go 100 mistakes - #8: any says nothing
    WithGo1.18,thepredeclaredtypeanybecameanaliasforanempty interface;hence,alltheinterface{}occurrencescanbereplacedbyany. IffuturedevelopersneedtousetheStore struct,theywillprobablyhavetodigintothedocumentationorre......
  • 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......
  • [LeetCode] 2108. Find First Palindromic String in the Array
    Givenanarrayofstringswords,returnthefirstpalindromicstringinthearray.Ifthereisnosuchstring,returnanemptystring"".Astringispalindromicifitreadsthesameforwardandbackward.Example1:Input:words=["abc&quo......
  • 力扣递归 深度优先搜索 之 104. 二叉树的最大深度
    给定一个二叉树root,返回其最大深度。二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数。 示例1: 输入:root=[3,9,20,null,null,15,7]输出:3示例2:输入:root=[1,null,2]输出:2/** *Definitionforabinarytreenode. *publicclassTre......
  • MIT 6.1810 Lab: Multithreading
    lab网址:https://pdos.csail.mit.edu/6.828/2022/labs/cow.htmlxv6Book:https://pdos.csail.mit.edu/6.828/2022/xv6/book-riscv-rev3.pdfschedule代码分析scheduler在内核初始化的最后调用,内核初始化由main函数承担,运行在特权模式,main函数由start函数调用,start函数运行在机器模......
  • CF103E Buying Sets(最大权闭合子图模型)
    题意简述有\(n\)个元素和\(n\)个集合,保证任意\(k\)个集合的并\(\gek\)。每个集合有权值\(a_i\),你需要选出一些集合使得集合数等于集合并大小,并在此基础上最小化选出的集合权值和。\(n\le300,|a_i|\le10^6\)。分析将集合和元素看成物品,我们发现,若选择了一个集合,则......