golang escapes single quotes, double quotes and back quotes in string variables

package main

import (
   "strconv"
   "fmt"
)


func main () {
    
    
    var a string
    a = "qwe'wer',`f.lopg`  "uiii""  // 随便写的例子,因为字符串变量中的单双引号是我们不能提前知道的
    b := strconv.Quote(a)
    fmt.println(a)
}

Like this unknown string variable, there are single quotation marks, double quotation marks and even return single quotation marks. When using variables, the single and double quotation marks in the variable will affect the single and double quotation marks outside the variable and need to be modified. When the single and double quotation marks in the variable are valid, then you can use the strconv package to achieve

After running, the printed result is qwe'wer', `f.lopg` "uiii", and the escape character \ is added in front of the single and double quotation marks.

Guess you like

Origin blog.csdn.net/weixin_43202081/article/details/109742963