Go Formatting Verbs

Go Tutorial

 Go Formatting Verbs – Complete Guide with Examples

In Go (Golang), formatting verbs are special placeholders used with fmt.Printf, fmt.Sprintf, and related functions to control how values are displayed.

They allow you to print data in a clean, readable, and formatted way, which is essential for debugging, logging, and user output.


What Are Formatting Verbs in Go?

Formatting verbs are symbols that start with % and tell Go how to format a value.

Example


 

  • %s → string
  • %d → integer

 Why Formatting Verbs Are Important?

Formatting verbs help you:

  •  Control output appearance
  •  Print different data types correctly
  •  Debug values and types
  •  Create readable logs
  •  Format numbers and text

 Commonly Used Formatting Verbs

 General Verbs

VerbDescription
%vDefault value
%+vStruct with field names
%#vGo-syntax representation
%TType of the value
%%Prints %

Example


 Integer Formatting Verb

VerbMeaning
%dDecimal
%bBinary
%oOctal
%xHexadecimal (lowercase)
%XHexadecimal (uppercase)

Example


 Floating-Point Formatting Verb

VerbMeaning
%fDecimal notation
%.2f2 decimal places
%eScientific notation
%gCompact format

Example

  •  Controls decimal precision

 String Formatting Verb

VerbMeaning
%sString
%qQuoted string
%xHex dump of string

Example


 Boolean Formatting Verb

VerbMeaning
%tBoolean value

Example


 Pointer Formatting Verb

VerbMeaning
%pMemory address

Example


 Struct Formatting Verb


 

VerbOutput
%v{Amit 25}
%+v{Name:Amit Age:25}
%#vmain.User{Name:"Amit", Age:25}

 Width and Alignment

Width

Left Alignment

  •  Useful for tables

 Zero Padding

Output: 00025


Formatting Multiple Values


 


Printf vs Sprintf vs Fprintf

FunctionPurpose
PrintfPrints to console
SprintfReturns formatted string
FprintfWrites to writer (file, buffer)

Example


 Common Mistakes

  •  Using wrong verb for type
  •  Forgetting \n in Printf 
  •  Mismatch between verb and value
  •  Overusing %v everywhere

 Best Practices

  •  Use specific verbs (%d, %s)
  •  Use %v for quick debugging
  •  Control precision for floats
  •  Use %+v for structs
  •  Keep output readable

 Interview Questions: Go Formatting Verb

1. What does %v do?
Prints the default value.

2. How to print type of variable?
Use %T.

3. Which verb prints struct field names?
%+v.

4. How to print float with 2 decimals?
%.2f.

5. How to print memory address?
%p.


 Summary

  • Formatting verb control output
  • Used with fmt.Printf family
  •  Different verb for different types
  •  Precision and alignment supported
  •  Essential for debugging & logging

Mastering Go formatting verb will make your output clean, professional, and interview-ready

You may also like...