golang中数据类型及获取Int数据类型的最大值

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字符串

取得int数据类型最大值

  1. 最大值
    uint8 : 0 to 255
    uint16 : 0 to 65535
    uint32 : 0 to 4294967295
    uint64 : 0 to 18446744073709551615
    int8 : -128 to 127
    int16 : -32768 to 32767
    int32 : -2147483648 to 2147483647
    int64 : -9223372036854775808 to 9223372036854775807

  2. math包
    比如: math.MaxInt64,结果为9223372036854775807

  3. 取反:
    通过取反和移位的方式来取得最大值

      a. 如果是一个无符号的数字

      那么最大值就是  1111

      那么就是  ^uint8(0)  ,^uint16(0)  ^uint32(0)  ^uint64(0)

      
      b. 如果是一个有符号的数数字

      那么最大值就是   0111

      那么就是 0000 取反  1111  后移一位 0111

      那么就是   int8(^uint8(0) >> 1)  int16(^uint16(0) >>1 )

参考链接:
golang各种int最大值
go语言int类型最大值

posted @ 2022-01-10 11:17  cosmoswong  阅读(6851)  评论(0编辑  收藏  举报