How to Use strconv.AppendUint() Function in Golang

Golang strconv.AppendUint() function is used to append the string form of the unsigned integer i, as generated by FormatUint, to dst and returns the extended buffer.

Syntax

func AppendUint(dst []byte, i uint64, base int) []byte

Parameters

  1. dst: A byte array (or byte slices) in which we must append the string form of the unsigned integer.
  2. i: An integer value to be appended.
  3. base: Base of the integer value.

Return Value

The return type of the AppendUint() function is a []byte, and it returns the extended buffer after appending the string form of the unsigned integer.

Example 1: Usage of strconv.AppendUint()

package main

import (
  "fmt"
  "strconv"
)

func main() {
  // A slice of bytes
  s := []byte("uint number: ")

  // An unsigned integer
  num := uint64(12345)

  // Convert and append the number to the byte slice
  s = strconv.AppendUint(s, num, 10)

  // Convert the byte slice back to a string and print it
  fmt.Println(string(s))
}

Output

uint number: 12345

Example 2

package main

import (
  "fmt"
  "strconv"
)

func main() {
  val1 := []byte("uint value (base 8): ")
  val1 = strconv.AppendUint(val1, 123, 8)
  fmt.Println(string(val1))
  fmt.Println("Length: ", len(val1))
  fmt.Println("Capacity: ", cap(val1))

  val2 := []byte("uint value (base 2): ")
  val2 = strconv.AppendUint(val2, 78, 2)
  fmt.Println(string(val2))
  fmt.Println("Length: ", len(val2))
  fmt.Println("Capacity: ", cap(val2))
}

Output

uint value (base 8): 173
Length: 24
Capacity: 48
uint value (base 2): 1001110
Length: 28
Capacity: 48

That’s it.

Similar posts

strconv.AppendFloat() Function

strconv.AppendInt() Function

strconv.AppendBool() Function