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

Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a CanBackquote() function which is used to check whether the string str can be represented unchanged as a single-line backquoted string without control characters other than a tab or not. To access CanBackquote() function you need to import strconv Package in your program with the help of import keyword.

Syntax:

func CanBackquote(str string) bool

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

Return value: This function returns true if the string str can be represented unchanged as a single-line backquoted string, otherwise return false.

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

Example 1:




// Golang program to illustrate
// strconv.CanBackquote() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
    // Checking whether the given
    // string can be represented
    // unchanged as a single-line 
    // backquoted string
    // Using CanBackquote() function
    fmt.Println(strconv.CanBackquote("Welcome to w3wiki"))
    fmt.Println(strconv.CanBackquote("`Welcome to w3wiki`"))
    fmt.Println(strconv.CanBackquote(`"Welcome to w3wiki"`))
  
}


Output:

true
false
true

Example 2:




// Golang program to illustrate
// strconv.CanBackquote() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
// Main function
func main() {
  
    // Checking whether the given
    // string can be represented
    // unchanged as a single-line
    // backquoted string
    // Using CanBackquote() function
    res := strconv.CanBackquote("Welcome to w3wiki")
    if res == true {
        fmt.Println("The given string is unchanged "+
                     "single-line backquoted string.")
    } else {
        fmt.Println("The given string is a "+
         "changeable single-line backquoted string.")
    }
  
}


Output:

The given string is unchanged single-line backquoted string.


Contact Us