go语言中各整型的取值范围

golang中的数据类型

类型 名称 长度 零值 说明
bool 布尔类型 1 false 其值不为真即为家,不可以用数字代表true或false
byte 字节型 1 0 uint8别名
rune 字符类型 4 0 专用于存储unicode编码,等价于uint32
uint, int, 整型 4或8 0 32位或64位
uint8, int8 整型 1 0 -128 ~ 127, 0 ~ 255
uint16, int16 整型 2 0 -32768 ~ 32767, 0 ~ 65535
uint32, int32 整型 4 0 -21亿 ~ 21 亿, 0 ~ 42 亿
uint64, int64 整型 8 0
float32 浮点型 4 0.0 小数位精确到7位
float64 浮点型 8 0.0 小数位精确到15位
complex64 复数类型 8
complex128 复数类型 16
uintptr 整型 4或8 ⾜以存储指针的uint32或uint64整数
string 字符串 "" utf-8字符串

各种整型的取值范围计算原理:通过取反或者移位的方式来取得最大值最小值

  • uint8 : 0 to 255

    Max  = 1<<8 - 1
    //or
    Max  = ^uint8(0)
    
    Min = ^uint8(0)>>8
    
  • uint16 : 0 to 65535

    Max  = 1<<16 - 1
    //or
    Max  = ^uint16(0)
    
    Min = ^uint16(0)>>16
    
  • uint32 : 0 to 4294967295

    Max  = 1<<32 - 1
    //or
    Max  = ^uint32(0)
    
    Min = ^uint32(0)>>32
    
  • uint64 : 0 to 18446744073709551615

    Max  = 1<<64 - 1
    //or
    Max  = ^uint64(0)
    
    Min = ^uint64(0)>>64
    
  • int8 : -128 to 127

    Max  = 1<<7 - 1
    //or
    Max  = ^uint8(0) >> 1
    
    Min = -1 << 7
    
  • int16 : -32768 to 32767

    Max  = 1<<15 - 1
    //or
    Max  = ^uint16(0) >> 1
    
    Min = -1 << 15
    
  • int32 : -2147483648 to 2147483647

    Max  = 1<<31 - 1
    //or
    Max  = ^uint32(0) >> 1
    
    Min = -1 << 31
    
  • int64 : -9223372036854775808 to 9223372036854775807

Max  = 1<<63 - 1
//or
Max  = ^uint64(0) >> 1

Min = -1 << 63

另外,go中的int或者uint根据操作系统不同,有32位或者64位

posted on 2022-04-29 14:30  xufat  阅读(738)  评论(0)    收藏  举报

导航

/* 返回顶部代码 */ TOP