Golang: Print String

By Xah Lee. Date: . Last updated: .

Print One Byte in String

When you use a string index, example: "abc"[0], the value is a byte.

You may print it as hexadecimal, or integer (unicode codepoint), or as a char.

fmt.Printf("%#v\n", x[0])
Print as hexadecimal. Sample output: 0x61
fmt.Printf("%d\n", x[0])
Print as integer. Sample output: 97
fmt.Printf("%q\n", x[0])
Print as raw byte. Sample output: 'a'. May be a problem if the byte is a control character or non-printable 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 non-printable Horizontal Tab (codepoint 9) followed by a emoji.

you may:

The fmt.Printf function has several placeholders to help.

%s
Output raw bytes as is. If string contain non-printable character 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. (example: "ā™„\tšŸ˜‚")
%+q
Output in golang string syntax, using backslash escape for anything that's not printable ASCII. (example: "\u2665\t\U0001f602")
% x
Hexadecimal, with space between each 2 digits. (example: e2 99 a5 09 f0 9f 98 82) This is best if you want to know the byte values.

[see ASCII Table]

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, example: U+2665 U+0009 U+1F602, first convert the string into Rune Slice , then print it with %U

Example:
fmt.Printf("%U\n", []rune(x))

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]
}

Golang

Examples

Reference