dongxue7306 2015-07-07 10:42
浏览 138
已采纳

为什么Go有几种不同的整数类型?

I find it rather confusing that there are different integer types in Go. What is the necessity of defining these different categories, given that these distinctions are not present in many other major programming languages?

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr
  • 写回答

4条回答 默认 最新

  • doukuang6795 2015-07-07 10:54
    关注

    The C99 language has a similar classification thru <stdint.h> (and C is even more fine-grained, with types like int_fast32_t) ; having that many integral types is useful for portability and efficiency.

    Compatibility with C99 & C++ may be enough a reason for Go to have these types.

    You may want to write code which can efficiently run on embedded microcontrollers, tablets (32 bits ARM), cheap laptops (32 bits x86), bigger desktops (64 bits x86-64), servers (perhaps also PowerPC or 64 bits AARCH64 ARM), etc... And you have various programming models or ABIs on some operating systems (e.g. x86, x32, amd64 on my Linux desktop).

    On various architectures, the cost of integral operations may vary greatly. On some machines, adding an int might be more costly than adding a C long (or an int64 from Go). On other machines (probably most of them), it might be the opposite. And CPU cache considerations could matter a lot w.r.t. performance. And in some cases (e.g. if you have billion-sized array) data size matters a lot. At last, for binary data coming from outside, size, layout, alignment, and endianness matter a lot. Read about serialization.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序