nats的安装docker方式
zap日志写入通道被覆盖

msgpack代替json防止丢失类型

妙音 posted @ 2021年6月30日 15:12 in golang , 667 阅读
 

描述

 
开发了一个通信协议, 协议对数据类型比较敏感, 根据不同数据数据类型使用不同格式。golang的数据类型能够满足需求。
 
然而,将协议转为http方式时,如果传入json,会发现数据类型不符合预期
 
例如,本来是应该int结果是float64; 本来应该是[]byte,结果是string
 

环境

 
http服务器: gin
 
开发语言: golang
 

解决方法探寻

 
什么传输协议会带上数据类型?
 
* json类型丢失
 
默认情况下gin的BindJSON,可将传入的参数绑定到具体的结构体,产生类型转换。然后,我的结果需要是动态的,不知道参数的具体内容。
 
* 自定义参数格式,将类型带上。例如 marchinery
 
Arg struct{
    Type string
    Value interface{}
}
 
缺点:太麻烦
 
* 常用协议中选择
 
xml         可以用,不理想
 
yaml        不能解决问题
 
protobuf    不能解决问题
 
messagepack 有详细的数据类型,完美
 
 

msgpack替代json

 
gin示例
 
func SendXX(c *gin.Context){
    rawData, _ := c.GetRawData()
    var param interface{}
    msgpack.Unmarshal(rawData, ¶m)

result := struct{
Name string
}{
Name: "xxx",
}
resultData, _ := msgpack.Marshal(result)
c.Data(200, "application/x-msgpack", resultData)
}
 

备注

 
* gin自带的msgpack版本是1.1.7不好用,不会自动进行类型转换。 最好改用  github.com/vmihailenco/msgpack/v5
 
* 替换gin中的部分接口就可以,不用全部改为msgpack
 
 
 
 
Avatar_small
psd letter 说:
2021年7月01日 18:54

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. psd letter

Avatar_small
tozo t6 review 说:
2021年7月04日 04:42

The total battery life of 30 hours. Craved right and left initials to make it easier to place the earbuds. The touch system is more appropriate. Technically, yes, you can answer or reject calls tozo t6 review

Avatar_small
seo 说:
2021年7月06日 19:35 It is extremely nice to see the greatest details presented in an easy and understanding manner. 123movies.com official site
Avatar_small
seo 说:
2021年7月06日 19:40

It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content. 123 movies

Avatar_small
seo 说:
2021年7月06日 19:43

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. 123movies

Avatar_small
Digital_Work 说:
2021年7月08日 07:33

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. ver anime

Avatar_small
seo 说:
2021年7月08日 13:31

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! sa gaming

Avatar_small
seo 说:
2021年7月10日 20:16

It is extremely nice to see the greatest details presented in an easy and understanding manner. superslot

Avatar_small
seo 说:
2021年7月10日 20:22

That is very helpful for increasing my knowledge in this field. pg

Avatar_small
seo 说:
2021年7月13日 15:23 A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. joker gaming
Avatar_small
seo 说:
2021年7月13日 18:10

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! waec expo

Avatar_small
seo 说:
2021年7月13日 23:48

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. joker gaming

Avatar_small
Biofit 说:
2021年7月13日 23:56

cool stuff you have got and you keep update all of us.

Avatar_small
инженерные сети 说:
2021年7月15日 18:53

I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. инженерные сети

Avatar_small
Digital_Zone 说:
2021年8月22日 20:39

This is very interesting content! I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are great. soaptoday

Avatar_small
rent a car beograd 说:
2021年8月26日 20:24

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work rent a car beograd

Avatar_small
how to become a regi 说:
2021年8月28日 20:26

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work how to become a registered iso of wells fargo

Avatar_small
check this out 说:
2021年9月02日 13:33

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. check this out

Avatar_small
AAA 说:
2021年9月03日 00:30

This blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. магазин интернет магазин

Avatar_small
merchant services sa 说:
2021年9月04日 20:09

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. merchant services sales representative

Avatar_small
dfg` 说:
2021年9月04日 23:56

This article is an appealing wealth of informative data that is interesting and well-written. I commend your hard work on this and thank you for this information. You’ve got what it takes to get attention. oreo

Avatar_small
Anwalt Kassel 说:
2021年9月07日 14:32

I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up. <a href="https://rechtsanwalt-roscher.de">Anwalt Kassel</a>

Avatar_small
Umzüge Bielefeld 说:
2021年9月07日 18:43

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. Umzüge Bielefeld

Avatar_small
Digital_Zone 说:
2021年9月10日 07:17 I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Birla Tisya
Avatar_small
dfg` 说:
2021年9月11日 18:42

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. There tend to be not many people who can certainly write not so simple posts that artistically. Continue the nice writing lions breath carts

Avatar_small
dfg` 说:
2021年9月12日 22:14

Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. https://barowatt.com

Avatar_small
martian rocks weed 说:
2021年9月13日 00:05

Darknetweedstore.com is the fastest and convenient way in which one can order and receive weed without any interceptions to USA, uk, Poland, Ireland , France martian rocks weed

Avatar_small
dfg` 说:
2021年9月13日 19:11

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. free coins to play jackpot party

Avatar_small
dfg` 说:
2021年9月15日 17:22

Cool stuff you have got and you keep update all of us. HD Streamz

Avatar_small
Digital_Zone 说:
2021年9月16日 20:29

Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback. Zonnepanelen Roosdaal

Avatar_small
Digital_Zone 说:
2021年9月16日 20:42

Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. คาสิโน

Avatar_small
dfg` 说:
2021年9月17日 15:49

There are specific dissertation web sites over the web any time you access not surprisingly launched in the web blog. Crypto Mining

Avatar_small
Hardik 说:
2021年9月17日 17:51
I’m impressed, I must say. Truly rarely must i encounter a weblog that’s both educative and entertaining, and without a doubt, you have hit the nail about the head. Your concept is outstanding; the thing is something too few people are speaking intelligently about. My business is happy that we stumbled across this within my search for some thing relating to this.
<a href="https://shoppingpossible.com/how-to-detect-a-conference-call-on-android/"title="How To Detect A Conference Call On Android">How To Detect A Conference Call On Android
</a>
 
[url=https://shoppingpossible.com/how-to-detect-a-conference-call-on-android/]How To Detect A Conference Call On Android[/url]
 
Avatar_small
dfg` 说:
2021年9月19日 18:35

Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. pubg mobile download hack

Avatar_small
Digital_Zone 说:
2021年9月19日 22:21

There are a lot of blogs over the Internet. But I can surely say that your blog is amazing in all. It has all the qualities that a perfect blog should have. submissiveporn

Avatar_small
dfg` 说:
2021年9月21日 22:30

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. blackpods

Avatar_small
dfg` 说:
2021年9月24日 15:08

I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. buy palm seeds

Avatar_small
dfg` 说:
2021年9月25日 00:35

Dengue fever is another mosquito-borne deadly disease mostly finds in tropical areas. A huge number of people from India, Southeast Asia, China, the Caribbean island, and African tested Dengue Symptoms every year.

Avatar_small
dfg` 说:
2021年9月27日 00:18

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. Sofa cleaning Waterlooville

Avatar_small
dfg` 说:
2021年9月27日 17:59

Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! poker online

Avatar_small
layarqq 说:
2021年9月27日 19:59

Betting is the point at which you wage cash for something in any event, when you don't have a clue what the result will be. layarqq

Avatar_small
dfg` 说:
2021年9月28日 01:07

hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. moving-local-distance-to-austin

Avatar_small
dfg` 说:
2021年9月28日 01:12

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work. Packing Tips: The Complete List of Dos And Don’ts For Long-Distance Moving

Avatar_small
dfg` 说:
2021年9月28日 01:19

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work. What Is the Average Cost of Moving an Apartment?

Avatar_small
dfg` 说:
2021年9月28日 18:14

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. City top rank business listings

Avatar_small
huram 说:
2021年9月28日 22:51

Mmm.. estimable to be here in your report or notify, whatever, I repute I should moreover process strong for my have website want I play some salubrious further updated busy in your location. <a href="https://www.thenewcanadians.com/?fbclid=IwAR3_6ZAviXa01yzNp94ajApHUvZANDZL3ENoZO1NTM26P0DbmDHzOqTs7eg">situs idn poker</a>

Avatar_small
dfg` 说:
2021年10月01日 20:35

If you set out to make me think today; mission accomplished! I really like your writing style and how you express your ideas. Thank you. [url=https://apkity.com/pikashow-apk-4/]pikashow apk[/url]

Avatar_small
dfg` 说:
2021年10月01日 20:35

Thank you for such a well written article. It’s full of insightful information and entertaining descriptions. Your point of view is the best among many. pikashow apk

Avatar_small
https://apkity.com/t 说:
2021年10月03日 01:07

Thanks for sharing such a beautiful information please keep it

Avatar_small
dfg` 说:
2021年10月05日 18:33

Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out. Blackpods Pro

Avatar_small
dfg` 说:
2021年10月07日 15:11

Wow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. open source real estate crowdfunding software

Avatar_small
sabra 说:
2021年10月07日 19:36

I came onto your blog while focusing just slightly submits. Nice strategy for next, I will be bookmarking at once seize your complete rises... travel agency

Avatar_small
dfg` 说:
2021年10月10日 22:08

I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really great : D. Good job, cheers endreinigung

Avatar_small
dfg` 说:
2021年10月12日 18:07

I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,.. 먹튀폴리스

Avatar_small
dfg` 说:
2021年10月14日 13:58

Excellent to be visiting your blog again, it has been months for me. Rightly, this article that I've been served for therefore long. I want this article to finish my assignment within the faculty, and it has the same topic together with your article. Thanks for the ton of valuable help, nice share. Latest rap news

Avatar_small
dfg` 说:
2021年10月14日 15:57

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! ufadna168

Avatar_small
dfg` 说:
2021年10月16日 22:12

Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks 핀벳88검증

Avatar_small
Dr.Billa 说:
2021年10月18日 04:54

Great work✅ This is the type of information that should be shared around the web. I also have some information related to on my website✅ emirates flights


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter