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
+4.2k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+20.1k Golang : Check if os.Stdin input data is piped or from terminal
+11.8k Golang : Convert decimal number(integer) to IPv4 address
+13.8k Golang : Get current time
+15.6k Golang : How to login and logout with JWT example
+20k Swift : Convert (cast) Int to int32 or Uint32
+8.3k Golang : How to check variable or object type during runtime?
+7.6k Golang : How to execute code at certain day, hour and minute?
+7.4k Golang : Detect sample rate, channels or latency with PortAudio
+14.4k Golang : Overwrite previous output with count down timer
+7.8k Golang : Gomobile init produce "iphoneos" cannot be located error
+9.1k Golang : Write multiple lines or divide string into multiple lines