Golang strconv.AppendInt() Function with Examples

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

strconv.AppendInt()

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

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

Syntax:

func AppendInt(dst []byte, i int64, base int) []byte

Parameter(s):

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

Return Value:

The return type of AppendInt() function is a []byte, it returns the extended buffer after appending the given integer value.

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

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

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

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

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

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

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

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

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

Output:

Before AppendInt()
Int (base 2):
After AppendInt()
Int (base 2):1101100
------------
Before AppendInt()
Int (base 10):
After AppendInt()
Int (base 10):108
------------
Before AppendInt()
Int (base 16):
After AppendInt()
Int (base 16):6c

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

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

	// Appending an integer value
	x2 = strconv.AppendInt(x2, 108, 2)
	x2 = strconv.AppendInt(x2, 255, 2)

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

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

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

	// Appending an integer value
	x10 = strconv.AppendInt(x10, 108, 10)
	x10 = strconv.AppendInt(x10, 255, 10)

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

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

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

	// Appending an integer value
	x16 = strconv.AppendInt(x16, 108, 16)
	x16 = strconv.AppendInt(x16, 255, 16)

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

Output:

Before appending...
x2: Int (base 2):
Length(x2):  13
After appending...
x2: Int (base 2):110110011111111
Length(x2):  28
----------
Before appending...
x10: Int (base 10):
Length(x10):  14
After appending...
x10: Int (base 10):108255
Length(x10):  20
----------
Before appending...
x16: Int (base 16):
Length(x16):  14
After appending...
x16: Int (base 16):6cff
Length(x16):  18

Golang strconv Package »





Comments and Discussions!

Load comments ↻






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