看了Golang中不定长的数据类型,总结了些有的没的,进来看看,万一有收获呢 | 青训营笔记

264 阅读5分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的的第1篇笔记

介绍

😆在这里,我对Golang基础中的一些不定长的数据类型做了个小总结

如果你全知道

image.png

😊如果有小伙伴能想到更多知识,欢迎大家在评论区留言,那么我们就开始吧

👩‍💻👨‍💻哟西,一个棕~

😎😎😎我是小小分割线

1234:我是整形

在Golang中,我们的类型非常丰富,各式各样

那么我们 曾经其他语言最用的 不带任何长度标记的 不定长的 各种类型 又是多少长度呢

1. int

在曾经,我们在很多语言都喜欢使用int这个类型

那么在Golang中int是一个什么样的呢

在官方文档中:

int is a signed integer type that is at least 32 bits in size. It is a distinct type, however, and not an alias for, say, int32.

int一个是大小至少为32位有符号整数类型,int和int32这两者并不是一个别名关系int和int32是截然不同的类型


2. uint

uint是一个无符号的整数的数据类型。

无符号意味着,他不能够表示负数,也就是说它可把符号位拿出来,那么就可以表示更多的正数了,如果你想只是表达正整数,用它会更好

那么Golang中int是什么样的呢

官方文档中:

uint is a variable sized type, on your 64 bit computer uint is 64 bits wide.

uint 是一种可变大小的类型,在64位计算机上,uint 是64位宽的

uint 类型长度取决于 CPU,如果是32位CPU就是4个字节,如果是64位就是8个字节

😎😎😎又是我,小小分割线

"hello,world":我是字符串

string

废话不多说,我们来看看string的源码介绍


// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.

type string string

重新排列如下:

string is the set of all strings of 8-bit bytes

string是所有8位字节串的集合

conventionally but not necessarily representing UTF-8-encoded text.

但不一定表示utf-8编码的文本。

A string may be empty,but not nil.

字符串可以是empty空的,但是不会是nil。

Values of string type are immutable.

字符串类型的值是不可变的。

Golang的优势:对于英文文本,Go使用8位来表示每一个字节,而Java或Python则需要16位或更多。

Go语言中字符串的字节使用UTF-8编码表示Unicode文本,因此Go语言字符串是变宽字符序列,每一个字符都用一个或者多个字符表示

其他语言的字符串中的单个字符可以被字节索引

但是在Golang中只有在字符串只包含7位的ASCII字符时才可以被字节索引

也就是在它们都是用一个单一的UTF-8字节表示时

那么这是不是表示Go语言在字符串处理能力上就比其他语言弱呢?

image.png

答案是:不是的

因为在Golang中是支持一个字符一个字符的迭代

况且在Golang的标准库中存在大量的字符串操作函数,对我们十分的友好

而且不仅有以上的解决方法,在Go语言中,一个单一的码点在内存中以 rune 的形式表示,我们还可以将Go语言的字符串转化为Unicode码点切片(类型为 []rune),切片是支持直接索引的。

不仅在以上的操作中的优势,Golang它采用了UTF-8编码,使得Go语言无需关系机器码的排列顺序,也无需编码解码来使用其他语言。

我们上面提到了rune,那么我们下面就看看rune

😎😎😎双是我,小小分割线

rune:我就是上面提到的rune

在英文解释中:

rune :如尼字母(属于北欧古文字体系); 神秘的记号; 有魔力的符号

rune就是如尼文是一套字母表,开始它属于大约1500年前的北欧和日尔曼人。

这一“字母表”中的“字母”被认为包含着可以进行占卜的神秘因素。“如尼”(Rune)—词意思是“神秘的”或“隐蔽的”。

image.png

废话少说,上源码哈哈哈

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.

// 重新排列一下:

// rune is an alias for int32 and is equivalent to int32 in all ways. 

// It is used, by convention to distinguish character values from integer values.
type rune = int32

我们根据rune的源码可以知道

rune is an alias for int32 and is equivalent to int32 in all ways.

rune只是int32的别名,在所有方面都等价于int32

It is used, by convention to distinguish character values from integer values.

按照惯例,它用于区分字符值和整数值

说到runne就不得不提到byte

😎😎😎叒是我,小小分割线

byte:我也是用来处理字符的

那么:到底该选哪个呢?

认证!!!

image.png

上源码!!!

// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8

源码整理如下:

byte is an alias for uint8 and is equivalent to uint8 in all ways.

根据源码我们可以知道,byte也是一个别名,它是uint8的别名

byte是uint8的别名,在所有方面都相当于uint8。

It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.

按照惯例,它用于区分字节值和8位无符号整型值。

😎😎😎叕是我,小小分割线

byte和runne

golang中byte数据类型与rune相似

它们都是用来表示字符类型的变量类型

它们的不同在于:

  • byte是uint8的别名,常用来处理ascii字符

  • rune是int32的别名,常用来处理unicode或utf-8字符

😎😎😎叕又是我,小小分割线

都用心看到这里了,那就求个赞吧😘

🥳🥳🥳如果小伙伴有其他的小知识,一定不要忘了在评论区讨论哟,多多讨论,生态才会越来越好