type Person struct { Name string Age int } func (p Person) String() string { return fmt.Sprintf("%s (%d years)", p.Name, p.Age) } func main() { p := Person{"Alice", 30} fmt.Println(p) // output: Alice (30 years) }
age := 30 fmt.Printf("Alice is %d years old.\n", age)In this example, we use the Printf function from the fmt package to print a message that includes the value of the age variable. The %d format specifier is used to indicate that the age value should be printed as a decimal integer. Package Library: The fmt Stringer String package is part of the standard library in Go.