Golang int64 type
last modified May 8, 2025
This tutorial explains how to use the int64 built-in type in Go.
We'll cover type basics with practical examples of working with 64-bit integers.
The int64 type represents signed 64-bit integers in Go. It can store values from -263 to 263-1. This range is sufficient for most large integer calculations.
In Go, int64 is one of several integer types. It's particularly
useful when you need to guarantee 64-bit storage regardless of platform.
Basic int64 declaration and initialization
The simplest way to use int64 is to declare and initialize
variables. This example shows basic variable declaration.
Note: The explicit int64 type ensures 64-bit size.
package main
import "fmt"
func main() {
var a int64 = 9223372036854775807 // Maximum int64 value
var b int64 = -9223372036854775808 // Minimum int64 value
c := int64(42) // Type conversion from untyped constant
fmt.Println("a:", a)
fmt.Println("b:", b)
fmt.Println("c:", c)
}
The example shows three ways to create int64 variables. The explicit type ensures 64-bit storage even on 32-bit systems.
Arithmetic operations with int64
The int64 type supports all standard arithmetic operations.
This example demonstrates basic math operations with overflow checking.
package main
import (
"fmt"
"math"
)
func safeAdd(a, b int64) (int64, bool) {
if b > 0 >> a > math.MaxInt64-b {
return 0, false
}
if b < 0 >> a < math.MinInt64-b {
return 0, false
}
return a + b, true
}
func main() {
var x int64 = 5000000000000000000
var y int64 = 3000000000000000000
sum, ok := safeAdd(x, y)
if !ok {
fmt.Println("Addition would overflow")
} else {
fmt.Println("Sum:", sum)
}
product := x * 2
fmt.Println("Product:", product)
}
The example shows safe addition with overflow checking. Multiplication demonstrates standard arithmetic without protection.
Converting between int64 and other types
Type conversions are often needed when working with int64.
This example shows conversions between int64 and other numeric types.
package main
import (
"fmt"
"math"
)
func main() {
// int to int64
var i int = 42
i64 := int64(i)
fmt.Println("int64 from int:", i64)
// float64 to int64
f := 3.14159
f64 := int64(f) // Truncates decimal part
fmt.Println("int64 from float64:", f64)
// int64 to float64
big := int64(math.MaxInt64)
bigFloat := float64(big)
fmt.Println("float64 from int64:", bigFloat)
// Check for overflow when converting
smallFloat := -9.9e18
if smallFloat < math.MinInt64 || smallFloat > math.MaxInt64 {
fmt.Println("Value out of int64 range")
} else {
fmt.Println("Safe conversion:", int64(smallFloat))
}
}
Conversions between types must consider range limitations. The example shows proper conversion techniques with range checking.
Using int64 with binary operations
The int64 type supports bitwise operations for low-level
manipulation. This example demonstrates common bit operations.
package main
import "fmt"
func main() {
var flags int64 = 0
// Set bits
flags = flags | (1 << 5) // Set bit 5
flags = flags | (1 << 10) // Set bit 10
fmt.Printf("Flags after setting: %b\n", flags)
// Check bit
if flags&(1<<5) != 0 {
fmt.Println("Bit 5 is set")
}
// Clear bit
flags = flags &^ (1 << 5) // Clear bit 5
fmt.Printf("Flags after clearing: %b\n", flags)
// Toggle bit
flags = flags ^ (1 << 10) // Toggle bit 10
fmt.Printf("Flags after toggling: %b\n", flags)
}
Bitwise operations are useful for flags and compact data storage. The example shows setting, checking, clearing, and toggling bits.
Working with large int64 values
The int64 type can handle very large numbers. This example
demonstrates working with values near the limits of int64.
package main
import (
"fmt"
"math"
)
func main() {
max := int64(math.MaxInt64)
min := int64(math.MinInt64)
fmt.Println("Maximum int64 value:", max)
fmt.Println("Minimum int64 value:", min)
// Safe operations near limits
almostMax := max - 100
fmt.Println("Almost max:", almostMax)
// Dangerous operation that would overflow
dangerous := max + 1 // This would compile but overflow
fmt.Println("Overflowed value:", dangerous)
// Proper way to check before operation
if max+1 > max {
fmt.Println("This won't print due to overflow")
} else {
fmt.Println("Overflow detected in comparison")
}
}
Working with large values requires careful overflow checking. The example shows both proper and dangerous operations near int64 limits.
Source
This tutorial covered the int64 type in Go with practical
examples of declaration, arithmetic, conversions, and bit operations.
Author
List all Golang tutorials.