String Formatting
This Go code demonstrates the usage of the fmt
package for formatted printing and string formatting. Let's go through the code with inline comments and explanations:
output
Explanation:
Printing Structs:
struct1
,struct2
, andstruct3
showcase different ways to print a struct (point
in this case) with various formatting options.
Printing Types:
%T
is used to print the type of a value.
Printing Booleans and Integers:
%t
is used for boolean values.%d
is used for decimal integers.
Printing Binary, Character, and Hexadecimal:
%b
prints integers in binary format.%c
prints a character using its ASCII value.%x
prints integers in hexadecimal format.
Printing Floating-Point Numbers:
%f
prints floating-point numbers.%e
and%E
print floating-point numbers in scientific notation.
Printing Strings:
%s
prints strings.%q
prints strings with double quotes.%x
prints strings in hexadecimal format.
Printing Pointers:
%p
is used to print pointer values.
Printing with Width and Alignment:
Various examples demonstrate printing with specified width and alignment using
%6d
,%6.2f
,%-6.2f
,%6s
, and%-6s
.
String Formatting with Sprintf:
Sprintf
is used to format a string without printing it to the console.
Printing to os.Stderr:
Fprintf
is used to format and print a string toos.Stderr
.
This code provides a comprehensive overview of the formatting options available in the fmt
package for various types of values.
Last updated
Was this helpful?