go
35 lines · 5 steps
Implementing fmt.Stringer in Go
How custom String methods let your types control their own formatted output and compose cleanly.
Explained by
highlit
1package geometry
2
3import "fmt"
4
5// Point represents a coordinate in 2D space.
6type Point struct {
7 X, Y int
8}
9
10// String satisfies the fmt.Stringer interface, controlling how a Point
11// is rendered by fmt verbs like %v, %s, and by Println.
12func (p Point) String() string {
13 return fmt.Sprintf("(%d, %d)", p.X, p.Y)
14}
15
16// Color models an RGB color.
17type Color struct {
18 R, G, B uint8
19}
20
21func (c Color) String() string {
22 return fmt.Sprintf("#%02X%02X%02X", c.R, c.G, c.B)
23}
24
25// Shape composes other Stringer values; its own String method delegates
26// to them, demonstrating how Stringer composes naturally.
27type Shape struct {
28 Name string
29 Origin Point
30 Fill Color
31}
32
33func (s Shape) String() string {
34 return fmt.Sprintf("%s at %v filled %v", s.Name, s.Origin, s.Fill)
35}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Any type with a String() string method automatically satisfies fmt.Stringer and controls its own rendering under %v, %s, and Println.
- 2Implementing Stringer once means every fmt verb and logging call formats your type consistently, without callers writing format strings.
- 3Stringer composes: an outer type's String method can use %v on its Stringer fields, so formatting nests automatically.
Related explainers
go
package streaming import ( "bufio"
Streaming NDJSON logs over HTTP in Go
http-streaming
channels
select
Advanced
10 steps
go
package api import ( "crypto/sha256"
ETag conditional requests in Gin
http-caching
etag
conditional-requests
Intermediate
6 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
go
package middleware import ( "net/http"
Per-plan export limits in Gin middleware
middleware
rate-limiting
authorization
Intermediate
7 steps
go
package httputil import ( "net"
Safely extracting the real client IP in Go
security
http
ip-spoofing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/implementing-fmt-stringer-in-go-explained-go-15b6/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.