int64 in Go



In this tutorial, we will learn about the int64 data type in Go. We will cover the basics of defining and using int64 values, including how to perform arithmetic operations and compare 64-bit integers.


Understanding the Int64 Data Type in Go

The int64 data type in Go is used to represent 64-bit signed integer values. The range of an int64 is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.


Defining an Int64 Variable

Int64 variables in Go can be defined using the var keyword or by type inference with explicit type conversion.

var x int64 = 9223372036854775807
y := int64(10000000000)

Performing Arithmetic Operations

Arithmetic operations such as addition, subtraction, multiplication, and division can be performed on int64 values.

sum := x + y
difference := x - y
product := x * y
quotient := x / y

Comparing Int64 Values

Int64 values can be compared using relational operators.

fmt.Println(x > y)
fmt.Println(x < y)
fmt.Println(x == y)


Example 1: Defining and using Int64 Variables

We can define and use int64 variables in Go to represent 64-bit signed integer values.

For example,

  1. Define an int64 variable named x and assign it a value.
  2. Define another int64 variable named y using type inference with explicit type conversion and assign it a value.
  3. Print the values of both int64 variables to the console.

Go Program

package main
import "fmt"
func main() {
    var x int64 = 9223372036854775807
    y := int64(10000000000)
    fmt.Println(x)
    fmt.Println(y)
}

Output

9223372036854775807
10000000000


Example 2: Performing Arithmetic Operations

We can perform arithmetic operations on int64 values in Go.

For example,

  1. Define two int64 variables named x and y.
  2. Perform addition, subtraction, multiplication, and division on these int64 values.
  3. Print the results of these operations to the console.

Go Program

package main
import "fmt"
func main() {
    x := int64(9223372036854775807)
    y := int64(10000000000)
    sum := x + y
    difference := x - y
    product := x * y
    quotient := x / y
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", difference)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
}

Output

Sum: -9223372026854775809
Difference: 9223372026854775807
Product: 46116860184273879040000000000
Quotient: 922337203


Example 3: Comparing Int64 Values

We can compare int64 values in Go using relational operators.

For example,

  1. Define two int64 variables named x and y with different values.
  2. Use relational operators to compare the int64 values and print the results to the console.

Go Program

package main
import "fmt"
func main() {
    x := int64(9223372036854775807)
    y := int64(10000000000)
    fmt.Println(x > y)  // true
    fmt.Println(x < y)  // false
    fmt.Println(x == y) // false
}

Output

true
false
false


Example 4: Using Int64 in a Function

We can pass int64 values to functions and return int64 values from functions in Go.

For example,

  1. Define a function named subtract that takes two int64 parameters and returns their difference.
  2. Call the function with int64 arguments and print the result to the console.

Go Program

package main
import "fmt"
func subtract(a int64, b int64) int64 {
    return a - b
}
func main() {
    result := subtract(20000000000, 10000000000)
    fmt.Println(result)
}

Output

10000000000