How do I implement the Stringer interface in Go?

Go, Stringer interface, fmt package, struct, method
This example demonstrates how to implement the Stringer interface in Go which allows you to define a string representation for your struct types.
package main import ( "fmt" ) // Person struct that implements the Stringer interface type Person struct { Name string Age int } // String method to implement the Stringer interface func (p Person) String() string { return fmt.Sprintf("Name: %s, Age: %d", p.Name, p.Age) } func main() { person := Person{Name: "John Doe", Age: 30} fmt.Println(person) // Output: Name: John Doe, Age: 30 }

Go Stringer interface fmt package struct method