Open In App

strconv.AppendQuoteRune() Function in Golang With Examples

Last Updated : 21 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Go language provides inbuilt support to implement conversions to and from string representations of basic data types with the help of strconv Package. This package provides an AppendQuoteRune() function which appends a single-quoted Go character literal representing the rune x, as generated by QuoteRune, to num and returns the extended buffer as shown in below syntax. To access the AppendQuoteRune() function you need to import strconv Package in your program. Syntax:
func AppendQuoteRune(num []byte, x rune) []byte
Example 1: C
// Golang program to illustrate
// strconv.AppendQuoteRune() Function
package main

import (
    "fmt"
    "strconv"
)

func main() {

    // Using AppendQuoteRune() function
    val1 := []byte("Rune 1: ")
    val1 = strconv.AppendQuoteRune(val1, 'B')
    fmt.Println(string(val1))

    val2 := []byte("Rune 2: ")
    val2 = strconv.AppendQuoteRune(val2, '\a')
    fmt.Println(string(val2))

}
Output:
Rune 1: 'B'
Rune 2: '\a'
Example 2:
// Golang program to illustrate
// strconv.AppendQuoteRune() Function

package main

import (
    "fmt"
    "strconv"
)

func main() {

    // Using AppendQuoteRune() function
    val1 := []byte("Rune 1: ")
    val1 = strconv.AppendQuoteRune(val1, 'c')
    fmt.Println(string(val1))
    fmt.Println("Length: ", len(val1))
    fmt.Println("Capacity: ", cap(val1))

    val2 := []byte("Rune 2: ")
    val2 = strconv.AppendQuoteRune(val2, '💣')
    fmt.Println(string(val2))
    fmt.Println("Length: ", len(val2))
    fmt.Println("Capacity: ", cap(val2))

}
Output:
Rune 1: 'c'
Length:  11
Capacity:  16
Rune 2: '💣'
Length:  14
Capacity:  16

Article Tags :

Similar Reads