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

msgpack代替json防止丢失类型

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

描述

 
开发了一个通信协议, 协议对数据类型比较敏感, 根据不同数据数据类型使用不同格式。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
sophia 说:
2023年6月20日 15:32

A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. 천안출장마사지

Avatar_small
briansclub 说:
2023年7月04日 16:08

A big thank you for your blog. Really looking forward to read more. Great.

Avatar_small
sophia 说:
2023年8月20日 18:25

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene. cci large rifle primers

Avatar_small
sophia 说:
2023年8月29日 01:46

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene. เว็บแทงบอลดีที่สุดUFABET

Avatar_small
sophia 说:
2023年8月31日 17:56

This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. livpure review

Avatar_small
sophia 说:
2023年9月02日 21:18

This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. 먹튀검증

Avatar_small
Dave 说:
2023年9月06日 18:03

Awesome article! I want people to know just how good this information is in your article. It’s interesting, compelling content. Your views are much like my own concerning this subject. bubble shooter games free

Avatar_small
Dave 说:
2023年9月11日 00:58

These rings are a symbol of lasting love. 鑽戒

Avatar_small
Dave 说:
2023年9月16日 18:25

You have beaten yourself this time, and I appreciate you and hopping for some more informative posts in future. Thank you for sharing great information to us. rugs nz

Avatar_small
Dave 说:
2023年9月21日 00:49

You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this... Practo clone

Avatar_small
Dave 说:
2023年9月28日 00:11

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. Magazine Classic

Avatar_small
seo akc 说:
2023年10月07日 03:36

I can give you the address Here you will learn how to do it correctly. Read and write something good. <a href="https://www.cbdflex.com/product/30ct-cbd-gummy-bears-300mg/">Buy CBD Gummy Bears</a>

Avatar_small
Dave 说:
2023年10月07日 11:13

Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info. ฝันเห็นเณร

Avatar_small
Dave 说:
2023年10月08日 13:44

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. Jogo do Tigre

Avatar_small
Dave 说:
2023年10月09日 14:24

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well. REGENERE DROPS

Avatar_small
Dave 说:
2023年10月11日 20:28

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. CORTEXI

Avatar_small
Dave 说:
2023年10月25日 20:25

I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. bitcoin cracker tool

Avatar_small
wallpaper singapore 说:
2023年11月02日 01:55

I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... Sajta de Pollo

Avatar_small
wallpaper singapore 说:
2023年11月03日 13:54

It is an excellent blog, I have ever seen. I found all the material on this blog utmost unique and well written. And, I have decided to visit it again and again. wallpaper singapore

Avatar_small
Dav 说:
2023年11月05日 20:58

I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. wallpaper singapore

Avatar_small
Dave 说:
2023年11月13日 18:32

The packaging is like a time capsule, preserving the essence of each pencil. pencil packing job

Avatar_small
Dave 说:
2023年11月16日 15:54

I think that thanks for the valuabe information and insights you have so provided here. kuliah online kelas karyawan murah

Avatar_small
Dave 说:
2023年11月16日 19:23

I think that thanks for the valuabe information and insights you have so provided here. temazepam 10mg kopen

Avatar_small
Dave 说:
2023年11月25日 13:58

The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. Crimsafe northside

Avatar_small
Dave 说:
2023年12月22日 17:21

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. roket288

Avatar_small
jack 说:
2023年12月23日 04:19

It is somewhat fantastic, and yet check out the advice at this treat. <a href="https://calculator-converter.com/mph-to-knots.htm">mph to knots</a>

Avatar_small
Dave 说:
2023年12月24日 13:39

Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. fausse carte d'identité

Avatar_small
jack 说:
2023年12月25日 03:53

Profit primarily prime quality items -- you can understand them all within: <a href="https://metric-calculator.com/convert-microinch-to-in.htm">microinch to in</a>

Avatar_small
Dave 说:
2024年1月07日 02:11

Great survey, I'm sure you're getting a great response. weed bestellen

Avatar_small
lahoreescortsfun 说:
2024年1月22日 18:00

Discover unparalleled companionship with our elegant and professional escort girls. Our carefully selected individuals, primarily women, offer more than just social accompaniment—they provide an enriching experience tailored to your preferences. Whether you seek engaging conversations, social companionship at events, or a more intimate encounter within the bounds of the adult entertainment industry, our escort girls bring a touch of sophistication and discretion to every interaction. Embracing diverse perspectives on personal agency and human relationships, our escort services navigate the intricacies of this industry with professionalism and respect. Explore a world where companionship transcends expectations, and every moment is crafted to meet your desires. Welcome to a realm of elegance, charm, and personalized connection with our exceptional escort girls.Call girls in pakistan

<a href="https://lahoremodels4.blogspot.com/">call girls in lahore</a>
<a href="https://lahoremodels4.blogspot.com/">lahore call girls</a>
<a href="https://lahoremodels4.blogspot.com/">escorts in lahore</a>
<a href="https://lahoremodels4.blogspot.com/">lahore escorts</a>

Avatar_small
plutotours 说:
2024年2月05日 13:57

Pluto Tours offers the best prices for Leh Ladakh tour packages. Click here to obtain unique prices on bespoke Ladakh vacation packages that include airfare, a guide, a clean pass accommodation, local transportation, and sightseeing.

Avatar_small
plutotours 说:
2024年2月05日 13:58

Pluto Tours offers a wide choice of Kashmir tour packages with intriguing incentives. Book bespoke Kashmir packages with great prices and incentives.

Avatar_small
Dave 说:
2024年2月09日 01:59

Great survey, I'm sure you're getting a great response. weed bestellen


登录 *


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