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
+2.9k Golang : Convert lines of string into list for delete and insert operation
+11.6k Golang : How to check if input from os.Args is integer?
+10k Android Studio : Use image as AlertDialog title with custom layout example
+6.9k Android Studio : Simple input textbox and intercept key example
+6.7k Golang : Get UDP client IP address and differentiate clients by port number
+5.2k Golang : Handle Palindrome string with case sensitivity and unicode
+6.8k Golang : Accessing content anonymously with Tor
+23k Golang : Encrypt and decrypt data with AES crypto
+4.9k Golang : Handling Yes No Quit query input
+11.6k Golang : How to make a file read only and set it to writable again?
+4.7k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+13.9k Golang : Padding data for encryption and un-padding data for decryption