教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang基础数据类型-整型

golang基础数据类型-整型

发布时间:2022-03-06   编辑:jiaochengji.com
教程集为您提供golang基础数据类型-整型等资源,欢迎您收藏本站,我们将为您提供最新的golang基础数据类型-整型资源

go同时提供了有符号和无符号的整数类型

<h2>有符号</h2> <pre><code class="lang-go hljs">// int8 is the set of all signed 8-bit integers. // Range: -128 through 127. type int8 int8 // int16 is the set of all signed 16-bit integers. // Range: -32768 through 32767. type int16 int16 // int32 is the set of all signed 32-bit integers. // Range: -2147483648 through 2147483647. type int32 int32 // int64 is the set of all signed 64-bit integers. // Range: -9223372036854775808 through 9223372036854775807. type int64 int64 </code></code></pre> <h2>无符号</h2> <pre><code class="lang-go hljs">// uint8 is the set of all unsigned 8-bit integers. // Range: 0 through 255. type uint8 uint8 // uint16 is the set of all unsigned 16-bit integers. // Range: 0 through 65535. type uint16 uint16 // uint32 is the set of all unsigned 32-bit integers. // Range: 0 through 4294967295. type uint32 uint32 // uint64 is the set of all unsigned 64-bit integers. // Range: 0 through 18446744073709551615. type uint64 uint64 </code></code></pre> <h2>和平台相关</h2>

无固定大小,和特定平台相关,32位平台就是32位,64位平台就是64位

<pre><code class="lang-go hljs">// 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. type int int // uint is an unsigned integer type that is at least 32 bits in size. It is a // distinct type, however, and not an alias for, say, uint32. type uint uint </code></code></pre> <h2>别名</h2>

一般只有处理字符的时候使用

<pre><code class="lang-go hljs">// 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 // 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 </code></code></pre> <h2>针对指针</h2>

还有一种无符号的整数类型uintptr,没有指定具体的bit大小但是足以容纳指针。

uintptr类型只有在底层编程时才需要,特别是Go语言和C语言函数库或操作系统接口相交互的地方。

<pre><code class="lang-go hljs">// uintptr is an integer type that is large enough to hold the bit pattern of // any pointer. type uintptr uintptr </code></code></pre> <h2>零值和默认类型</h2> <pre><code class="lang-go hljs">func TestInt1(t *testing.T) { // 零值0 var num1 int8 fmt.Println(num1) // 0 // 默认类型int num2 := 10 fmt.Println(reflect.TypeOf(num2), num2) // int 10 } </code></code></pre> <h2>有符号和无符号的选择</h2>

虽然go同时提供了有符号和无符号的整数类型,但是大多数情况下,我们编码过程中还是更加倾向于使用有符号的int类型。比如说数组的长度,虽然不可能为负数,但是内置的len函数返回的还是一个有符号的int,这样做是有原因的,看以下代码:

<pre><code class="lang-go hljs">func TestInt1(t *testing.T) { medals := []string{"gold", "silver", "bronze"} for i := len(medals) - 1; i >= 0; i-- { fmt.Println(medals[i]) // "bronze", "silver", "gold" } } </code></code></pre>

如果说len返回的是一个无符号的整数类型,那么i也是一个无符号的整数类型,i--一直不会小于0,则i >= 0一直为真

无符号数往往只有在位运算或其它特殊的运算场景才会使用,就像bit集合、分析二进制文件格式或者是哈希和加密操作等。它们通常并不用于仅仅是表达非负数量的场合。

<h2>边界值</h2>

math包里面已经定义了

<pre><code class="lang-go hljs">// Integer limit values. const ( MaxInt8 = 1<<7 - 1 MinInt8 = -1 << 7 MaxInt16 = 1<<15 - 1 MinInt16 = -1 << 15 MaxInt32 = 1<<31 - 1 MinInt32 = -1 << 31 MaxInt64 = 1<<63 - 1 MinInt64 = -1 << 63 MaxUint8 = 1<<8 - 1 MaxUint16 = 1<<16 - 1 MaxUint32 = 1<<32 - 1 MaxUint64 = 1<<64 - 1 ) </code></code></pre> <h2>范围溢出,不会报错</h2> <pre><code class="lang-go hljs">func TestInt(t *testing.T) { // 上限溢出 var num1 int8 = math.MaxInt8 fmt.Println(num1) // 127 num1 fmt.Println(num1) // -128 // 下限溢出 var num2 int16 = math.MinInt16 fmt.Println(num2) // -32768 num2-- fmt.Println(num2) // 32767 // 无符号整数 var i uint8 = 0 i-- fmt.Println(i) // 255 } </code></code></pre>
到此这篇关于“golang基础数据类型-整型”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
golang基础教程
golang lesson3 - 基础数据类型
golang中接口的内部实现
Go:06---语言数据类型整体介绍、数据类型检查(reflect包)、数据类型转换
C语言 的 整型数据和浮点型数据
Go数据类型整理
Go数组、切片、映射的原理--简明解析
学习golang开始前的准备工作
新手入门PHP必知的七种数据类型
golang基础数据类型-整型

[关闭]
~ ~