If you want to find out the size of a particular value, there are two ways to do that - using the unsafe package, or using the reflection package. The following code demonstrates both:

package main

import (
    "fmt"
    "reflect"
    "unsafe"
)

func main() {
    var i int
    fmt.Printf("Size of var (reflect.TypeOf.Size): %d\n", reflect.TypeOf(i).Size())
    fmt.Printf("Size of var (unsafe.Sizeof): %d\n", unsafe.Sizeof(i))
}

However, I am not aware of a way to get the size of a type directly. But I think you'll find out that the sizeof function is not needed as often as in C.

Answer from rob74 on Stack Overflow
🌐
W3Schools
w3schools.com › go › go_integer_data_type.php
Go Integer Data Types
Integer data types are used to store a whole number without decimals, like 35, -50, or 1345000.
Discussions

Int vs Int8, int 32
When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type. More on reddit.com
🌐 r/golang
38
26
December 20, 2023
c++ - Equivalent of sizeof(aType) in Go - Stack Overflow
C++ and several other languages have a function called sizeof(int) (or whatever type you need) that returns the number of bytes consumed by a particular data type, in the current system. Is there an More on stackoverflow.com
🌐 stackoverflow.com
What is the difference between int and int64 in Go? - Stack Overflow
I have a string containing an integer (which has been read from a file). I'm trying to convert the string to an int using strconv.ParseInt(). ParseInt requires that I provide a bitsize (bit size... More on stackoverflow.com
🌐 stackoverflow.com
Very noob question, but I am just wrapping my head about different types of numbers... if I set a variable to be int... how could I find out what int it is? (int32, int64 etc?)
Hey not a dumb question at all. An int is a third, seperate type. But of course it does have a length- usually 64 bit but could be 32. Rule of thumb: use int only for for loops and such, and use sized ints for manipulating data. More on reddit.com
🌐 r/golang
12
17
September 24, 2023
🌐
Medium
medium.com › @tbpalsulich › how-big-is-an-int-in-go-f0e715342a32
How big is an int in Go?. Go has several built-in numeric types… | by Tyler Bui-Palsulich | Medium
October 9, 2018 - You can see the definition of strconv.IntSize in https://golang.org/src/strconv/atoi.go: const intSize = 32 << (^uint(0) >> 63)// IntSize is the size in bits of an int or uint value.const IntSize = intSize ·
🌐
Go Tutorial
golangbot.com › types
Basic Data Types in Go | golangbot.com
May 5, 2024 - We can infer from the above output that a and b are of type int and they have a size of 8 bytes(64 bits). The output will vary if you run the above program on a 32 bit system.
🌐
Medium
medium.com › @LukePetersonAU › understanding-integer-types-in-go-a55453f5ae00
Understanding integer types in Go | by Luke Peterson | Medium
April 8, 2024 - Besides these, Go also offers int and uint, which are platform-dependent. They are 32 bits in size on 32-bit systems and 64 bits in size on 64-bit systems.
🌐
Rotational
rotational.io › blog › ranges-of-integer-data-types
Rotational Labs | Ranges of Integer Data Types
This blog post is a quick reference for the standard sizes and a discussion on why it matters. So without further ado, here are the ranges of the standard numeric types: ... // Constants defined in the math package const ( MaxInt8 int8 = 1<<7 - 1 MinInt8 int8 = -1 << 7 MaxInt16 int16 = 1<<15 - 1 MinInt16 int16 = -1 << 15 MaxInt32 int32 = 1<<31 - 1 MinInt32 int32 = -1 << 31 MaxInt64 int64 = 1<<63 - 1 MinInt64 int64 = -1 << 63 MaxUint8 uint8 = 1<<8 - 1 MaxUint16 uint16 = 1<<16 - 1 MaxUint32 uint32 = 1<<32 - 1 MaxUint64 uint64 = 1<<64 - 1 ) // Complement method (system specific) const ( MaxUint uint = ^uint(0) MinUint uint = 0 MaxInt int = int(MaxUint >> 1) MinInt int = -MaxInt - 1 )
Find elsewhere
Top answer
1 of 6
78
func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt always returns int64.

bitSize defines the range of values.

If the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

http://golang.org/pkg/strconv/#ParseInt

type int 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.

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

So int could be bigger than 32 bit in the future or on some systems like int in C.

I guess on some systems int64 might be faster than int32 because that system only works with 64-bit integers.

Here is an example of an error when bitSize is 8:

http://play.golang.org/p/_osjMqL6Nj

package main

import (
    "fmt"
    "strconv"
)

func main() {
    i, err := strconv.ParseInt("123456", 10, 8)
    fmt.Println(i, err)
}
2 of 6
36

Package strconv

func ParseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt interprets a string s in the given base (2 to 36) and returns the corresponding value i. If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.

The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

ParseInt always returns an int64 value. Depending on bitSize, this value will fit into int, int8, int16, int32, or int64. If the value cannot be represented by a signed integer of the size given by bitSize, then err.Err = ErrRange.

The Go Programming Language Specification

Numeric types

The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

There is also a set of predeclared numeric types with implementation-specific sizes:

uint     either 32 or 64 bits
int      same size as uint

int is either 32 or 64 bits, depending on the implementation. Usually it's 32 bits for 32-bit compilers and 64 bits for 64-bit compilers.

To find out the size of an int or uint, use strconv.IntSize.

Package strconv

Constants

const IntSize = intSize

IntSize is the size in bits of an int or uint value.

For example,

package main

import (
    "fmt"
    "runtime"
    "strconv"
)

func main() {
    fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
    fmt.Println(strconv.IntSize)
}

Output:

gc amd64 linux
64
🌐
LabEx
labex.io › tutorials › go-how-to-determine-int-size-on-platform-446330
How to determine int size on platform - Golang
This tutorial provides comprehensive insights into detecting and managing integer sizes, helping developers write more flexible and platform-independent code by leveraging Golang's built-in capabilities for type detection and memory management.
🌐
Educative
educative.io › answers › what-is-type-int-in-golang
What is type int in Golang?
int is one of the available numeric data types in Go. int has a platform-dependent size, as, on a 32-bit system, it holds a 32 bit signed integer, while on a 64-bit system, it holds a 64-bit signed integer.
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-data-types-in-go
Understanding Data Types in Go | DigitalOcean
May 1, 2019 - The second type is an implementation-specific type. In this type, the bit size can vary based on the architecture the program is built on. For instance, if we use the int type, when Go compiles for a 32-bit architecture, the size of the data type will be 32 bits.
🌐
YourBasic
yourbasic.org › golang › int-vs-int64
Pick the right one: int vs. int64 · YourBasic Go
The int type is either 32 or 64 bits, and always big enough to hold the maximum possible length of an array.
🌐
CalliCoder
callicoder.com › golang-basic-types-operators-type-conversion
Golang Basic Types, Operators and Type Conversion | CalliCoder
February 18, 2022 - When you are working with integer values, you should always use the int data type unless you have a good reason to use the sized or unsigned integer types. In Golang, you can declare octal numbers using prefix 0 and hexadecimal numbers using the prefix 0x or 0X.
🌐
Ado
ado.xyz › blog › go-numerical-type-ranges
Go Numerical Type Ranges - Ado.xyz
May 9, 2020 - The range for the int8 type is between -128 and 127.
🌐
Go Packages
pkg.go.dev › builtin
builtin package - builtin - Go Packages
int is a signed integer type that is at least 32 bits in size.
🌐
Reddit
reddit.com › r/golang › very noob question, but i am just wrapping my head about different types of numbers... if i set a variable to be int... how could i find out what int it is? (int32, int64 etc?)
Very noob question, but I am just wrapping my head about different types of numbers... if I set a variable to be int... how could I find out what int it is? (int32, int64 etc?) : r/golang
September 24, 2023 - Share · Share · Sort by: Best · Open comment sort options · Best · Top · New · Controversial · Old · Q&A · witty82 · • 3y ago · Hey not a dumb question at all. An int is a third, seperate type. But of course it does have a length- usually 64 bit but could be 32. Rule of thumb: ...
🌐
Google Groups
groups.google.com › g › golang-dev › c › fKUn_jWtnmA
The size of int and uint
Similarly, I could imagine hashing a stream of ints as "hash = rotateLeft(hash, 5) ^ i", and the rotateLeft implementation depends on sizeof(int). It's not a great hash, but it's quick and simple. I'm not saying that these use cases are common, I'm just suggesting use cases for a IntBits constant. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message · to David Symonds, r...@golang.org, snilsson, golang-dev