if int and int32 the same, why golang keeps these two?

xuanbao · 2018-02-23 02:30:02 · 607 次点击    
这是一个分享于 2018-02-23 02:30:02 的资源,其中的信息可能已经有所发展或是发生改变。

tbh, this raises confusing if you were programming in java or cpp


评论:

iroflmaowtf:

https://golang.org/pkg/builtin/#int

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.

:

[deleted]

TheMerovius:

int is 64 bits on 64-bit platforms, and 32 bits on 32-bit platforms.

nit: int is either int32 or int64; an implementation can choose whatever it wants, no matter what platform it runs on (for example, GopherJS on my laptop runs on a 64 bit platform, but uses int32 for ints, as they have to fit in a float64).

nsd433:

As others have said, int and int32 are not the same, and that's why you have both. Most of the time you want a reasonable int. Other times you want an int which truncates at 32 bits.

You mention C++. C and C++ have the same destinction. In C/C++ 'int' is a reasonable integer size on the target CPU, and int32_t is a signed integer with exactly 32 bits.

faiface:

Surprised no one mentioned that int usually has 64 bits on 64-bit systems and 32 bits on 32-bit systems. The distinction from both int32 and int64 is clear.

irene634:

If that is correct, it is the same to use int and int64 on 64-bit systems?

faiface:

No. On 64-bit systems, both types allow same sets of values, but are different in the Go type system and values of one need to be explicitly converted to the other.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

607 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传