How to use the strconv.AppendUint() function in Golang
Overview
The strconv package’s AppendUint() function appends the string form of the unsigned integer i (as generated by FormatUint() function) to dst. It returns the extended buffer.
Syntax
func AppendUint(dst []byte, i uint64, base int) []byte
Parameters
The AppendUint() function takes three parameters:
dst: This is a byte array to append the string form of the unsigned integer.i: This is the integer value to be appended.base: This is the base of the given integer value.
Return value
The function AppendUint() returns the extended buffer after the string form of the unsigned integer i is appended.
Example
The following code shows how to implement strconv.AppendUint() in Golang:
// Using the strconv.AppendUint() functionpackage mainimport ("fmt""strconv")func main() {// Declaring and assigning a valuex := []byte("sample code")fmt.Println("The initial value before AppendUint(): ", string(x))// Appending the integer value in base 10x = strconv.AppendUint(x, 148, 10)fmt.Println()fmt.Println("The new value after AppendUint(): ",string(x))fmt.Println()// Appending the integer value in base 2x = strconv.AppendUint(x, 105, 2)fmt.Println()fmt.Println("The new values in the array: ",string( x))}
Explanation
- Line 4: We add the
mainpackage. - Lines 6–9: We import the necessary packages.
- Line 11–31: We define the
main()function and variablesxandyof the byte array type. We then assign it an integer value and a base. Then, we pass the variable to theAppendUint()function, which appends the string form of the specified unsigned integer value. Finally, the result is displayed.