给出以下示例:
\n\nfunc main() {\n buf := new(bytes.Buffer)\n enc := json.NewEncoder(buf)\n toEncode := []string{"hello", "w\xc3\xb6rld"}\n enc.Encode(toEncode)\n fmt.Println(buf.String())\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我希望输出带有转义的 Unicode 字符:
\n\n\n\n\n[“你好”,“w\\u00f6rld”]
\n
而不是:
\n\n\n\n\n[“你好”,“w\xc3\xb6rld”]
\n
我试图编写一个函数来引用 Unicode 字符,strconv.QuoteToASCII并将结果提供给Encode()然而,这会导致双重转义:
func quotedUnicode(data []string) []string {\n for index, element := range data { \n quotedUnicode := strconv.QuoteToASCII(element) \n // get rid of additional quotes \n quotedUnicode = strings.TrimSuffix(quotedUnicode, "\\"") \n quotedUnicode = strings.TrimPrefix(quotedUnicode, "\\"") \n data[index] = quotedUnicode \n } \n …Run Code Online (Sandbox Code Playgroud)