fmt.Printf
in Go is a powerful function used to format and print data. It is similar to the printf
function in languages like C, and it uses format specifiers to determine how the provided arguments should be formatted in the output string.
Common Format Specifiers:
%v: The default format, depending on the argument type.
%+v: Same as
%v
but will include the field names in the default formatter for structs.%#v: A representation of the Go-syntax for the value.
%T: Type of the value.
%d: Integer (base 10).
%b: Integer (base 2, binary).
%o: Integer (base 8, octal).
%x, %X: Integer (base 16, hex). Uses lowercase or uppercase letters for a-f respectively.
%f, %F: Floating-point number.
%e, %E: Scientific notation.
%g: Decides
%e
or%f
based on magnitude.%s: String.
%q: A double-quoted string safely escaped with Go syntax.
%p: Pointer.
%c: The character represented by the corresponding Unicode code point.
Using fmt.Printf
:
Here's how you'd use some of these format specifiers with fmt.Printf
:
package main
import "fmt"
func main() {
name := "Alice"
age := 30
height := 1.68
fmt.Printf("Name: %s, Age: %d, Height: %f meters\n", name, age, height)
}
This program will print: Name: Alice, Age: 30, Height: 1.680000 meters
.
Exercises:
Basic Formatting:
- Declare an integer, a float, and a string. Print them using
%d
,%f
, and%s
.
- Declare an integer, a float, and a string. Print them using
Go Syntax Formatting:
- Declare a slice of integers and print it using
%v
and%#v
. Observe the difference.
- Declare a slice of integers and print it using
Printing Types:
- Declare a few variables of different types (int, float64, string, bool). Use
%T
to print their types.
- Declare a few variables of different types (int, float64, string, bool). Use
Scientific Notation:
- Declare a very small float (like
0.000000123
) and a large float (like1234567890.0
). Print them using%e
,%E
, and%g
.
- Declare a very small float (like
Structs with Field Names:
- Declare a struct representing a book with fields
Title
andAuthor
. Initialize an instance of the struct and print it using%v
and%+v
.
- Declare a struct representing a book with fields
Escape Sequences in Strings:
- Declare a string containing quotes and backslashes. Print it using
%s
and%q
.
- Declare a string containing quotes and backslashes. Print it using
Pointer Addresses:
- Declare a variable and print its memory address using
%p
.
- Declare a variable and print its memory address using
Solutions
Let's solve each exercise.
1. Basic Formatting:
package main
import "fmt"
func main() {
number := 42
piValue := 3.141592
word := "Hello"
fmt.Printf("Integer: %d, Float: %f, String: %s\n", number, piValue, word)
// Output: Integer: 42, Float: 3.141592, String: Hello
}
2. Go Syntax Formatting:
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
fmt.Printf("Default: %v\n", numbers) // Output: Default: [1 2 3 4 5]
fmt.Printf("Go syntax: %#v\n", numbers) // Output: Go syntax: []int{1, 2, 3, 4, 5}
}
3. Printing Types:
package main
import "fmt"
func main() {
integer := 42
floating := 3.14
text := "Golang"
truthy := true
fmt.Printf("Type of integer: %T\n", integer) // Output: Type of integer: int
fmt.Printf("Type of floating: %T\n", floating) // Output: Type of floating: float64
fmt.Printf("Type of text: %T\n", text) // Output: Type of text: string
fmt.Printf("Type of truthy: %T\n", truthy) // Output: Type of truthy: bool
}
4. Scientific Notation:
package main
import "fmt"
func main() {
small := 0.000000123
large := 1234567890.0
fmt.Printf("Small in %e\n", small) // Output: Small in 1.230000e-07
fmt.Printf("Small in %E\n", small) // Output: Small in 1.230000E-07
fmt.Printf("Large in %g\n", large) // Output: Large in 1.23456789e+09
}
5. Structs with Field Names:
package main
import "fmt"
type Book struct {
Title string
Author string
}
func main() {
myBook := Book{Title: "Go Programming", Author: "John Doe"}
fmt.Printf("Default: %v\n", myBook) // Output: Default: {Go Programming John Doe}
fmt.Printf("With field names: %+v\n", myBook) // Output: With field names: {Title:Go Programming Author:John Doe}
}
6. Escape Sequences in Strings:
package main
import "fmt"
func main() {
sentence := "She said, \"Hello, World!\" and \\ disappeared."
fmt.Printf("Normal: %s\n", sentence)
// Output: Normal: She said, "Hello, World!" and \ disappeared.
fmt.Printf("Quoted: %q\n", sentence)
// Output: Quoted: "She said, \"Hello, World!\" and \\ disappeared."
}
7. Pointer Addresses:
package main
import "fmt"
func main() {
number := 42
fmt.Printf("Address of number: %p\n", &number) // This will print the memory address of the number.
}
These solutions demonstrate how to utilize the format specifiers with the fmt.Printf
function in Go. If you run and understand each one, you'll have a solid grasp of this concept!