Various examples of printing and formatting in Golang
Printf method accepts a formatted string for that the codes like "%s" and "%d" in this string to indicate insertion points for values. Those values are then passed as arguments.
Example
package main
import (
"fmt"
)
var(
a = 654
b = false
c = 2.651
d = 4 + 1i
e = "Australia"
f = 15.2 * 4525.321
)
func main(){
fmt.Printf("d for Integer: %d\n", a)
fmt.Printf("6d for Integer: %6d\n", a)
fmt.Printf("t for Boolean: %t\n", b)
fmt.Printf("g for Float: %g\n", c)
fmt.Printf("e for Scientific Notation: %e\n", d)
fmt.Printf("E for Scientific Notation: %E\n", d)
fmt.Printf("s for String: %s\n", e)
fmt.Printf("G for Complex: %G\n", f)
fmt.Printf("15s String: %15s\n", e)
fmt.Printf("-10s String: %-10s\n",e)
t:= fmt.Sprintf("Print from right: %[3]d %[2]d %[1]d\n", 11, 22, 33)
fmt.Println(t)
}
Output
d for Integer: 654
6d for Integer: 654
t for Boolean: false
g for Float: 2.651
e for Scientific Notation: (4.000000e+00+1.000000e+00i)
E for Scientific Notation: (4.000000E+00+1.000000E+00i)
s for String: Australia
G for Complex: 68784.8792
15s String: Australia
-10s String: Australia
Print from right: 33 22 11
Most Helpful This Week
Launches 10 Goroutines and each goroutine adding 10 values to a Channel
Comparing Structs with the Different Values Assigned to Data Fields
How to print string with double quote in Go?
Copy Struct Type Using Value and Pointer Reference
Example: How to use ReadAtLeast from IO Package in Golang?
Panic: runtime error: index out of range error in Golang
Convert Int data type to Int16 Int32 Int64
Golang program to demonstrates how to encode map data into a JSON string.
Golang HTTP GET request with parameters
Web Application to generate QR code in Golang