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
+12.6k Android Studio : Highlight ImageButton when pressed on example
+32.6k Golang : Regular Expression for alphanumeric and underscore
+8.6k Golang : Set or add headers for many or different handlers
+9.3k Golang : Terminate-stay-resident or daemonize your program?
+25.2k Golang : Get current file path of a file or executable
+9.7k Golang : Qt get screen resolution and display on center example
+22.4k Golang : Convert Unix timestamp to UTC timestamp
+8.1k Golang : Qt splash screen with delay example
+27.4k Golang : dial tcp: too many colons in address
+9.6k Golang : Sort and reverse sort a slice of floats
+4.8k Javascript : How to get width and height of a div?
+25.6k Golang : How to write CSV data to file