int64<\/td> | 64 bits\/8 byte<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n int<\/strong><\/p>\n\n\n\nSize: <\/strong>Platform Dependent. <\/p>\n\n\n\n- On 32 bit machines, the size of an int will be 32 bits or 4 bytes. <\/li><\/ul>\n\n\n\n
- On 64 bit machines, the size of an int will be 64 bits or 8 bytes<\/li><\/ul>\n\n\n\n
Range<\/strong>: Again Platform dependent<\/p>\n\n\n\n- On 32 bit machines, the range of int will be -231<\/sup> to 231<\/sup> -1.<\/li><\/ul>\n\n\n\n
- On 64 bit machines, the range of int will be -263<\/sup> to 263<\/sup> -1<\/li><\/ul>\n\n\n\n
When to Use:<\/strong><\/p>\n\n\n\n- It is a good idea to use int whenever using signed Integer other than the cases mentioned below
- When the machine is a 32 bit and range needed is greater than -231<\/sup> to 231<\/sup> -1, then use int64<\/strong> instead int<\/strong>. Note that in this case for int64, 2 32-bit memory addresses to form a 64-bit number together.<\/li><\/ul>
- When the range is less then use appropriate integer type.<\/li><\/ul><\/li><\/ul>\n\n\n\n
Properties:<\/strong><\/p>\n\n\n\n- Declare an int <\/li><\/ul>\n\n\n\n
var a int<\/code><\/pre>\n\n\n\n- int is default type for integer. <\/strong>When you don’t specify a type the default will be int<\/li><\/ul>\n\n\n\n
b := 2 \/\/The default is also int\nfmt.Println(reflect.TypeOf(b)) => int<\/code><\/pre>\n\n\n\n- bits <\/strong>package of golang can help know the size of an int <\/strong>on your system<\/li><\/ul>\n\n\n\n
\/\/This is computed as const uintSize = 32 << (^uint(0) >> 32 & 1) \/\/ 32 or 64\nsizeOfIntInBits := bits.UintSize\nfmt.Println(sizeOfIntInBits) => 32 0r 34<\/code><\/pre>\n\n\n\n- unsafe.Sizeof() <\/strong>function can also be used to see the size of int in bytes<\/li><\/ul>\n\n\n\n
Full Working Code <\/strong><\/p>\n\n\n\nBelow is the full working code of the above properties<\/p>\n\n\n\n package main\n\nimport (\n \"fmt\"\n \"math\/bits\"\n \"reflect\"\n \"unsafe\"\n)\n\nfunc main() {\n \/\/This is computed as const uintSize = 32 << (^uint(0) >> 32 & 1) \/\/ 32 or 64\n sizeOfIntInBits := bits.UintSize\n fmt.Printf(\"%d bits\\n\", sizeOfIntInBits)\n \n var a int\n fmt.Printf(\"%d bytes\\n\", unsafe.Sizeof(a))\n fmt.Printf(\"a's type is %s\\n\", reflect.TypeOf(a))\n \n b := 2\n fmt.Printf(\"b's typs is %s\\n\", reflect.TypeOf(b))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n64 bits\n8 bytes\na's type is int\nb's typs is int<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n int8<\/strong><\/p>\n\n\n\nSize: <\/strong>8 bits or 1 byte<\/p>\n\n\n\nRange<\/strong>: -27<\/sup> to 27<\/sup> -1.<\/p>\n\n\n\nWhen to Use:<\/strong><\/p>\n\n\n\n- Use int8 when there it is known that the int range will be between -27<\/sup> to 27<\/sup> -1. For temporary values such as loop invariants, it is still advisable to use int even though it might take more space because it is likely to be promoted to int in some operations or library calls.<\/li><\/ul>\n\n\n\n
- For array values which lies between -27 to 27 -1, is a good use case for using int8. For eg if you are storing ASCII index for lowercase letters then int8 can be used<\/li><\/ul>\n\n\n\n
- It is a good idea to use int8 for data values.<\/li><\/ul>\n\n\n\n
Example:<\/strong><\/p>\n\n\n\nThe below code example illustrates below points<\/p>\n\n\n\n - Declare an int8<\/li>
- Print size of int8 in bytes<\/li><\/ul>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"reflect\"\n \"unsafe\"\n)\n\nfunc main() {\n \/\/Declare a int 8\n var a int8 = 2\n \n \/\/Size of int8 in bytes\n fmt.Printf(\"%d bytes\\n\", unsafe.Sizeof(a))\n fmt.Printf(\"a's type is %s\\n\", reflect.TypeOf(a))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n1 bytes\na's type is int8<\/code><\/pre>\n\n\n\nint16<\/strong><\/p>\n\n\n\nSize: <\/strong>16 bits or 2 byte<\/p>\n\n\n\nRange<\/strong>: -215<\/sup> to 215<\/sup> -1.<\/p>\n\n\n\nWhen to Use:<\/strong><\/p>\n\n\n\n- Use int16 when there it is known that the int range will be between -215<\/sup> to 215<\/sup> -1. For temporary values such as loop invariants, it is still advisable to use int even though it might take more space because it is likely to be promoted to int in some operations or library calls.<\/li><\/ul>\n\n\n\n
- For array values which lies between -215 to 215 -1, is a good use case for using int8. For eg if you are storing ASCII index for lowercase letters than int16 can be used.<\/li><\/ul>\n\n\n\n
Example:<\/strong><\/p>\n\n\n\nThe below code example illustrates below points<\/p>\n\n\n\n - Declare an int16<\/li><\/ul>\n\n\n\n
- Print size of int16 in bytes<\/li><\/ul>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"reflect\"\n \"unsafe\"\n)\n\nfunc main() {\n \/\/Declare a int16\n var a int16 = 2\n \n \/\/Size of int8 in bytes\n fmt.Printf(\"%d bytes\\n\", unsafe.Sizeof(a))\n fmt.Printf(\"a's type is %s\\n\", reflect.TypeOf(a))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n2 bytes\na's type is int16<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n int32<\/strong><\/p>\n\n\n\nSize: <\/strong>32 bits or 4 byte<\/p>\n\n\n\nRange<\/strong>: -231<\/sup> to 231<\/sup> -1.<\/p>\n\n\n\nExample:<\/strong><\/p>\n\n\n\nThe below code example illustrates below points<\/p>\n\n\n\n - Declare an int32<\/li><\/ul>\n\n\n\n
- Print size of int8 in bytes<\/li><\/ul>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"reflect\"\n \"unsafe\"\n)\n\nfunc main() {\n \/\/Declare a int32\n var a int32 = 2\n \n \/\/Size of int32 in bytes\n fmt.Printf(\"%d bytes\\n\", unsafe.Sizeof(a))\n fmt.Printf(\"a's type is %s\\n\", reflect.TypeOf(a))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n4 bytes\na's type is int32<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n int64<\/strong><\/p>\n\n\n\nSize: <\/strong>64 bits or 8 byte<\/p>\n\n\n\nRange<\/strong>: -263<\/sup> to 263<\/sup> -1<\/p>\n\n\n\nWhen to Use:<\/strong><\/p>\n\n\n\n- int64<\/strong> is used when the range is higher. For eg
|