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
+6.1k Golang : How to determine if request or crawl is from Google robots
+19.2k Golang : How to run your code only once with sync.Once object
+8.7k Golang : Generate EAN barcode
+17.9k Golang : Display list of time zones with GMT
+6.4k Fix sudo yum hang problem with no output or error messages
+7.4k Golang : Get today's weekday name and calculate target day distance example
+6.9k Golang : alternative to os.Exit() function
+26.6k Golang : dial tcp: too many colons in address
+24k Golang : Change file read or write permission example
+6k Golang : Join lines with certain suffix symbol example
+12.1k Swift : Convert (cast) Int or int32 value to CGFloat
+13.6k Android Studio : Use image as AlertDialog title with custom layout example