Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of the general types (int / uint) over specific types (int64 / uint64) in Go lang?

I understand that int and uint are 64bit signed / unsigned integers - just like int64 / uint64. And I also understand that int is not simply an alias for int64 (like byte -> uint8 is), so int64 would need to be converted to int and visa versa when applicable. However what are the benefits of using one over the other? Is there any run time performance penalty for using the general types?

Sorry if this is a common question - I had Googled for the answer (and searched on here too) as I'd have thought others might have cropped up before but didn't find anyone answer the question in terms of how they affect performance (if at all), memory usage (I'm guessing not if they're both 64bit integers?) nor how the compiler treats them.

edit: I'm aware that int / unit are 32bit on 32bit architectures. For the sake of brevity and comparing like for like, I was assuming this is a 64bit Golang environment.

like image 424
Laurence Avatar asked May 07 '13 19:05

Laurence


People also ask

What is the difference between int and Int64 Golang?

int is one of the available numeric data types in Go used to store signed integers. int64 is a version of int that only stores signed numeric values composed of up to 64 bits.

What is the difference between UInt64 and Int64?

Int64 is used to represents 64-bit signed integers . UInt64 is used to represent 64-bit unsigned integers.

What is difference between int and UINT in Golang?

uint means “unsigned integer” while int means “signed integer”. Unsigned integers only contain positive numbers (or zero).

Should I use int or Int64?

The types int8 , int16 , int32 , and int64 (and their unsigned counterparts) are best suited for data. An int64 is the typical choice when memory isn't an issue.


1 Answers

int and uint are only 64-bit on 64-bit architectures. On 32-bit architectures they are 32 bits.

The general answer is that, unless you need a certain precision, sticking with datatypes that are the same size as a word on the current architecture (e.g. 32 bits on a 32-bit architecture) is typically slightly more efficient.

like image 140
Lily Ballard Avatar answered Oct 14 '22 13:10

Lily Ballard