Converting Unicode Code Points to Literal Characters in Go
In Go, when reading a text file containing Unicode code points using ioutil.ReadFile(), the resulting data retains the code points instead of the intended characters. To convert these code points to literal characters, we can utilize the functionality provided by the strconv package:
package main import ( "fmt" "strconv" "strings" ) func main() { lines := []string{ `\u0053`, `\u0075`, `\u006E`, } // Manually append quotes to allow for strconv.Unquote() usage for i, v := range lines { lines[i] = `"` + v + `"` } // Unquote the values and remove quotes for i, v := range lines { var err error lines[i], err = strconv.Unquote(v) if err != nil { fmt.Println(err) } } fmt.Println(lines) }
Here's what the code achieves:
This approach effectively substitutes the Unicode code points with their corresponding literal characters, as desired in the original question.
The above is the detailed content of How Can I Convert Unicode Code Points to Literal Characters in Go?. For more information, please follow other related articles on the PHP Chinese website!