How to Convert Uint16 to String in Golang

To convert a uint16 value to a string in Go, use the “strconv.FormatUint() or strconv.Itoa() function.”

Method 1: Using the strconv.FormatUint() function

package main

import (
  "fmt"
  "strconv"
)

func main() {
  var myUint16 uint16 = 42

  // Convert uint16 to string using strconv.FormatUint()
  myString := strconv.FormatUint(uint64(myUint16), 10)

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

Output

Converted uint16 to string: 42

Method 2: Using the strconv.Itoa() function

The strconv.Itoa() function takes an int as an argument, and you need to convert your uint16 value to an int and then use the strconv.Itoa() function to convert an int to a string.

package main

import (
  "fmt"
  "strconv"
)

func main() {
  var myUint16 uint16 = 42

  // Convert uint16 to int, then to string using strconv.Itoa()
  myString := strconv.Itoa(int(myUint16))

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

Output

Converted uint16 to string: 42

That’s it!

Related posts

Golang interface to string

Golang time to string

Golang int to string

Golang bool to string

Golang int64 to string

Golang rune to string

Golang struct to string