How to Implement the Stringer Interface for Custom Types

Implement the String() method on your type to satisfy the fmt.Stringer interface for custom formatting.

Implement the String() method on your custom type to satisfy the fmt.Stringer interface.

type MyType struct {
    Value int
}

func (m MyType) String() string {
    return fmt.Sprintf("MyType(%d)", m.Value)
}

This allows fmt.Print and fmt.Sprintf to automatically call your method when formatting the type.