bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Go/Go Tutorial
Go•Go Tutorial

Go Formatting Verbs

Formatting Verbs for Printf()

Go offers several formatting verbs that can be used with the Printf() function.

General Formatting Verbs

The following verbs can be used with all data types:

VerbDescription
%vPrints the value in the default format
%#vPrints the value in Go-syntax format
%TPrints the type of the value
%%Prints the % sign

Example

package main
import ("fmt")
func main() {
  var i = 15.5
  var txt = "Hello World!"
  fmt.Printf("%v\n", i)
  fmt.Printf("%#v\n", i)
  fmt.Printf("%v%%\n", i)
  fmt.Printf("%T\n", i)
  fmt.Printf("%v\n", txt)
  fmt.Printf("%#v\n", txt)
  fmt.Printf("%T\n", txt)
}

Integer Formatting Verbs

The following verbs can be used with the integer data type:

VerbDescription
%bBase 2
%dBase 10
%+dBase 10 and always show sign
%oBase 8
%OBase 8, with leading 0o
%xBase 16, lowercase
%XBase 16, uppercase
%#xBase 16, with leading 0x
%4dPad with spaces (width 4, right justified)
%-4dPad with spaces (width 4, left justified)
%04dPad with zeroes (width 4

Example

package main
import ("fmt")
func main() {
  var i = 15
  fmt.Printf("%b\n", i)
  fmt.Printf("%d\n", i)
  fmt.Printf("%+d\n", i)
  fmt.Printf("%o\n", i)
  fmt.Printf("%O\n", i)
  fmt.Printf("%x\n", i)
  fmt.Printf("%X\n", i)
  fmt.Printf("%#x\n", i)
  fmt.Printf("%4d\n", i)
  fmt.Printf("%-4d\n", i)
  fmt.Printf("%04d\n", i)
}

String Formatting Verbs

The following verbs can be used with the string data type:

VerbDescription
%sPrints the value as plain string
%qPrints the value as a double-quoted string
%8sPrints the value as plain string (width 8, right justified)
%-8sPrints the value as plain string (width 8, left justified)
%xPrints the value as hex dump of byte values
% xPrints the value as hex dump with spaces

Example

package main
import ("fmt")
func main() {
  var txt = "Hello"
  fmt.Printf("%s\n", txt)
  fmt.Printf("%q\n", txt)
  fmt.Printf("%8s\n", txt)
  fmt.Printf("%-8s\n", txt)
  fmt.Printf("%x\n", txt)
  fmt.Printf("% x\n", txt)
}

Boolean Formatting Verbs

The following verb can be used with the boolean data type:

VerbDescription
%tValue of the boolean operator in true or false format (same as using %v)

Example

package main
import ("fmt")
func main() {
  var i = true
  var j = false
  fmt.Printf("%t\n", i)
  fmt.Printf("%t\n", j)
}

Float Formatting Verbs

The following verbs can be used with the float data type:

VerbDescription
%eScientific notation with 'e' as exponent
%fDecimal point, no exponent
%.2fDefault width, precision 2
%6.2fWidth 6, precision 2
%gExponent as needed, only necessary digits

Example

package main
import ("fmt")
func main() {
  var i = 3.141
  fmt.Printf("%e\n", i)
  fmt.Printf("%f\n", i)
  fmt.Printf("%.2f\n", i)
  fmt.Printf("%6.2f\n", i)
  fmt.Printf("%g\n", i)
}

Previous

Go Output Functions

Next

Go Data Types

This chapter

Overview
39

Lessons

95m

Read time

1. Go Tutorial2. Go Introduction3. Go Getting Started4. Go Syntax5. Go Comments6. Go Variables7. Go Multiple Variable Declaration8. Go Variable Naming Rules9. Go Constants10. Go Output Functions11. Go Formatting Verbs12. Go Data Types13. Go Boolean Data Type14. Go Integer Data Types15. Go Float Data Types16. Go String Data Type17. Go Arrays18. Go Slices19. Go Access, Change, Append and Copy Slices20. Go Operators21. Go Arithmetic Operators22. Go Assignment Operators23. Go Comparison Operators24. Go Logical Operators25. Go Bitwise Operators26. Go Conditions27. Go if statement28. Go if else Statement29. Go else if Statement30. Go Nested if Statement31. Go switch Statement32. Go Multi-case switch Statement33. Go For Loops34. Go Functions35. Go Function Parameters and Arguments36. Go Function Returns37. Go Recursion Functions38. Go Struct39. Go Maps

On this page

Formatting Verbs for Printf()General Formatting VerbsInteger Formatting VerbsString Formatting VerbsBoolean Formatting VerbsFloat Formatting Verbs