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 error

Explanation:

  1. Printing Structs:

    • struct1, struct2, and struct3 showcase different ways to print a struct (point in this case) with various formatting options.

  2. Printing Types:

    • %T is used to print the type of a value.

  3. Printing Booleans and Integers:

    • %t is used for boolean values.

    • %d is used for decimal integers.

  4. 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.

  5. Printing Floating-Point Numbers:

    • %f prints floating-point numbers.

    • %e and %E print floating-point numbers in scientific notation.

  6. Printing Strings:

    • %s prints strings.

    • %q prints strings with double quotes.

    • %x prints strings in hexadecimal format.

  7. Printing Pointers:

    • %p is used to print pointer values.

  8. Printing with Width and Alignment:

    • Various examples demonstrate printing with specified width and alignment using %6d, %6.2f, %-6.2f, %6s, and %-6s.

  9. String Formatting with Sprintf:

    • Sprintf is used to format a string without printing it to the console.

  10. Printing to os.Stderr:

    • Fprintf is used to format and print a string to os.Stderr.

This code provides a comprehensive overview of the formatting options available in the fmt package for various types of values.

Last updated