×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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 »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.