| Tested on | RHEL 10.2 with Go 1.27.0 |
|---|---|
| Package | strconv, math (Go 1.27.0 standard library) |
| Applies to | Any host with Go installed |
| Privilege | Normal user |
| Scope | Convert int to int64 and int64 to int, int width with strconv.IntSize, safe narrowing with math.MinInt and math.MaxInt, when to pick each type, and strconv only for text. Does not cover arbitrary-precision integers or generic conversion helpers. |
| Related guides | Getting started with Go Golang float64 to int Golang byte to int Golang for loop Golang JSON omitempty |
To convert int to int64 in Go, use a conversion expression:
var i int = 42
n := int64(i)Converting int to int64 is safe because every value representable by Go's int type fits in int64 on supported architectures.
The reverse direction needs care:
i := int(n)int may be narrower than int64, so int64 to int can discard high-order bits without an error.
Convert int to int64
Go does not perform implicit numeric conversion between distinct integer types. This assignment does not compile:
var n int64 = i // i is intUse an explicit conversion instead:
i := 42
n := int64(i)Sample output when you print n:
42No strconv call is involved. The conversion widens the value to 64 bits and preserves the integer.
Why int and int64 are different types
int and int64 are separate types even when they hold the same number. That is why int64(i) is required instead of a plain assignment.
int has implementation-dependent width: either 32 or 64 bits on supported Go platforms. int64 is always exactly 64 bits.
The useful contract is that int is at least 32 bits and is either 32 or 64 bits. Do not assume int always matches a CPU register width in every conceptual context.
strconv.IntSize reports how many bits int uses on the target you build for:
fmt.Println(strconv.IntSize)On linux/amd64 with Go 1.27.0:
64On a 32-bit int target the same call prints 32.
Convert int64 to int
n := int64(42)
i := int(n)This is safe only when the value fits in the target int.
Go integer conversions do not report overflow. When converting between integer types, Go adjusts the representation to the destination width. If high-order bits do not fit, they are discarded; the conversion does not report overflow. int(n) does not return (value, error) and does not panic just because n is out of range for int.
On linux/amd64, int and int64 are both 64 bits, so ordinary values round-trip. On platforms where int is 32 bits, converting math.MaxInt64 to int truncates to a different number with no runtime error.
Safely convert int64 to int
Compare against the int range before narrowing when the source may be large, untrusted, or cross-platform:
func int64ToInt(n int64) (int, error) {
if n < int64(math.MinInt) || n > int64(math.MaxInt) {
return 0, errors.New("value does not fit in int")
}
return int(n), nil
}math.MinInt and math.MaxInt represent the platform's int limits on the build target.
Use this pattern for network or API input, database values, and any int64 that might exceed 32-bit range on some builds.
When should you use int vs int64?
Prefer int for ordinary in-memory Go code: slice indexes, lengths, loop counters, and APIs that naturally use int.
Prefer fixed-width types such as int32 or int64 when width is part of an external contract: database columns, file formats, binary protocols, serialized JSON fields, or API models that specify int64. Do not convert everything to int64 merely because it sounds larger.
Do you need strconv to convert int to int64?
No. When the value is already int, use int64(i).
strconv is for text. Parse decimal strings with strconv.ParseInt(text, 10, 64). Format integers with strconv.Itoa or fmt.Sprintf when you need a string representation.
Do not route numeric int values through strconv.Itoa and strconv.ParseInt just to reach int64. That path is indirect and adds error handling with no benefit when the source type is already an integer.
Common integer conversion mistakes
| Mistake | Correct approach |
|---|---|
Expecting automatic int → int64 assignment |
Use int64(i) |
Assuming int is always 64-bit |
It can be 32 or 64 bits; check strconv.IntSize |
Assuming int64 → int reports overflow |
Conversion itself reports no error |
| Narrowing untrusted or large values without checking | Compare against math.MinInt / math.MaxInt |
Converting int → string → int64 for numeric data |
Use direct int64(i) |
Using int in a fixed-width binary format |
Use int32, int64, or the width the spec defines |
References
- The Go Programming Language Specification — Conversions
- Package strconv — IntSize
- Package math — MinInt
- Package math — MaxInt
Summary
int64(i) widens int to int64 and is safe on supported Go architectures. int(n) narrows int64 to int and may truncate when int is narrower or when n exceeds the int range.
Go does not report overflow for integer conversions. Compare against math.MinInt and math.MaxInt before narrowing untrusted or large int64 values. Use strconv only when the input or output is text, not for a direct int to int64 conversion.

