ADVERTISEMENT
ADVERTISEMENT

Which format verb with fmt.Printf() is used to print the type of a variable in Go?

Learn how to print the type of a variable using fmt.Printf() in Golang?
Submitted by IncludeHelp, on October 04, 2021

In Go programming language, fmt.Printf() function is an inbuilt function of fmt package, it is used to format according to a format specifier and writes to standard output.

To print the type of a variable using the fmt.Printf() function – we use %T format verb.

Example 1:

package main

import (
	"fmt"
)

func main() {
	x := 10
	fmt.Printf("%T", x)
}

Output:

int

Example 2:

// Golang program to demonstrate the
// example of %T format verb

package main

import "fmt"

func main() {
	// Declaring some variable with the values
	a := 10
	b := 123.45
	c := "Hello, world!"
	d := []int{10, 20, 30}

	// Printing types & values of the variables
	fmt.Printf("%T, %v\n", a, a)
	fmt.Printf("%T, %v\n", b, b)
	fmt.Printf("%T, %v\n", c, c)
	fmt.Printf("%T, %v\n", d, d)
}

Output:

int, 10
float64, 123.45
string, Hello, world!
[]int, [10 20 30]

Golang FAQ »

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.