Open In App

strconv.AppendInt() Function in Golang With Examples

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides an AppendInt() function which is used to append the string form of the integer val, as generated by FormatInt, to num and returns the extended buffer as shown in the below syntax. To access AppendInt() function you need to import strconv Package in your program.

Syntax:

func AppendInt(num []byte, val int64, base int) []byte

Example 1:




// Golang program to illustrate
// strconv.AppendInt() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Using AppendInt() function
    val1 := []byte("int value(base 10): ")
    val1 = strconv.AppendInt(val1, -35, 10)
    fmt.Println(string(val1))
  
    val2 := []byte("int value(base 16): ")
    val2 = strconv.AppendInt(val2, -44, 16)
    fmt.Println(string(val2))
  
}


Output:

int value(base 10): -35
int value(base 16): -2c

Example 2:




// Golang program to illustrate
// strconv.AppendInt() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Using AppendInt() function
    val1 := []byte("int value(base 10): ")
    val1 = strconv.AppendInt(val1, 45, 10)
      
    fmt.Println(string(val1))
    fmt.Println("Length: ", len(val1))
    fmt.Println("Capacity: ", cap(val1))
  
    val2 := []byte("int value(base 16): ")
    val2 = strconv.AppendInt(val2, 42, 16)
      
    fmt.Println(string(val2))
    fmt.Println("Length: ", len(val2))
    fmt.Println("Capacity: ", cap(val2))
  
}


Output:

int value(base 10): 45
Length:  22
Capacity:  48
int value(base 16): 2a
Length:  22
Capacity:  48


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads