Golang Printf – fmt Package | Go Format

Golang printf fmt package

In this blog, we are going to have a deep look at the Golang Printf Format function in fmt package or Golang Format Package.

Learn about Golang Basics.

Let’s Start!

Golang Printf function

Golang Printf Function Signature

func Printf(format string, a …interface{}) (n int, err error)

The Golang Printf function takes two parameters the format string and an interface. It returns the number of bytes written and an error if any error encountered while writing.

Example:

a, err := fmt.Printf("%v\n", "Hello Go")
fmt.Println(a, err)

Output:

Hello Go
9 <nil>

Hello Go, string gets printed out and in the next line the number of bytes and any possible error is printed.

The new line character is also added as bytes and thus the total number of bytes is 9 and the error is NIL.

Golang Printf Types of Format String

The tags or verbs used in the format string of Go Printf function is very similar to C language.

Important Tags are:

1. %v

This tag is used to print the value in default format.

Example:

fmt.Printf("%v\n", "Hello Go")

Hello Go

2. %+v

This tag is used to print field names of structs.

Golang Print struct

Difference between %v and %+v

When %v is used as format tag in Printf only structs values are printed.

Example:

type student struct {
	name   string
	rollno int
}

func main() {

	s := student{
		name:   "Divyanshu Shekhar",
		rollno: 17,
	}
	number, err := fmt.Printf("%v\n", s)
	fmt.Printf("%v,%v\n", number, err)
}

Output:

{Divyanshu Shekhar 17}
23,<nil>

But, when we are using %+v the field name of the struct also gets printed out along with the values.

Example:

number, err := fmt.Printf("%+v\n", s)

Output:

{name:Divyanshu Shekhar rollno:17}
35,

3. %#v

This also Prints the value in Golang syntax.

Example:

	fmt.Printf("%#v\n", "Hello Go")
	fmt.Printf("%#v\n", 17)
	fmt.Printf("%#v\n", 17.50)
	fmt.Printf("%#v\n", true)
	fmt.Printf("%#v\n", 1)
	fmt.Printf("%#v\n", false)
	fmt.Printf("%#v\n", 0)
	fmt.Printf("%#v\n", 'a')

Output:

“Hello Go”
17
17.5
true
1
false
0
97

Comparision with %v

	fmt.Printf("%v\n", "Hello Go")
	fmt.Printf("%v\n", 17)
	fmt.Printf("%v\n", 17.50)
	fmt.Printf("%v\n", true)
	fmt.Printf("%v\n", 1)
	fmt.Printf("%v\n", false)
	fmt.Printf("%v\n", 0)
	fmt.Printf("%v\n", 'a')

Output:

Hello Go
17
17.5
true
1
false
0
97

With %#v the string value gets printed as it is written in Go i.e with quotes while quotes are not printed when using %v in Printf in Go ftm Package.

4. %T

The %T tag helps us to know what type of Go value it is.

Example:

	fmt.Printf("%T\n", "Hello Go")
	fmt.Printf("%T\n", 17)
	fmt.Printf("%T\n", 17.50)
	fmt.Printf("%T\n", true)
	fmt.Printf("%T\n", []int{1, 2, 3})
	fmt.Printf("%T\n", false)
	fmt.Printf("%T\n", [3]int{1, 2, 3})
	fmt.Printf("%T\n", 'a')

Output:

string
int
float64
bool
[]int
bool
[3]int
int32

Learn about Golang Datatypes.

5. %%

The %% tag in Printf format string takes no value and just returns % symbol.

Example:

	fmt.Printf("%%")

Output:

%

7. \n

\n is an escape sequence character and is used to print new line.

Golang Printf Boolean Tag

1. %t

The %t tag just print true or false value.

This tag can also check conditions and then print bool types.

Example:

	fmt.Printf("%v,%t\n", "true ", true)
	fmt.Printf("%v,%t\n", "false ", false)
	fmt.Printf("%v,%t\n", "5 == 5 ", 5 == 5)
	fmt.Printf("%v,%t\n", "10 == 5 ", 10 == 5)
	fmt.Printf("%v,%t\n", "5 < 2 ", 5 < 2)
	fmt.Printf("%v,%t\n", "10 > 5 ", 10 > 5)

Output:

true ,true
false ,false
5 == 5 ,true
10 == 5 ,false
5 < 2 ,false 10 > 5 ,true

Golang Printf Integer Tags:

%b	Binary representation of a decimal number (base 2)
%d	Decimal Representation (base 10)
%o	Octal Representation (base 8)
%x	Hexadecimal Representation (base 16, lower-case a-f)
%X	Hexadecimal Representation (base 16, upper-case A-F)
%U	Unicode format

Example:

	fmt.Printf("%v,%b\n", "Binary of 17", 17)
	fmt.Printf("%v,%d\n", "Decimal of 17", 17)
	fmt.Printf("%v,%o\n", "Octal of 17", 17)
	fmt.Printf("%v,%x\n", "Hexadecimal(x) of 200", 200)
	fmt.Printf("%v,%X\n", "Hexadecimal(X) of 200", 200)

Output:

Binary of 17,10001
Decimal of 17,17
Octal of 17,21
Hexadecimal(x) of 200,c8
Hexadecimal(X) of 200,C8

Golang Printf Floating point Tags:

%e	scientific notation
%E	scientific notation for larger number
%f	decimal point but no exponent
%F	synonym for %f
%x	Hexadecimal notation(with decimal power of two exponent)
%X	Uppercase Hexadecimal notation

Example:

	fmt.Printf("%v,%e\n", "10.50", 10.50)
	fmt.Printf("%v,%E\n", "10.50", 10.50)
	fmt.Printf("%v,%f\n", "10.543219876", 10.543219876)
	fmt.Printf("%v,%F\n", "10.543219876", 10.543219876)
	fmt.Printf("%v,%x\n", "10.50", 10.50)
	fmt.Printf("%v,%X\n", "10.50", 10.50)

Output:

10.50,1.050000e+01
10.50,1.050000E+01
10.543219876,10.543220
10.543219876,10.543220
10.50,0×1.5p+03
10.50,0X1.5P+03

Golang Printf String and Slice of Bytes tags:

%s	value of the string or slice
%q      double-quoted string

Example:

	s := []string{"The", "Go", "Language"}
	str := "Hello Go"
	fmt.Printf("%s\n", s)
	fmt.Printf("%q\n", s)
	fmt.Printf("%s\n", str)
	fmt.Printf("%q\n", str)

Output:

[The Go Language]
[“The” “Go” “Language”]
Hello Go
“Hello Go”

Go Printf Pointer tags

%p	   value of the pointer(Address stored in base 16)
%p(slice)  When applied on Slices it returns the address of 0th index 

Example:

	s := []string{"The", "Go", "Language"}
	n := 17
	p := &n
	fmt.Printf("%p\n", p)
	fmt.Printf("%p\n", &n)
	fmt.Printf("%p\n", s)
	fmt.Printf("%p\n", &s[0])

Output:

0xc0000100a0
0xc0000100a0
0xc000070330
0xc000070330

Also, read Why Golang is called the future of Server-side language?

Learn more about Go Printf function from Go fmt package official Docs.

Leave a Reply

Your email address will not be published. Required fields are marked *