SoFunction
Updated on 2025-03-04

golang format input and output operations

Formatted strings are composed of placeholders and normal characters.

Placeholder starts with '%' and ends with verb.

Placeholders are composed of five types of elements: flags, width, precision, parameter index, and verbs.

Except for the last verb, all other elements can be omitted.

See the following example description of common types:

Default format and type

Value: []int64{0, 1}

Formatted effects verb describe
[0 1] %v Default format
[]int64{0, 1} %#v go syntax printing
[]int64 %T Type Print

Integer (indent, binary type, positive and negative symbol)

Value: 15

Formatted effects verb describe
15 %d Decimal
+15 %+d Positive and negative symbols must be displayed
␣␣15 %4d Pad space (width is 4, right-aligned)
15␣␣ %-4d Pad space (width is 4, left aligned)
0015 %04d Pad space (width is 4)
1111 %b Binary
17 %o Octal
f %x Hexadecimal, lowercase
F %X Hexadecimal, capital
0xf %#x Hexadecimal, with the digit 0x

Characters (with quotes, Unicode)

Value: 65 (Unicode letter A)

Formatted effects verb describe
A %c character
'A' %q Characters with quotes
U+0041 %U Unicode
U+0041 'A' %#U Unicode has quotes

Boolean(true/false)

Use %t to format a boolean type to true or false

Pointer (hex)

Use %p to format a pointer to hexadecimal display method

Floating point (indent, precision, scientific counting)

Value: 123.456

Formatted effects verb describe
1.234560e+02 %e Scientific Counting
123.456000 %f Decimal decimal
123.46 %.2f Default width, accuracy is 2
␣␣123.46 %8.2f Width is 8 and accuracy is 2
123.456 %g Select %e or %f according to the situation, and there is no 0 at the end

String or byte slice (quotation marks, indentation, hexadecimal)

Value: "café"

Formatted effects verb describe
café %s String output as is
␣␣café %6s Width is 6, right-aligned
café␣␣ %-6s Width is 6, left aligned
"café" %q Quoted strings
636166c3a9 %x Print each byte in hexadecimal form
63 61 66 c3 a9 % x Print each byte in hexadecimal form, with spaces between bytes

Supplement: Format symbol description in golang

The default format representation of the %v value

%+v is similar to %v, but the field name will be added when outputting the structure

Go syntax representation of %#v value

Go syntax representation of the type of %T value

%% percent sign

Boolean value:

%t word true or false

Integer:

%b is expressed as binary

%c The unicode code value corresponding to this value

%d is decimal

%o is expressed as octal

%q The literal value of the go syntax character enclosed in single quotes corresponding to this value will be safely escaped if necessary

%x is expressed in hexadecimal, using a-f

%X is expressed in hexadecimal, using A-F

%U is expressed in Unicode format: U+1234, equivalent to "U+%04X"

Two components of floating point numbers and complex numbers:

%b Scientific notation method without decimal parts and binary indexes, such as -123456p-78; see

%e Scientific notation method, such as -1234.456e+78

%E Scientific notation method, such as -1234.456E+78

%f has a decimal part but no exponential part, such as 123.456

%F is equivalent to %f

%g adopts %e or %f format according to actual conditions (for a more concise and accurate output)

%G adopts %E or %F format according to actual conditions (for a more concise and accurate output)

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.