To format and print variables of different data types using fmt.Printf in Go, you can use format verbs. Here's an example that demonstrates how to format and print variables of different data types:
package main
import "fmt"
func main() { // Variables of different data types var number = 42 var pi = 3.14159 var isTrue = true var str = "Hello, Go!"
// Formatting and printing variables
fmt.Printf("Number: %d\n", number) // %d for formatting integers
fmt.Printf("Pi: %f\n", pi) // %f for formatting floating-point numbers
fmt.Printf("Is True?: %t\n", isTrue) // %t for formatting booleans
fmt.Printf("String: %s\n", str) // %s for formatting strings
}
In this example, we have variables of different data types: number (int), pi (float64), isTrue (bool), and str (string).
To format and print these variables, we use Printf from the fmt package. Inside the printf statement, we use format verbs like %d for integers, %f for floating-point numbers, %t for booleans, and %s for strings. These verbs are followed by the variable name to be printed. The %d and %s verbs are followed by a newline character (\n) to print a new line after each variable.
When you run this program, it will output:
Number: 42 Pi: 3.141590 Is True?: true String: Hello, Go!