Golang strconv.FormatInt Function

The strconv.FormatInt function in Golang is part of the strconv package and is used to convert an integer value into its string representation in a specified numerical base. This function is particularly useful when you need to convert integers to strings for output, logging, or other forms of data processing, and you want control over the numerical base (e.g., binary, octal, decimal, hexadecimal).

Table of Contents

  1. Introduction
  2. strconv.FormatInt Function Syntax
  3. Examples
    • Basic Usage
    • Formatting with Different Bases
    • Combining with Other Formatting Functions
  4. Real-World Use Case Example
  5. Conclusion

Introduction

The strconv.FormatInt function provides a way to convert integers into their string representations with control over the numerical base. This is especially useful in situations where you need to display or store integer values in different bases, such as binary, octal, decimal, or hexadecimal.

strconv.FormatInt Function Syntax

The syntax for the strconv.FormatInt function is as follows:

func FormatInt(i int64, base int) string

Parameters:

  • i int64: The integer value to be formatted.
  • base int: The numerical base (2 to 36) used to format the integer as a string.

Returns:

  • string: The string representation of the integer in the specified base.

Behavior:

  • Converts the integer to a string: The function converts the integer value to its string representation in the specified base, ranging from binary (base 2) to hexadecimal (base 16) and beyond.

Examples

Basic Usage

This example demonstrates how to use strconv.FormatInt to convert an integer to its string representation in base 10.

Example

package main

import (
	"fmt"
	"strconv"
)

func main() {
	num := int64(12345)
	str := strconv.FormatInt(num, 10)
	fmt.Println("Formatted integer:", str)
}

Output:

Formatted integer: 12345

Explanation:

  • The strconv.FormatInt function converts the integer 12345 into the string "12345" in base 10.

Formatting with Different Bases

This example shows how to use strconv.FormatInt to format integers in different numerical bases.

Example

package main

import (
	"fmt"
	"strconv"
)

func main() {
	num := int64(255)

	binaryStr := strconv.FormatInt(num, 2)  // Base 2 (binary)
	octalStr := strconv.FormatInt(num, 8)   // Base 8 (octal)
	hexStr := strconv.FormatInt(num, 16)    // Base 16 (hexadecimal)

	fmt.Println("Binary:", binaryStr)
	fmt.Println("Octal:", octalStr)
	fmt.Println("Hexadecimal:", hexStr)
}

Output:

Binary: 11111111
Octal: 377
Hexadecimal: ff

Explanation:

  • The strconv.FormatInt function converts the integer 255 into its string representation in binary, octal, and hexadecimal formats.

Combining with Other Formatting Functions

This example demonstrates how to combine strconv.FormatInt with other formatting functions to create complex outputs.

Example

package main

import (
	"fmt"
	"strconv"
)

func main() {
	id := int64(42)
	price := float64(19.99)

	idStr := strconv.FormatInt(id, 10)
	priceStr := strconv.FormatFloat(price, 'f', 2, 64)

	output := "ID: " + idStr + ", Price: $" + priceStr
	fmt.Println(output)
}

Output:

ID: 42, Price: $19.99

Explanation:

  • The strconv.FormatInt function is used to convert the integer 42 to a string, and strconv.FormatFloat formats the floating-point number 19.99. The result is a formatted string that combines both values.

Real-World Use Case Example: Generating User IDs

A practical use case for strconv.FormatInt is generating and formatting user IDs for display or logging purposes.

Example: Generating User IDs

package main

import (
	"fmt"
	"strconv"
)

func main() {
	userID := int64(1001)
	logMessage := "User ID: " + strconv.FormatInt(userID, 10) + " has logged in."
	fmt.Println(logMessage)
}

Output:

User ID: 1001 has logged in.

Explanation:

  • The strconv.FormatInt function is used to convert the integer user ID to a string, which is then included in a log message.

Conclusion

The strconv.FormatInt function in Go is used for converting integers into strings with control over the numerical base. It is particularly useful in scenarios where you need to format integers in different bases, such as binary, octal, decimal, or hexadecimal. Whether you’re generating IDs, preparing data for output, or working with numbers in different bases, strconv.FormatInt provides a straightforward way to handle integer formatting in your Go applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top