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
+17.1k Golang : When to use init() function?
+6.9k Golang : Calculate BMI and risk category
+9k Golang : Handle sub domain with Gin
+21.1k Golang : Get password from console input without echo or masked
+14.8k Golang : Basic authentication with .htpasswd file
+6.5k Golang : How to validate ISBN?
+6.8k Nginx : Password protect a directory/folder
+8.8k Golang : GMail API create and send draft with simple upload attachment example
+12k Golang : Detect user location with HTML5 geo-location
+6.3k CodeIgniter : form input set_value cause " to become & quot
+5.7k Get website traffic ranking with Similar Web or Alexa
+39.5k Golang : Remove dashes(or any character) from string