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

Last Updated : 5 May, 2020
Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a Quote() function which is used to find a double-quoted Go string literal representing str and the returned string uses Go escape sequences (\t, \n, \xFF, \u0100) to control characters and non-printable characters defined by IsPrint. To access Quote() function you need to import strconv Package in your program. Syntax:
func Quote(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. Example 1: C
// Golang program to illustrate strconv.Quote() Function
package main

import (
    "fmt"
    "strconv"
)

func main() {
    // Finding a double-quoted Go
    // string literal representing str
    // Using Quote() function
    str := strconv.Quote(`" Hello 
     Welcome to GeeksforGeeks "`)
    fmt.Println(str)

}
Output:
"\" Hello \n     Welcome to GeeksforGeeks \""
Example 2: C
// Golang program to illustrate
// strconv.Quote() Function
package main

import (
    "fmt"
    "strconv"
)

func main() {

    // Finding a double-quoted Go string literal
    // Using Quote() function
    val1 := strconv.Quote(`"Hello! GFG   "`)
    fmt.Println("Result 1: ", val1)
    fmt.Println("Length 1: ", len(val1))

    val2 := strconv.Quote(`"Welcome!
        GeeksforGeeks"`)
    fmt.Println("Result 2: ", val2)
    fmt.Println("Length 2: ", len(val2))

}
Output:
Result 1:  "\"Hello! GFG   \""
Length 1:  19
Result 2:  "\"Welcome!\n        GeeksforGeeks\""
Length 2:  37
Comment

Explore