How to change int to int64 in Golang? [SOLVED]

How to change int to int64 in Golang? [SOLVED]

In Golang, it is very simple to convert an int to int64. In today’s post, we will show you some simple examples of converting an int to int64. Depending on the platform, an int can be either 32 or 64 bits. Normally, an index, length, or capacity should be an int. The optimal data types are int8, int16, int32, and int64. The conversion between int and int64 is called type conversion.


Example 1: Cast int to int64

In the example below, we will try to convert an int to int64. We can use the fmt.Printf function with the %T verb to print out the type of the variable:

package main

import "fmt"

func main() {
    i := 1104
    var i64 int64
    i64 = int64(i)

    fmt.Printf("i value: %v, i type: %T\n", i ,i)
        fmt.Printf("i64 value: %v, i64 type: %T", i64, i64)
}

Output:

i value: 1104, i type: int
i64 value: 1104, i64 type: int64

Example 2: Cast int64 to int

In the example below, we will cast an int64 to int

package main

import "fmt"

func main() {
    var i64 int64
    i64 = 1104
    i := int(i64)

    fmt.Printf("i value: %v, i type: %T\n", i, i)
    fmt.Printf("i64 value: %v, i64 type: %T", i64, i64)
}

Output:

i value: 1104, i type: int
i64 value: 1104, i64 type: int64

Example 3: Convert an int64 to int with boundary cases

This casting method works well with the boundary cases such as math.MaxInt64 or math.MinInt64:

package main

import (
    "fmt"
    "math"
)

func main() {
    var maxI64 int64
    maxI64 = math.MaxInt64
    maxI := int(maxI64)

    var minI64 int64
    minI64 = math.MinInt64
    minI := int(minI64)

    fmt.Printf("min i value: %v, min i type: %T\n", minI, minI)
    fmt.Printf("min i64 value: %v, min i64 type: %T\n", minI64, minI64)

    fmt.Printf("max i value: %v, min i type: %T\n", maxI, maxI)
    fmt.Printf("max i64 value: %v, max i64 type: %T", maxI64, maxI64)
}

Output:

min i value: -9223372036854775808, min i type: int
min i64 value: -9223372036854775808, min i64 type: int64
max i value: 9223372036854775807, min i type: int
max i64 value: 9223372036854775807, max i64 type: int6

Example 4: Convert int to string and string to int64

We can use the strconv.Itoa function to convert an int to a decimal string and then use the strconv.ParseInt function to parse a decimal string to an int64. Let’s take a look at the example below to see how to use these two functions to convert int to int64:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := 1104
    fmt.Printf("i value: %v, i type: %T\n", i, i)
    iStr := strconv.Itoa(i)
    if i64, err := strconv.ParseInt(iStr, 10, 64); err == nil {
        fmt.Printf("i64 value: %v, i64 type: %T", i64, i64)
    } else {
        fmt.Println(i64, "is not an integer.")
    }
}

Output:

i value: 1104, i type: int
i64 value: 1104, i64 type: int64

Summary

In today’s article, I discussed the quickest way to cast an int to int64. We can directly cast int to int64 or versa (even with the boundary cases such as MaxInt64 and MinInt64 values). The other method is to convert int to string and then use the strconv.ParseInt() function to cast string to int64.


References

https://go.dev/doc/effective_go#conversionsr
https://pkg.go.dev/strconv

Tuan Nguyen

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.