fmt.Printf()

When using Printf to output text to the console, we can use a special placeholder to output variables. This makes it easier to format text we want to print to the console:

age := 20
fmt.Printf("Your age is %v", age)

Below a table of all formats:

Conversion Characters

Description

%b

It is used to format base 2 numbers

%o

It is used to format base 8 numbers

%O

It is used to format base 8 numbers with 0o prefix

%d

It is used to format base 10 numbers with lower-case letters for a-f (integers)%

%x

It is used to format base 16 numbers with upper-case letters for A-F

%X

It is used to format base 16 numbers

%g

It is used to format float values

%s

It is used to format string values

%t

It is used to format true or false values

%e

It is used to format scientific values

%T

Type of the value

%f

Decimal number (floats)

%.2f

How many decimal places

%.0f

No decimal places

Last updated