golang的继承不是继承
golang如何实现静态变量的效果

golang网络字节与基本类型转换

妙音 posted @ 2021年4月15日 21:35 in golang , 1134 阅读
 

说明

 
网络通信需要将go基本数据类型转为字节. go如何做?
 

基本类型

c类型 go类型 字节长度
signed char int8     1
unsigned char  uint8    1
_Bool            bool     1
short            int16    2
unsigned short uint16   2
int              int32    4
unsigned int uint32   4
float            float32  4
long long int64    8
unsigned l long uint64   8
double           float64 8
 

有符号与无符号转换

 
* int8/uint->byte或 byte->int8/uint8
1个字节强制类型转换会超范围
 
// int8 ->byte
var a int8 = -1
byte(a)          // 正常 255

//byte->int8
int8(byte(255))  //异常 constant 255 overflows int8

// byte->int8
var a byte = 255
int8(a)         //正常 -1
 

通用方法Write/Read

 
* int8/uint8/int16/uint16/int32/uint32/int64/uint64/float32/float64->[]byte
 
var a int16 =1
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, &a)
fmt.Println(buf.Bytes())
 
* []byte ->int8/uint8/int16/uint16/int32/uint32/int64/uint64/float32/float64
 
b :=[]byte{64, 9, 30, 184, 81, 235, 133, 31}
var a float64
binary.Read(bytes.NewBuffer(b), binary.BigEndian, &a)
fmt.Println(a)
 

binary.BigEndian方法

import (
	"bytes"
	"encoding/binary"
	"reflect"
)

//=================================
//		数字-->字节
//=================================
func Number2Bytes(value interface{}) []byte{
	result := make([]byte, 0)

	switch v := value.(type){
	case int8:
		return append(result, uint8(v))
	case int16:
		binary.BigEndian.PutUint16(result, uint16(v))
		return result
	case int32:
		binary.BigEndian.PutUint32(result, uint32(v))
		return result
	case int64:
		binary.BigEndian.PutUint64(result, uint64(v))
		return result
	case uint8:
		return append(result, uint8(v))
	case uint16:
		binary.BigEndian.PutUint16(result, v)
		return result
	case uint32:
		binary.BigEndian.PutUint32(result, v)
		return result
	case uint64:
		binary.BigEndian.PutUint64(result, v)
		return result
	case float32:
		buf := new(bytes.Buffer)
		binary.Write(buf, binary.BigEndian, &v)
		return buf.Bytes()
	case float64:
		buf := new(bytes.Buffer)
		binary.Write(buf, binary.BigEndian, &v)
		return buf.Bytes()
	}
	return nil
}

//=================================
//		字节-->数字
//=================================
func Bytes2Number(data []byte, kind reflect.Kind) interface{}{
	switch kind{
	case reflect.Int8:
		return int8(data[0])
	case reflect.Int16:
		return int16(binary.BigEndian.Uint16(data))
	case reflect.Int32:
		return int32(binary.BigEndian.Uint32(data))
	case reflect.Int64:
		return int64(binary.BigEndian.Uint64(data))
	case reflect.Uint8:
		return data[0]
	case reflect.Uint16:
		return binary.BigEndian.Uint16(data)
	case reflect.Uint32:
		return binary.BigEndian.Uint32(data)
	case reflect.Uint64:
		return binary.BigEndian.Uint64(data)
	case reflect.Float32:
		var v float32
		buf := bytes.NewBuffer(data)
		binary.Read(buf, binary.BigEndian, &v)
		return v
	case reflect.Float64:
		var v float64
		buf := bytes.NewBuffer(data)
		binary.Read(buf, binary.BigEndian, &v)
		return v
	}
	return nil
}
 
 
 
Avatar_small
Digital_Zone 说:
2021年5月22日 04:18 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 Godrej Hill Retreat
Avatar_small
Digital_Zone 说:
2021年5月22日 04:30

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! subcision acne scars singapore

Avatar_small
Digital_Zone 说:
2021年5月22日 04:37

I am unable to read articles online very often, but I’m glad I did today. This is very well written and your points are well-expressed. Please, don’t ever stop writing. coffee tables

Avatar_small
Digital_Zone 说:
2021年5月22日 04:44

Get Bet Quality cannabis products at very good prices only at Good Buds Only we give you the best of INDICA, SATIVA and HYBRID strains which are very medicated and very good for your health Best Cannabis Online

Avatar_small
Digital_Zone 说:
2021年5月22日 04:53

Want start business in Indonesia? BNK Consulting Resource Service, Tax & Investment Advisory to Doing Business - also Visas to Work Staying for Foreigners Resource Service

Avatar_small
Digital_Zone 说:
2021年5月22日 05:00

Excellent website! I adore how it is easy on my eyes it is. I am questioning how I might be notified whenever a new post has been made. Looking for more new updates. Have a great day! promo terbaru diskon togel

Avatar_small
Digital_Zone 说:
2021年5月24日 01:19 I am incapable of reading articles online very often, but I’m happy I did today. It is very well written, and your points are well-expressed. I request you warmly, please, don’t ever stop writing. Zonnepanelen kopen
Avatar_small
Digital_Zone 说:
2021年5月24日 01:39

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. Keukens Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 01:43

It is a good site post without fail. Not too many people would actually, the way you just did. I am impressed that there is so much information about this subject that has been uncovered and you’ve defeated yourself this time, with so much quality. Good Works! Zonnepanelen

Avatar_small
Digital_Zone 说:
2021年5月24日 01:47

Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. Keukenontwerper Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 04:15

It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait reading. I am impressed with your work and skill. Thanks. BTW vrijstelling zonnepanelen

Avatar_small
Digital_Zone 说:
2021年5月24日 04:20

You have a real talent for writing unique content. I like how you think and the way you express your views in this article. I am impressed by your writing style a lot. Thanks for making my experience more beautiful. Ramen Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 04:25

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. Landelijke keukens Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 04:29

Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. Salderingsregeling zonnepanelen

Avatar_small
Digital_Zone 说:
2021年5月24日 04:35

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. Moderne keukens Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 04:41

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. Ramen en deuren Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 04:46

Only aspire to mention ones content can be as incredible. This clarity with your post is superb and that i may think you’re a guru for this issue. High-quality along with your concur permit me to to seize your current give to keep modified by using approaching blog post. Thanks a lot hundreds of along with you should go on the pleasurable get the job done. Hedendaagse keukens Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 04:50

I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... Beste zonnepanelen

Avatar_small
Digital_Zone 说:
2021年5月24日 04:58

It’s appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or advice. Perhaps you could write next articles referring to this article. I desire to read even more things about it! Keukens op maat Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 05:02

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! Ramen deuren Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 05:06

Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help. Maatkeukens Leuven

Avatar_small
Digital_Zone 说:
2021年5月24日 05:10

I am unable to read articles online very often, but I’m glad I did today. This is very well written and your points are well-expressed. Please, don’t ever stop writing. Beste zonnepanelen merken

Avatar_small
Digital_Zone 说:
2021年5月24日 05:14

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. Airco

Avatar_small
Digital_Zone 说:
2021年5月24日 05:18

I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help. BTW zonnepanelen terugvragen

Avatar_small
Digital_Zone 说:
2021年5月24日 05:23

Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. Airconditioning

Avatar_small
Digital_Zone 说:
2021年5月24日 05:27

Thank you so much as you have been willing to share information with us. We will forever admire all you have done here because you have made my work as easy as ABC. Zonnepanelen

Avatar_small
Digital_Zone 说:
2021年5月24日 05:31

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. Airco installatie

Avatar_small
Digital_Zone 说:
2021年5月24日 05:35

Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. Zonnepanelen kopen

Avatar_small
Digital_Zone 说:
2021年5月24日 05:39

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

Avatar_small
Digital_Zone 说:
2021年5月24日 05:43

Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again. Webdesign

Avatar_small
Digital_Zone 说:
2021年5月24日 05:48

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. Vermageren

Avatar_small
Digital_Zone 说:
2021年5月26日 04:06

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. dark web links

Avatar_small
Digital_Zone 说:
2021年5月31日 04:30

Your article has piqued a lot of positive interest. I can see why since you have done such a good job of making it interesting. Dr Gőz Péter ügyvéd

Avatar_small
Digital_Zone 说:
2021年6月02日 01:40

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. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future... acne scars laser singapore

Avatar_small
Digital_Zone 说:
2021年6月02日 03:26

Today, I was just browsing along and came upon your blog. Just wanted to say good blog and this article helped me a lot, due to which I have found exactly I was looking. 123 movies

Avatar_small
Digital_Zone 说:
2021年6月02日 03:36

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. 123movies proxy

Avatar_small
Digital_Zone 说:
2021年6月02日 03:44

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 movies123

Avatar_small
Digital_Zone 说:
2021年6月02日 03:51

Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help. 123movies

Avatar_small
Digital_Zone 说:
2021年6月02日 03:59

Well-Written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can't pause to read more posts. Thanks for the precious help. 123movies that works

Avatar_small
Digital_Zone 说:
2021年6月02日 04:06

Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon! 123movie

Avatar_small
Digital_Zone 说:
2021年6月02日 04:25

This article is an appealing wealth of useful informative that is interesting and well-written. I commend your hard work on this and thank you for this information. I know it very well that if anyone visits your blog, then he/she will surely revisit it again. 123 movies

Avatar_small
Digital_Zone 说:
2021年6月02日 04:32

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. soap2day safe

Avatar_small
Digital_Zone 说:
2021年6月14日 23:14 An interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers. เว็บแทงบอลออนไลน์
Avatar_small
ceme online 说:
2021年6月29日 19:40

I really understand how it feels to have a blog bandar ceme online but a lot of spam by other people. Please be patient because this exam

Avatar_small
seo 说:
2021年11月30日 22:24

Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. toilet near me

Avatar_small
SEO 说:
2022年2月15日 00:35

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

Avatar_small
SEO 说:
2022年2月15日 00:36

I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! สล็อต

Avatar_small
SEO 说:
2022年2月15日 00:36

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

Avatar_small
SEO 说:
2022年2月15日 00:37

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. joker123

Avatar_small
SEO 说:
2022年2月15日 00:38

Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. บ้าดูบอล

Avatar_small
SEO 说:
2022年2月15日 00:38

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. wm casino

Avatar_small
SEO 说:
2022年2月15日 00:39

This is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here! สล็อต

Avatar_small
dark web/deep web/d 说:
2022年8月06日 23:02

While these websites do share a few common features, their main attraction is definitely the massive amount of dark web contents they offer. It would be hard to consider them legal, given the fact that they bypass technological protection methods such as encryption. dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月06日 23:37

This is an industry on which there is no real regulation, so you have to be especially careful. As a result, before you start linking, make sure you find out if the person you are linking with is allowed to do so. deep web

Avatar_small
dark web/deep web/d 说:
2022年8月06日 23:55

The Dark Web is filled with dangerous predators and hackers who prey on unsuspecting individuals who trust that the Web sites they visit are safe.  dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月07日 00:14

Hackers use these links to send you phishing scams and offer false links to make you think that you are downloading a valuable application.  dark web sites

Avatar_small
dark web/deep web/d 说:
2022年8月07日 00:32

For instance, the phrases "free money", "enter for free" or "give me" should not be included when searching for suspicious links. Instead, try searching for phrases like "how to make big money" or "get rich quick."  dark web

Avatar_small
dark web/deep web/d 说:
2022年8月07日 01:01

ClickBank markets thousands of products, and they display the highest quality of each product on their marketplace page.  work from home jobs

Avatar_small
dark web/deep web/d 说:
2022年8月07日 01:23

Now that you have products to promote, it's time to create a sales page. A sales page is just a short text or a short blog post about the product or service that you are promoting. affiliate marketing success


登录 *


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