Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
Problem :
You want to print rune, unicode or CJK(Chinese/Japanese/Korean) characters with Golang, but you are getting funny result. How to print rune properly?
Solution :
Use quoted verb in your Printf statement. From https://blog.golang.org/strings :
"The %q (quoted) verb will escape any non-printable byte sequences in a string so the output is unambiguous."
For example :
package main
import (
"fmt"
)
func main() {
sr := '\u212A'
fmt.Println(sr) // wrong way to print!
fmt.Printf("%+q\n", sr) // print back the unicode
fmt.Printf("%q\n", sr) // print K (Kelvin symbol)
src := '你'
fmt.Printf("%q\n", src) // print character 你
fmt.Printf("%+q\n", src) // print unicode codepoint
}
Output :
8490
'\u212a'
'K'
'你'
'\u4f60'
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.9k Golang : Get number of CPU cores
+16.7k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+7.7k Golang : Ways to recover memory during run time.
+21k Golang : Create and resolve(read) symbolic links
+8.7k Android Studio : Image button and button example
+16.9k Golang : XML to JSON example
+15.1k Golang : Get timezone offset from date or timestamp
+22.7k Golang : simulate tail -f or read last line from log file example
+15.5k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+9.5k Javascript : Read/parse JSON data from HTTP response
+5.8k Golang : Generate multiplication table from an integer example