Golang Printf – fmt Package | Go Format Read it later

Rate this post
[elementor-template id=”28017″]

Golang Printf – fmt Package | Go Format

Advertisement

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("%vn", "Hello Go")
fmt.Println(a, err)

Output:

Hello Go
9

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("%vn", "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("%vn", s)
	fmt.Printf("%v,%vn", number, err)
}

Output:

{Divyanshu Shekhar 17}
23,

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("%+vn", s)

Output:

{name:Divyanshu Shekhar rollno:17}
35,

3. %#v

This also Prints the value in Golang syntax.

Example:

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

Output:

“Hello Go”
17
17.5
true
1
false
0
97

Comparision with %v

	fmt.Printf("%vn", "Hello Go")
	fmt.Printf("%vn", 17)
	fmt.Printf("%vn", 17.50)
	fmt.Printf("%vn", true)
	fmt.Printf("%vn", 1)
	fmt.Printf("%vn", false)
	fmt.Printf("%vn", 0)
	fmt.Printf("%vn", '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("%Tn", "Hello Go")
	fmt.Printf("%Tn", 17)
	fmt.Printf("%Tn", 17.50)
	fmt.Printf("%Tn", true)
	fmt.Printf("%Tn", []int{1, 2, 3})
	fmt.Printf("%Tn", false)
	fmt.Printf("%Tn", [3]int{1, 2, 3})
	fmt.Printf("%Tn", '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,%tn", "true ", true)
	fmt.Printf("%v,%tn", "false ", false)
	fmt.Printf("%v,%tn", "5 == 5 ", 5 == 5)
	fmt.Printf("%v,%tn", "10 == 5 ", 10 == 5)
	fmt.Printf("%v,%tn", "5 < 2 ", 5 < 2)
	fmt.Printf("%v,%tn", "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,%bn", "Binary of 17", 17)
	fmt.Printf("%v,%dn", "Decimal of 17", 17)
	fmt.Printf("%v,%on", "Octal of 17", 17)
	fmt.Printf("%v,%xn", "Hexadecimal(x) of 200", 200)
	fmt.Printf("%v,%Xn", "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,%en", "10.50", 10.50)
	fmt.Printf("%v,%En", "10.50", 10.50)
	fmt.Printf("%v,%fn", "10.543219876", 10.543219876)
	fmt.Printf("%v,%Fn", "10.543219876", 10.543219876)
	fmt.Printf("%v,%xn", "10.50", 10.50)
	fmt.Printf("%v,%Xn", "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("%sn", s)
	fmt.Printf("%qn", s)
	fmt.Printf("%sn", str)
	fmt.Printf("%qn", 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("%pn", p)
	fmt.Printf("%pn", &n)
	fmt.Printf("%pn", s)
	fmt.Printf("%pn", &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.

Was This Article Helpful?

Leave a Reply

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