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:
// Importing necessary packages.
import (
	"fmt"
	"os"
)
// Defining a custom type 'point' with two integer fields.
type point struct {
	x, y int
}
// The main function, where the execution of the program begins.
func main() {
	// Creating an instance of the 'point' struct.
	p := point{1, 2}
	// Printing the struct 'p' using different format specifiers.
	fmt.Printf("struct1: %v\n", p)    // Default format.
	fmt.Printf("struct2: %+v\n", p)   // Adds field names.
	fmt.Printf("struct3: %#v\n", p)   // Adds Go syntax representation.
	// Printing the type of 'p'.
	fmt.Printf("type: %T\n", p)
	// Printing a boolean value.
	fmt.Printf("bool: %t\n", true)
	// Printing an integer value.
	fmt.Printf("int: %d\n", 123)
	// Printing an integer in binary format.
	fmt.Printf("bin: %b\n", 14)
	// Printing a character using its ASCII value.
	fmt.Printf("char: %c\n", 33)
	// Printing an integer in hexadecimal format.
	fmt.Printf("hex: %x\n", 456)
	// Printing a floating-point number.
	fmt.Printf("float1: %f\n", 78.9)
	// Printing a floating-point number in scientific notation.
	fmt.Printf("float2: %e\n", 123400000.0)
	fmt.Printf("float3: %E\n", 123400000.0)
	// Printing a string.
	fmt.Printf("str1: %s\n", "\"string\"")
	// Printing a string with double quotes.
	fmt.Printf("str2: %q\n", "\"string\"")
	// Printing a string in hexadecimal format.
	fmt.Printf("str3: %x\n", "hex this")
	// Printing a pointer value.
	fmt.Printf("pointer: %p\n", &p)
	// Printing integers with specified width.
	fmt.Printf("width1: |%6d|%6d|\n", 12, 345)
	// Printing floating-point numbers with specified width and precision.
	fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)
	// Printing floating-point numbers with specified width and left alignment.
	fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
	// Printing strings with specified width.
	fmt.Printf("width4: |%6s|%6s|\n", "foo", "b")
	// Printing strings with specified width and left alignment.
	fmt.Printf("width5: |%-6s|%-6s|\n", "foo", "b")
	// Using Sprintf to format a string.
	s := fmt.Sprintf("sprintf: a %s", "string")
	fmt.Println(s)
	// Using Fprintf to format and print a string to os.Stderr.
	fmt.Fprintf(os.Stderr, "io: an %s\n", "error")
}output
struct1: {1 2}
struct2: {x:1 y:2}
struct3: main.point{x:1, y:2}
type: main.point
bool: true
int: 123
bin: 1110
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08
str1: "string"
str2: "\"string\""
str3: 6865782074686973
pointer: 0xc00000a0d0
width1: |    12|   345|
width2: |  1.20|  3.45|
width3: |1.20  |3.45  |
width4: |   foo|     b|
width5: |foo   |b     |
sprintf: a string
io: an errorExplanation:
Printing Structs:
struct1,struct2, andstruct3showcase different ways to print a struct (pointin this case) with various formatting options.
Printing Types:
%Tis used to print the type of a value.
Printing Booleans and Integers:
%tis used for boolean values.%dis used for decimal integers.
Printing Binary, Character, and Hexadecimal:
%bprints integers in binary format.%cprints a character using its ASCII value.%xprints integers in hexadecimal format.
Printing Floating-Point Numbers:
%fprints floating-point numbers.%eand%Eprint floating-point numbers in scientific notation.
Printing Strings:
%sprints strings.%qprints strings with double quotes.%xprints strings in hexadecimal format.
Printing Pointers:
%pis 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:
Sprintfis used to format a string without printing it to the console.
Printing to os.Stderr:
Fprintfis 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?