What is the strconv.FormatUint() Function in Golang

Golang strconv.FormatUint() function “converts unsigned integer values (e.g., uint, uint16, uint32, uint64) to strings”. The FormatUint() function takes two arguments: the unsigned integer value (as uint64) and the base (radix) of the number system to use for the string representation.

Syntax

func FormatUint(i uint64, base int) string

Parameters

  1. “i” is the unsigned integer value of type uint64 that you want to convert to a string representation.
  2. “base” is an int representing the base (radix) of the number system to use for the string representation (e.g., 2 for binary, 10 for decimal, 16 for hexadecimal).

Return value

The FormatUint() function returns a string representing the given unsigned integer value in the specified base.

Example

package main

import (
  "fmt"
  "strconv"
)

func main() {
  var myUint uint = 123

  // Convert uint to string using strconv.FormatUint() with base 10 (decimal)
  myString := strconv.FormatUint(uint64(myUint), 10)

  fmt.Println("Converted uint to string:", myString)
}

Output

Converted uint to string: 123

In this code, we converted a uint value to a decimal string representation by casting the uint value to uint64 and then passing it along with the base 10 to the strconv.FormatUint() function. The resulting string is printed to the console.

Leave a Comment