Golang int8, int16, int32, float32 .... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Golang int8, int16, int32, float32 ....

hello, I am confused about the difference between int8, int16, int32, int64, uint8, uint16, and so on ... I know each one has a different length, but when do you use one of them? didn't they all give similar result? (a number)

31st Jan 2021, 6:12 PM
Yousef Ady
Yousef Ady - avatar
3 Answers
+ 3
because sometimes int32 or below is enough (and it could make a memory footprint very different for large dataset). however, basic use for few variable are commonly done with the widest int type available for your cpu (usualy int64 or int32 depending on platform)
31st Jan 2021, 11:59 PM
visph
visph - avatar
+ 1
they all give a similar result: a LIMITED integer. the optional leading 'u' is for unsigned (versus implicitly signed). the number after the 'int' give you the number of bits used to store the integer: 8 bits hold unsigned values from 0 to 2^8 not included (256), or signed from -2^7 to 2^7 (-128 to 127 included), and so on for each... it results to the wide is you int, the more it could handle number without overflowing ;)
31st Jan 2021, 7:57 PM
visph
visph - avatar
0
visph, why don't you just use int64? isn't int64 can hold numbers wider than int32 or below? and when do you use int8 or uint8?
31st Jan 2021, 11:54 PM
Yousef Ady
Yousef Ady - avatar