Golang: Print String as Sequence of Byte, Char, Codepoint
Print One Byte in String
When you use a string index, e.g. "abc"[0]
, the value is a
Byte.
You may print it as Hexadecimal , or integer (Unicode Codepoint), or as a char.
fmt.Printf("%#v", x[0])
-
Print as hexadecimal. Sample output:
0x61
fmt.Printf("%d", x[0])
-
Print as integer. Sample output:
97
fmt.Printf("%q", x[0])
-
Print as raw byte. Sample output:
'a'
.(May display as gibberish if the byte is a control character or non-printable ASCII Character.)
package main import "fmt" // ways to print 1 byte in string func main() { var x = "abc" fmt.Printf("%#v\n", x[0]) // 0x61 fmt.Printf("%d\n", x[0]) // 97 fmt.Printf("%q\n", x[0]) // 'a' }
Note, a index in string may not be a valid character.
package main import "fmt" // a index in string may not be a valid character func main() { // utf8 encoding for FACE WITH TEARS OF JOY char is 4 bytes: F0 9F 98 82 var y = "😂" // print 1st byte fmt.Printf("%#v\n", y[0]) // 0xf0 fmt.Printf("%d\n", y[0]) // 240 fmt.Printf("%q\n", y[0]) // 'ð' }
Print Whole String
Because string is Byte sequence, you may want to print them in different ways.
Say you have a string like this:
"♥\t😂"
,
unicode heart followed by invisible
Horizontal Tab (codepoint 9) followed by a emoji.
you may:
- Print string as text editor would display it.
- Print some char using escape sequence such as
\t
for tab. - Print some char using unicode escape sequence such as
\u2665
. - Print string as hexadecimal. (when you need to see the bytes).
The fmt.Printf
function has several placeholders to help.
%s
-
Output raw bytes as is.
🛑 WARNING: If string contain ASCII Control Characters or byte sequence that is not a valid character, this can cause problem in the output terminal.
%q
-
Output in golang string syntax, using backslash escape sequence for non-printable characters. (e.g.
"♥\t😂"
) %+q
-
Output in golang string syntax, using backslash escape for anything that's not printable ASCII. (e.g.
"\u2665\t\U0001f602"
) % x
-
Hexadecimal, with space between each 2 digits. (e.g.
e2 99 a5 09 f0 9f 98 82
) This is best if you want to know the byte values.
〔see ASCII Characters〕
package main import "fmt" func main() { var x = "♥\t😂" // with a tab (U+0009) in middle fmt.Printf("%s\n", x) // ♥ 😂 fmt.Printf("%q\n", x) // "♥\t😂" fmt.Printf("%+q\n", x) // "\u2665\t\U0001f602" fmt.Printf("% x\n", x) // e2 99 a5 09 f0 9f 98 82 }
Print String in Unicode Notation
To print string in unicode notation, e.g. U+2665
, first convert the string into Rune
Slice
, then print it with %U
package main import "fmt" func main() { // with a tab (U+0009) in middle var x = "♥\t😂" // turn the string into rune slice, then print it with %U fmt.Printf("%U\n", []rune(x)) // [U+2665 U+0009 U+1F602] }