golang如何实现静态变量的效果
妙音
posted @ 2021年4月24日 20:46
in golang
, 2423 阅读
说明
go的结构和方法接收者, 可以模拟类的概念, 然而go不支持静态变量
不能总是每次先创建一个结构体, 然后获取变量吧
该如何办?
如何创建"类"
先展示一下,平时我是如何创建"类"的
1.私有的结构体作为真实对象
2. 对外暴露方法,这个方法类似class可以作为参数传递,使用这个class就可以创建对象
3. BaseVar定义对外暴露的接口
4. 定义方法的interface,方便作为参数传递
type BaseVar interface { //对外暴露的接口 } //方便传递 type VarDecriptor func(params ...interface{}) BaseVar //等同于类 func VarBinary(params ...interface{}) BaseVar { s := &varBinary{ FormatCode: 0o10, TextCode: "B", PreferredTypes: []reflect.Kind{reflect.Uint8, reflect.Slice}, } return s } //实质上的对象 type varBinary struct{ BaseVar //外部接口 FormatCode int TextCode string PreferredTypes []reflect.Kind //bytes bytearray value []byte }
静态变量有何意义
关联常量与类
解决方法
定义全局变量,以"类"名作为key,存放静态变量。runtime能动态获取"类"名,通过"类"名取值。
var gFormatCode = map[string]int{ "VarBinary": 1, } func FormatCode(f VarDecriptor) int { //取方法名 fname := runtime.FuncForPC(reflect.ValueOf(fc).Pointer()).Name() names := strings.Split(fname, ".") funcName := names[1] //从全局变量中取值 return gFormatCode[funcName] } //使用时通过FormatCode,传入上面的VarBinary,就可以获取常量
2023年6月16日 01:08
I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. keputusan GD Lotto
2023年6月17日 12:27
This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this.. find out more
2023年7月01日 15:49
This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck. emerges
2023年7月04日 12:02
I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you! avante gym yoga
2023年7月07日 15:13
People have by using a brilliant vacancy. When i sanity undoubtedly quarry the item also in person advocate to help the buddys. My business is self-possessed many people resolve possibly be benefited with this world. Ahsan
2023年7月07日 21:27
I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. springbord
2023年7月08日 15:57
When i understand this post. I'm sure People fit many attempt to build this post. When i get pleasure from your job. <a href="https://mpa-seo.blogspot.com/2023/06/ahsan.html">Ahsan</a>
2023年7月08日 15:59
When i understand this post. I'm sure People fit many attempt to build this post. When i get pleasure from your job. Ahsan
2023年7月15日 18:14
I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article. exterior cleaning company
2023年7月21日 22:25
I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! ebooks with resell rights
2023年7月21日 22:46
We are tied directly into the sate’s renewal database which allows us to process your request almost instantly. orchard road hair removal
2023年7月30日 21:06
Automobile Dealerships in Rockwall, Texas, stay in vogue, improvement, and pure deluxe. You can feel the pleasure, like a buzz that will not quit when buying a new auto when you tip onto their display room flooring. The shining new automobile car dealerships with vehicles lightened up to a glimmer that reveals your image with crystal high quality call out to you from every instruction. It's challenging to endure their siren song as they bid you take a far better appearance, examining each inch of their structure outside and appreciating all its functions.Rockwall New Car Dealerships
2023年8月05日 20:55
Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more. Disney Plus Help
2023年8月16日 12:58
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page! nimbus
2023年8月17日 01:04
I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more. web design services in vancouver
2023年8月25日 00:28
I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more. water dispenser
2023年8月31日 12:09
I found Hubwit as a transparent s ite, a social hub which is a conglomerate of Buyers and Sellers who are ready to offer online digital consultancy at decent cost. rapidfinil reddit
2023年9月01日 00:42
It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested one.I concur with your conclusions and will eagerly look forward to your future updates. Rockwall Solar Panels
2023年9月03日 21:01
With so many books and articles coming up to give gateway to make-money-online field and confusing reader even more on the actual way of earning money, link vao vn88
2023年9月11日 17:45
Appreciate it intended for publishing a really beneficial document, When i happened onto your blog site in addition to understand some write-up. I'm keen on look connected with publishing... ahsanseoexpert
2023年9月19日 05:05
Thanks for the solution , i was looking forward to it !
2023年9月20日 18:42
Acknowledges intended for penmanship a really valuable column, When i happened adjacent to your site other than estimate a couple aid. I'd prefer ones develop connected with manuscript... Technology
2023年10月21日 03:13
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys. <a href="https://mariefrance.ae/en/products/lingerie">transparent training bra</a>
2023年10月21日 03:14
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys.
[url=https://mariefrance.ae/en/products/lingerie]transparent training bra[/url]
2023年10月21日 03:14
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys.
2023年10月21日 03:15
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys. transparent training bra
2023年10月21日 03:15
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys. transparent training bra
2023年10月21日 03:16
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys. <a href="https://mariefrance.ae/en/products/lingerie">transparent training bra</a>
2023年10月21日 03:16
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys. [url=https://mariefrance.ae/en/products/lingerie]transparent training bra[/url]
2023年10月21日 03:17
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys.
2023年10月21日 03:18
I should insist seldom of which it is amazing! The blog is usually info likewise generally fabricate wonderful entitys. transparent training bra
2023年10月25日 00:24
Mmm.. estimable for being through ones document or maybe advise, whatsoever, When i repute I should also practice sturdy intended for the include web page wish When i engage in many salubrious additionally kept up to date chaotic with your position. laser cutting machine
2023年10月28日 14:03
Thankyou for this post, I am a big big fan of this web site would like to proceed updated. g11bet
2023年10月28日 19:22
I would like to thnkx for the efforts you have put in writing this website. I’m hoping the same high-grade website post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own web site now. Actually the blogging is spreading its wings fast. Your write up is a great example of it. m11bet
2023年10月29日 18:41
Thanks for making the honest attempt to speak about this. I believe very robust approximately it and want to read more. If it’s OK, as you gain more in depth wisdom, would you thoughts adding extra articles similar to this one with additional information? It might be extremely useful and useful for me and my friends. https://nextnewstime.com/
2023年10月30日 18:26
I am not sure where you are getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for great info I was looking for this info for my mission. The Journey of A-1 Auto Transport: Joe Webster’s Vision in Motion
2023年11月05日 19:16
Generally I do not read article on blogs, however I wish to say that this write-up very forced me to check out and do so! Your writing taste has been amazed me. Thanks, very great post. Power And Acceleration Of GT Cars
2023年11月06日 12:09
<a href=" https://www.whitemushroomholidays.com">slot88</a> adalah salah satu jenis permainan judi online yang sedang booming
2023年11月10日 06:12
Great post, you have pointed out some fantastic details , I too conceive this s a very fantastic website. เว็บพนันบอล ไม่ผ่านเอเย่นต์
2023年11月12日 00:02
Kantorbola adalah situs slot gacor terbaik di indonesia , kunjungi situs RTP kantor bola untuk mendapatkan informasi akurat slot dengan rtp diatas 95% . Kunjungi juga link alternatif kami di kantorbola77 dan kantorbola99. kantorbola77
2023年11月18日 19:12
Hmm… When i understand information sites using a related difficulty, even so when i never ever stopped at your site. When i added in the item to help populars likewise i’ll possibly be ones dedicated primer.
<a href="http://newsrevolution.kesug.com/">News Revolution</a>
2023年11月25日 19:19
I exploit solely premium quality products -- you will observe these individuals on: <a href="http://dailyintelpost.great-site.net/2023/10/16/diet-plan-for-weight-gain-in-7-days/">Diet Plan for Weight Gain in 7 Days</a>
2023年11月25日 19:20
I exploit solely premium quality products -- you will observe these individuals on: Diet Plan for Weight Gain in 7 Days
2023年11月26日 20:33
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page! where to buy pappy van winkle
2023年11月27日 04:59
seriously that saintly even so, I adore ones enter in furthermore pleasant photographs may very well be aspect personss adverse appreciate currently being defrent intellect entire poeple, <a href="http://worldnews.wuaze.com/2023/11/22/best-basketball-shoes-for-ankle-support/">Best Basketball Shoes for Ankle Support</a>
2023年11月29日 18:31
I'm truly enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work! Best Videos
2023年12月05日 17:27
Choose your website plan You will be charged for the plan after the admin approves your vendor account Silver $ 299 99 5 Pages Premium theme Premium plugins CMS Wordpress Website speed Website backup system Add to cart Gold $ 499 99 8 Pages Premium theme Premium plugins High level security 1 Year free domain Web designing
2023年12月17日 17:12
Visit the 8171 website to determine if you qualify for government cash assistance. The Pakistani government initiated the 8171 Ehsaas Programme, aimed at assisting millions of individuals facing financial difficulties. This programme offers monetary aid to those who are unable to meet their families' essential needs. By creating an online portal, the government has streamlined <a href="https://ehsaasprogram8171.org/">Ehsaas Program 8171</a>
2024年1月16日 19:13
Mmm.. estimable for being through ones document or maybe advise, whatsoever, When i repute I should also practice sturdy intended for the include web page wish When i engage in many salubrious additionally kept up to date chaotic with your position. Winter Shoe Trends 2023 -2024
2024年1月16日 19:38
seriously that saintly even so, I adore ones enter in furthermore pleasant photographs may very well be aspect personss adverse appreciate currently being defrent intellect entire poeple,<a href="http://worldnews.wuaze.com/2023/11/22/best-basketball-shoes-for-ankle-support/">Best Basketball Shoes for Ankle Support</a>
2024年1月31日 03:19
I am able to provides you with the target In this article understand the best way to practice it the right way. Understand in addition to generate anything beneficial. <a href="http://worldnews.wuaze.com/">ibrahim bhlm</a>
2024年3月04日 23:06
My business is generally searching online intended for storys that could provide everyone. There may be definitely some sort of many to recognise with this. I find myself people manufactured several salubrious details with Capabilities also. Detain chaotic, brilliant vocation! Zyn Rewards List
2024年4月21日 14:21
2024年10月17日 19:47
Acknowledges intended for report a really effective formula, When i happened adjacent to your site other than decipher a finite say. I'd prefer ones strategy of inscription...
<a href="https://thescratchnews.com/unblocked-games-911/">Unblocked Games 911</a>
2024年10月21日 01:33
And so ton to take place in excess of ones wonderful blog site. Your site procures everyone an outstanding exchange connected with fulfilling.. Salubrious ton near the world. <a href="http://todayonlinenews.kesug.com/?i=1">todayonlinenews</a>
2024年10月21日 01:34
And so ton to take place in excess of ones wonderful blog site. Your site procures everyone an outstanding exchange connected with fulfilling.. Salubrious ton near the world. [url=http://todayonlinenews.kesug.com/?i=1]todayonlinenews[/url]
2024年10月24日 20:28
Allowed towards gathering connected with gaming in this article understand every little thing in relation to everyone.
2024年10月25日 22:19
Listed here you can study it is vital, these individuals affords the web page link within the very helpful internet site:
2024年10月26日 20:41
It is very helpful, on the other hand it might be critical so as to look at using web page:
2024年10月27日 21:57
Top notch subject matter, identical texts usually are When i have no idea of as long as they usually are competitive with your job available.
2024年11月12日 22:43
And so ton to take place in excess of ones wonderful blog site. Your site procures everyone an outstanding exchange connected with fulfilling.. Salubrious ton near the world. <a href="http://educationnewstoday.kesug.com/">educationnewstoday</a>
2024年11月21日 22:55
<a href="http://expressnews.kesug.com/">expressnews</a> Brilliant dispatch! My business is really having apt to in excess of that facts, is really neighborly the close friend. Moreover excellent blog site in this article involving the majority of the high-priced facts people attain. Pre-book in the effective practice your are performing in this article.
2024年11月21日 22:58
Incredibly useful facts, value proposing. Even so, I might suggest that:
2024年11月21日 23:02
<a href="http://expressnews.kesug.com/">expressnews</a> People have by using a brilliant vacancy. When i sanity undoubtedly quarry the item also in person advocate to help the buddys. My business is self-possessed many people resolve possibly be benefited with this world.
2024年11月26日 17:45
A totally free assert scarcely intended to most of the exceptional! Your blog is content and articles full element quite often fabricate marvelous entitys. repair my car