Go Formatting Verbs

Gayathri. B

Formatting Verbs for Printf()

  • Go provides a variety of formatting verbs that can be used with the Printf() function to format and display output in a structured way. These verbs control how different types of data are printed.

General Formatting Verbs in Go

  • These formatting verbs can be used with any data type in Go:

Example:

package main

import "fmt"

func main() {

  number := 15.5

  message := "Hello World!"

  fmt.Printf("Default format: %v\n", number)

  fmt.Printf("Go-syntax format: %#v\n", number)

  fmt.Printf("With percentage: %v%%\n", number)

  fmt.Printf("Type of number: %T\n", number)

  fmt.Printf("Default format: %v\n", message)

  fmt.Printf("Go-syntax format: %#v\n", message)

  fmt.Printf("Type of message: %T\n", message)

}

Output:

Default format: 15.5

Go-syntax format: 15.5

With percentage: 15.5%

Type of number: float64

Default format: Hello World!

Go-syntax format: "Hello World!"

Type of message: string

Integer Formatting Verbs in Go

  • The following formatting verbs are used with the Printf() function to format integer values in various ways:

Example:

package main

import "fmt"

func main() {

var i = 15

fmt.Printf("Binary       : %b\n", i)

fmt.Printf("Decimal      : %d\n", i)

fmt.Printf("Signed       : %+d\n", i)

fmt.Printf("Octal        : %o\n", i)

fmt.Printf("Octal (0o)   : %O\n", i)

fmt.Printf("Hex (lower)  : %x\n", i)

fmt.Printf("Hex (upper)  : %X\n", i)

fmt.Printf("Hex (0x)     : %#x\n", i)

fmt.Printf("Width 4      : %4d\n", i)

fmt.Printf("Left Align   : %-4d\n", i)

fmt.Printf("Zero Padded  : %04d\n", i)

}

Output:

Binary       : 1111  

Decimal      : 15    

Signed       : +15   

Octal        : 17    

Octal (0o)   : 0o17  

Hex (lower)  : f     

Hex (upper)  : F     

Hex (0x)     : 0xf   

Width 4      :   15  

Left Align   : 15    

Zero Padded  : 0015  

String Formatting Verbs in Go

  • The Printf() function in Go supports various formatting verbs for working with string values. Below are commonly used verbs for string formatting:

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)

}

Output:

Hello

"Hello"

   Hello

Hello   

48656c6c6f

48 65 6c 6c 6f

Boolean Formatting Verbs

  • In Go, the following formatting verb is used with boolean data types:

Example:

package main

import (

  "fmt"

)

func main() {

  var i = true

  var j = false

  fmt.Printf("%t\n", i)

  fmt.Printf("%t\n", j)

}

Output:

true

false

Float Formatting Verbs

  • The following formatting verbs can be used with float data types in Go:

Example

package main
import ("fmt")
func main() {
  var i = 3.141
  fmt.Printf("%e\n", i)     // scientific notation
  fmt.Printf("%f\n", i)     // full decimal
  fmt.Printf("%.2f\n", i)   // 2 decimal places
  fmt.Printf("%6.2f\n", i)  // width 6, 2 decimals
  fmt.Printf("%g\n", i)     // compact representation
}

Output:

3.141000e+00
3.141000
3.14
  3.14
3.141


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send