将字符串转换为字符串

Convert string to string

请说。 为什么代码中的字符串“\xF0\x9F\x98\x81”与参数命令行中的“\xF0\x9F\x98\x81”不一样?

func main() {

    text1 := "\xF0\x9F\x98\x81"
    text2 := os.Args[1]
}

长度字符串 "text1" = 4,如果 len(text1),"text2" = 16。 我如何将 "text2" 转换为 "text1"?

确实有一个标准包和一个函数。 strconv.Unquote

Unquote interprets s as a single-quoted, double-quoted, or backquoted Go string literal, returning the string value that s quotes. (If s is single-quoted, it would be a Go character literal; Unquote returns the corresponding one-character string.)

请注意,您需要引用输入的stirng。在您的代码中,它应该是:

text2 := strconv.Unquote(`"`+os.Args[1]+`"`)

游乐场示例:https://play.golang.org/p/u2yU3VQHhXO