golang

Golang Int64 to String Examples

In Go, converting an “int64” value to its string representation is a common task when working with numeric data. The “strconv” package provides a set of functions to perform this conversion conveniently. In this article, we will explore the several examples of converting an “int64” to a string in Go which uses the “strconv” functions which include the Itoa(), FormatInt(), FormatUint(), and FormatInt() functions. Despite these, we also have some other functions which are provided by Go that include the fmt.Sprintf() and fmt.Sprintf() functions.

Example 1: Golang Int64 to String Using the Strconv.Itoa() Function

Let’s begin with the strconv.Itoa() function of Go which is used to specifically switch an integer to its appropriate string format. “Itoa” here stands for “integer to ASCII.” The ASCII representation of “Int64” is explained in the following program:

package main
import (
    "fmt"
    "strconv"
)
func main() {
    MyString := strconv.Itoa(37)
    fmt.Println(MyString)
}

In this program, we import two packages: “fmt” and “strconv”. The “fmt” package provides the functions for formatted I/O operations, and the “strconv” package provides the functions to convert the values to and from the strings. After that, we define the main() function where the “MyString” variable is declared and assigned with the result of the strconv.Itoa() function. Here, the Itoa() function is passed with the integer which converts the integer into a string. In this instance, it transforms the number 37 into the appropriate string notation.

The output hence displays the transformed value of the integer into the string:

Example 2: Golang Int64 to String Using the Strconv.FormatInt() Function

Next is the strong.FormatInt() technique to convert a number in integer format to a string expression using a given base. It allows us to specify the base in which we want the integer to be represented as a string.

package main
import (
    "fmt"
    "strconv"
)
func main() {
    Int64Val := int64(365)
    str := strconv.FormatInt(Int64Val, 10)
    fmt.Println(str)
}

In this program, we use the two similar packages again as in the previous program: “fmt” and “strconv”. Then, we declare a variable named “Int64Val” and give it the value of 365. The “int64” type is a signed with a 64-bit integer type in Go, and the “365” value is explicitly converted to this type. Next, we define an “str” variable to employ the strconv.FormatInt() function. Using a provided basis, the FormatInt() function renders an integer as a string. In this case, it converts the “Int64Val” variable which is an “int64” to its decimal string representation by specifying a base of 10.

Therefore, the output of the program shows the converted string value of the “Int64Val” variable which is 365:

Example 3: Golang Int64 to String Using the Fmt.Sprintf() Function

In addition, the fmt.Sprintf() function in Go can also be used to transform the “Int64” to string. It formats and concatenates the strings based on a format specifier. Here, we demonstrate this function by running the example program in Go.

package main
import (
    "fmt"
    "reflect"
)
func main() {
    num := 1999
    fmt.Println(reflect.TypeOf(num))
    toStr := fmt.Sprintf("%v", num)
    fmt.Println(toStr)
    fmt.Println(reflect.TypeOf(toStr))
}

In this program, we import the necessary packages: “fmt” for formatting and printing and “reflect” for reflection operations. Then, inside the main() function, an integer variable, “num”, is declared and assigned with the value of 1999.

After that, we define the reflect.TypeOf() function that is called with “num” as an argument to determine the type of the variable. There, we have another declaration of the “toStr” variable where the fmt.Sprintf() function is used to convert the “num” variable to a string. The resulting string is assigned to the “toStr” variable. Again, we call the reflect.TypeOf() function but with the “toStr” as an argument to determine the type of the variable.

Thus, the output first represents that the initial type of the “num” variable is an integer. After converting it to the string, we can see that the string type is represented in the output:

Example 4: Golang Int64 to String Using the Strconv.FormatUint() Function

In the same manner, the strconv.FormatUint() function in Go is also used to convert but for an unsigned integer value to its string representation. It takes two arguments: the unsigned integer value and the base in which the value should be represented. Consider the program of the strconv.FormatUint() function:

package main
import (
    "fmt"
    "strconv"
)
func main() {
    MyInt := 42
    s1 := strconv.FormatUint(uint64(MyInt) , 5)
    fmt.Printf("Type : %T \nValue : %v\n", s1, s1)

}

In this program, we set the “fmt” and “strconv” packages to access the required operation to be done for the program. After that, we establish the “MyInt” variable and assign the value of 42. Here, MyInt’s type is assumed to be an “int” based on the value that was given to it. We perform the conversion of the “MyInt” variable to a string representation using the strconv.FormatUint() function.

Here, it takes two arguments: uint64(MyInt) converts the “MyInt” variable to an unsigned 64-bit integer, and “5” specifies the base for the conversion. The format string “Type : %T \nValue : %v\n” specifies the format of the output with “%T” as a placeholder for the type of the variable, and “%v” as a placeholder for the value. In the “s1” variable, “s1” replaces the placeholders in the format string which prints the actual type and value of the “s1” variable.

The integer value to a string representation in base 5 is converted as shown in the output where the type and value are printed:

Example 5: Golang Int64 to String Using the Strconv.FormatInt() Function

For the conversion of the signed integer value to its string representation, Go provides the strconv.FormatInt() function. The strconv.FormatInt() function is used in the following for the conversion of the signed “Int64” conversion into the type “String”:

package main
import (
    "fmt"
    "strconv"
)
func main() {
    var x int64 = 30
    var y int64 = 8
    fmt.Println(strconv.FormatInt(x, 16))
    fmt.Println(strconv.FormatInt(y, 2))  
}

In the program, we declare the “x” and “y” variables of type “int64” and assign them with the respective values of 30 and 8. Then, we define the strconv.FormatInt() function to convert the value of “x” to a string representation in base 16 (hexadecimal). Two inputs are required by the FormatInt() function: the base of the output string representation and the integer value to be transformed (x). Next, we convert the value of the “y” variable to a string representation in base 2 (binary) using the strconv.FormatInt() function.

The output shows the conversion of the values to string representations in different number bases:

Conclusion

Converting the “int64” values to strings is a straightforward task in Go. These examples are used in the article to demonstrate the different approaches to convert an “int64” value to its string representation in Go. Finally, we have the option of choosing the approach that best meets our needs based on the particular requirements of the application.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content