Appendix B. Formatting cheat sheet

 

Go offers several verbs that are passed to printing functions to format Go values. In this appendix, we present the most known verbs and special values that can be passed to these functions. You can refer to these tables all along the book. The result for each of the following entries was generated by fmt.Printf("{{verb}}", value).

Table B.1. Default

Verb

Output for fmt.Printf("{{Verb}}", []int64{0, 1})

Description

%v

[0 1]

Default format

%#v

[]int64{0, 1}

Go-syntax format

%T

[]int64

Type of the value

Table B.2. Integers

Verb

Output for fmt.Printf("{{Verb}}", 15)

Description

%d

15

Base 10

%+d

+15

Always show the sign

%4d

␣␣15

Pad to 4 characters with spaces, right justified

%-4d

15␣␣

Pad to 4 characters with spaces, left justified

%04d

0015

Pad to 4 characters with prefixing zeros

%b

1111

Base 2 (binary)

%o

17

Base 8 (octal)

%x

f

Base 16, lowercase

%X

F

Base 16, uppercase

%#x

0xf

Base 16 with leading 0x

sitemap