uint<\/td> 32 bits or 4 byte<\/td> 64 bits or 8 byte<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\nRange Table<\/strong><\/p>\n\n\n\nType<\/strong><\/td>Size (32 bit machine)<\/strong><\/td>Size (64 bit machine)<\/strong><\/td><\/tr>int<\/td> -231<\/sup> to 231<\/sup> -1<\/td>-263<\/sup> to 263<\/sup> -1<\/td><\/tr>uint<\/td> 0 to 232<\/sup> -1<\/td>0 to 264<\/sup> -1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/span>Know Size and Range<\/strong><\/span><\/h1>\n\n\n\nbits <\/strong>package of golang can help know the size of an int <\/strong>or uint <\/strong>on your system. bits.UintSize<\/strong> is the const that stores this value. It is calculated as below<\/li><\/ul>\n\n\n\nconst uintSize = 32 << (^uint(0) >> 32 & 1) \/\/ 32 or 64<\/code><\/pre>\n\n\n\nunsafe.Sizeof() <\/strong>function can also be used to see the size of int <\/strong>or unit<\/strong> in bytes<\/li><\/ul>\n\n\n\nOnce the size is known, the range can be deduced based upon size. See the below code for printing size.<\/p>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"math\/bits\"\n \"unsafe\"\n)\n\nfunc main() {\n \/\/This is computed as \n \/\/const uintSize = 32 << (^uint(0) >> 32 & 1) \/\/ 32 or 64\n sizeInBits := bits.UintSize\n fmt.Printf(\"%d bits\\n\", sizeInBits)\n\n \/\/Using unsafe.Sizeof() function. It will print size in bytes\n var a int\n fmt.Printf(\"%d bytes\\n\", unsafe.Sizeof(a))\n\n \/\/Using unsafe.Sizeof() function. It will print size in bytes\n var b uint\n fmt.Printf(\"%d bytes\\n\", unsafe.Sizeof(b))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n64 bits\n8 bytes\n8 bytes<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewKnow Size and Range Overview int is a signed Integer data type uint is an unsigned Integer data type Size and range of int and uint in go is…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4,135,134,136],"class_list":["post-1062","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang","tag-int","tag-range","tag-uint"],"yoast_head":"\n
Know Size and Range of int or uint in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n