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
+35.1k Golang : Remove dashes(or any character) from string
+9.4k Golang : Display list of countries and ISO codes
+12k Golang : Get URI segments by number and assign as variable example
+22.7k PHP : Convert(cast) string to bigInt
+6.2k Findstr command the Grep equivalent for Windows
+11.8k Golang : Submit web forms without browser by http.PostForm example
+3.2k JavaScript: Add marker function on Google Map
+26.3k Golang : JQuery AJAX post data to server and send data back to client example
+4.5k Golang : Extract XML attribute data with attr field tag example
+11.3k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+21.9k Golang : GORM read from database example