appendix BFormatting 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 throughout the book. The results for each of the following entries were 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.3 Floats

Verb

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

Description

%e

1.234560e+02

Scientific notation

%f

123.456000

Decimal point, no exponent. The default precision is 6.

%.2f

123.46

Default width, precision 2 digits after the decimal point

%8.2f

␣␣123.46

Width 8 chars, precision 2 digits after the decimal point. Default padding character is space.

%08.2f

00123.46

Width 8 chars, precision 2 digits after the decimal point. Left padding with specified character (here, 0).

%g

123.456

Exponent when needed, necessary digits only