2

golang基础数据类型-整型

 3 years ago
source link: https://studygolang.com/articles/33715
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

有符号

// 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

无符号

// 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

和平台相关

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

// 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

别名

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

// 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

针对指针

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

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

// uintptr is an integer type that is large enough to hold the bit pattern of
// any pointer.
type uintptr uintptr

零值和默认类型

func TestInt1(t *testing.T) {
   // 零值0
   var num1 int8
   fmt.Println(num1) // 0
     
   // 默认类型int
   num2 := 10
   fmt.Println(reflect.TypeOf(num2), num2) // int 10
}

有符号和无符号的选择

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

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"
   }
}

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

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

边界值

math包里面已经定义了

// 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
)

范围溢出,不会报错

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
}

有疑问加站长微信联系(非本文作者)

eUjI7rn.png!mobile

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK