×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang strconv.AppendUint() Function with Examples

Golang | strconv.AppendUint() Function: Here, we are going to learn about the AppendUint() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 06, 2021

strconv.AppendUint()

The AppendUint() function is an inbuilt function of the strconv package which is used to append the string form of the unsigned integer i (as generated by FormatUint() function), to dst and returns the extended buffer. Where dst is the first parameter of []byte type and i is the second parameter of uint64 type.

It accepts three parameters (dst, i, base) and returns the extended buffer.

Syntax

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

Parameters

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

Return Value

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

Example 1

// Golang program to demonstrate the
// example of strconv.AppendUint() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	x2 := []byte("Uint (base 2):")
	fmt.Println("Before AppendUint()")
	fmt.Println(string(x2))

	// Appending unsigned integer value
	x2 = strconv.AppendUint(x2, 108, 2)
	fmt.Println("After AppendUint()")
	fmt.Println(string(x2))

	fmt.Println("------------")

	x10 := []byte("Uint (base 10):")
	fmt.Println("Before AppendUint()")
	fmt.Println(string(x10))

	// Appending unsigned integer value
	x10 = strconv.AppendUint(x10, 108, 10)
	fmt.Println("After AppendUint()")
	fmt.Println(string(x10))

	fmt.Println("------------")

	x16 := []byte("Uint (base 16):")
	fmt.Println("Before AppendUint()")
	fmt.Println(string(x16))

	// Appending unsigned integer value
	x16 = strconv.AppendUint(x16, 108, 16)
	fmt.Println("After AppendUint()")
	fmt.Println(string(x16))
}

Output:

Before AppendUint()
Uint (base 2):
After AppendUint()
Uint (base 2):1101100
------------
Before AppendUint()
Uint (base 10):
After AppendUint()
Uint (base 10):108
------------
Before AppendUint()
Uint (base 16):
After AppendUint()
Uint (base 16):6c

Example 2

// Golang program to demonstrate the
// example of strconv.AppendUint() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	x2 := []byte("Uint (base 2):")
	fmt.Println("Before appending...")
	fmt.Println("x2:", string(x2))
	fmt.Println("Length(x2): ", len(x2))

	// Appending unsigned integer value
	x2 = strconv.AppendUint(x2, 108, 2)
	x2 = strconv.AppendUint(x2, 255, 2)

	fmt.Println("After appending...")
	fmt.Println("x2:", string(x2))
	fmt.Println("Length(x2): ", len(x2))

	fmt.Println("----------")

	x10 := []byte("Uint (base 10):")
	fmt.Println("Before appending...")
	fmt.Println("x10:", string(x10))
	fmt.Println("Length(x10): ", len(x10))

	// Appending unsigned integer value
	x10 = strconv.AppendUint(x10, 108, 10)
	x10 = strconv.AppendUint(x10, 255, 10)

	fmt.Println("After appending...")
	fmt.Println("x10:", string(x10))
	fmt.Println("Length(x10): ", len(x10))

	fmt.Println("----------")

	x16 := []byte("Uint (base 16):")
	fmt.Println("Before appending...")
	fmt.Println("x16:", string(x16))
	fmt.Println("Length(x16): ", len(x16))

	// Appending unsigned integer value
	x16 = strconv.AppendUint(x16, 108, 16)
	x16 = strconv.AppendUint(x16, 255, 16)

	fmt.Println("After appending...")
	fmt.Println("x16:", string(x16))
	fmt.Println("Length(x16): ", len(x16))
}

Output:

Before appending...
x2: Uint (base 2):
Length(x2):  14
After appending...
x2: Uint (base 2):110110011111111
Length(x2):  29
----------
Before appending...
x10: Uint (base 10):
Length(x10):  15
After appending...
x10: Uint (base 10):108255
Length(x10):  21
----------
Before appending...
x16: Uint (base 16):
Length(x16):  15
After appending...
x16: Uint (base 16):6cff
Length(x16):  19

Golang strconv Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.