如何使用 strconv。Golang 中的 CanBackquote()函数?

原文:https://www . geesforgeks . org/how-用法-strconv-canbackquote-function-in-golang/

Go 语言通过 strconv Package 提供内置的支持来实现基本数据类型的字符串表示之间的转换。这个包提供了一个 CanBackquote()函数,用于检查字符串是否可以不变地表示为一个没有制表符以外的控制字符的单行反引号字符串。要访问 CanBackquote()函数,您需要借助 import 关键字在程序中导入 strconv Package。

语法:

func CanBackquote(str string) bool

参数:该函数取一个字符串类型的参数,即 str。

返回值:如果字符串可以不变地表示为单行反引号字符串,则该函数返回 true,否则返回 false。

让我们借助给定的例子来讨论这个概念:

例 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 GeeksforGeeks"))
    fmt.Println(strconv.CanBackquote("`Welcome to GeeksforGeeks`"))
    fmt.Println(strconv.CanBackquote(`"Welcome to GeeksforGeeks"`))

}

输出:

true
false
true

例 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 GeeksforGeeks")
    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.")
    }

}

输出:

The given string is unchanged single-line backquoted string.