Convert int to int64 and int64 to int in Go

Deepak Prasad
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:

go
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:

go
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:

go
var n int64 = i // i is int

Use an explicit conversion instead:

go
i := 42
n := int64(i)

Sample output when you print n:

output
42

No 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:

go
fmt.Println(strconv.IntSize)

On linux/amd64 with Go 1.27.0:

output
64

On a 32-bit int target the same call prints 32.


Convert int64 to int

go
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:

go
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 intint64 assignment Use int64(i)
Assuming int is always 64-bit It can be 32 or 64 bits; check strconv.IntSize
Assuming int64int 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


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.


Frequently Asked Questions

1. How do I convert int to int64 in Go?

Use a conversion expression n := int64(i) where i has type int. Go does not assign between distinct integer types without an explicit conversion.

2. Is converting int to int64 safe?

Yes for Go supported int widths. Every value representable as int fits in int64 because int is either 32 or 64 bits and int64 is always 64 bits.

3. Is converting int64 to int always safe?

No. The value must fit in the destination int. On 32-bit int platforms, large int64 values are truncated when converted to int.

4. Does Go throw an overflow error when converting int64 to int?

No. Integer conversions do not return errors and do not panic solely because the value is out of range for the destination type.

5. How can I check whether an int64 fits in int?

Compare the int64 against int64(math.MinInt) and int64(math.MaxInt) before calling int(n).

6. Is int always 64-bit in Go?

No. int is either 32 or 64 bits depending on the target architecture. strconv.IntSize reports the width in bits.

7. Do I need strconv for int to int64 conversion?

No when the source is already int. Use strconv.ParseInt or strconv.Atoi only when the input is text.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise spanning these technologies, he develops robust solutions and implements efficient data processing and management strategies across various projects and platforms.

  • Go (programming language)
  • Python (programming language)
  • Java (programming language)
  • MongoDB
  • Kubernetes