Open In App

How to use strconv.QuoteToGraphic() Function in Golang?

Last Updated : 05 May, 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 a QuoteToGraphic() function which is used to find a double-quoted Go string literal representing str and the returned string leaves Unicode graphic characters defined by the IsGraphic, unchanged and uses Go escape sequences (\t, \n, \xFF, \u0100) for non-graphic characters. To access QuoteToGraphic() function you need to import strconv Package in your program with the help of import keyword.

Syntax:

func QuoteToGraphic(str string) string

Parameter: This function takes one parameter of string type, i.e., str.

Return value: This function returns a double-quoted Go string literal which represents str.

Let us discuss this concept with the help of the given examples:

Example 1:

// Golang program to illustrate 
// strconv.QuoteToGraphic() Function
package main

import (
    "fmt"
    "strconv"
)

func main() {

    // Finding a double-quoted Go 
    // string literal representing str
    // Using func QuoteToGraphic() function
    str := strconv.QuoteToGraphic("\u2665 Welcome GeeksforGeeks \u2665")
    fmt.Println (str)
    
}

Output:

"♥ Welcome GeeksforGeeks ♥"

Example 2:

// Golang program to illustrate
// strconv.QuoteToGraphic() Function
package main
 
import (
    "fmt"
    "strconv"
)
 
func main() {

    // Finding a double-quoted Go 
    // string literal representing rune
    // Using QuoteToGraphic() function
    val1 := strconv.QuoteToGraphic(`"I like Δ    "`)
    fmt.Println("Result 1: ", val1)
    fmt.Println("Length 1: ", len(val1))
   
    val2 := strconv.QuoteToGraphic("I love \u2666")
    fmt.Println("Result 2: ", val2)
    fmt.Println("Length 2: ", len(val2))
}

Output:

Result 1:  "\"I like Δ\t\""
Length 1:  17
Result 2:  "I love ♦"
Length 2:  12

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads