SoFunction
Updated on 2025-04-05

Detailed explanation of the usage example of Stringer in go language

Stringer in Go is a very useful interface defined in the fmt package of the standard library. The Stringer interface allows types to define how they are represented, which is especially useful when formatting output. Let's take a closer look:

Stringer interface definition:

type Stringer interface {
    String() string
}

use:
When using the print function of the fmt package (such as , ) to output a type that implements the Stringer interface, its String() method is automatically called to get the string representation of that type.

Implement Stringer:

type Person struct {
    Name string
    Age  int
}
func (p Person) String() string {
    return ("%s (%d years old)", , )
}

Example of usage:

func main() {
    p := Person{"Alice", 30}
    (p)  // Output: Alice (30 years old)}

Automatically call:
The fmt package will automatically check whether a type implements the Stringer interface. If it is implemented, the String() method will be called.

String representation for custom types:
The Stringer interface is particularly suitable for defining more meaningful or readable string representations for custom types.

Applications in other packages:
Many standard libraries and third-party packages use the Stringer interface to provide better output.

Relationship with error interface:
The error interface also has an Error() string method, similar to Stringer. A type can implement both error and Stringer interfaces.

Performance considerations:
The String() method should be efficient because it can be called frequently, especially when logging.

Example: Custom enumeration type

type Day int
const (
    Monday Day = iota
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
)
func (d Day) String() string {
    return [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}[d]
}
func main() {
    (Wednesday)  // Output: Wednesday}

Applications in debugging:
Implementing the Stringer interface makes debugging easier because it provides a readable representation of a custom type.

Differences from JSON and other serialization:
Stringer is mainly used for human-readable output, rather than data serialization. For serialization, a specific serialization method (such as MarshalJSON) should be used.

The Stringer interface is a simple but powerful feature in the Go language that allows developers to provide more meaningful string representations for custom types, enhancing the readability and debugibility of the code. It is often a good practice to consider implementing the Stringer interface when designing new types, especially when these types need to be printed or recorded.

This is the end of this article about the use of Stringer in Go language. For more related content of using go Stringer, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!