Skip to content
GDP for Noobs
☰ All patterns
CreationalBeginner

Factory

Ask for what you want; let a function decide how to build it.

01

Explain it like I'm new

Real-life analogy

Ordering at a restaurant

You say 'one pasta, please'. You don't walk into the kitchen and cook it yourself. The kitchen (factory) knows which chef, pan and recipe to use and hands you a finished plate.

😩 The problem

Your code needs a Notifier, but which one (email, SMS, Slack) depends on config at runtime. Scattering if kind == "email" checks everywhere makes adding a new type painful.

💡 The pattern

Write one constructor function, NewNotifier(kind), that returns an interface. Callers depend on the interface only. Adding a new type means touching one switch.

02

Walk through the code

Click a step to highlight its lines. This is a complete program you can copy and run.

main.go
1package main
2
3import "fmt"
4
5// Notifier is the "product" interface. Callers only ever see this.
6type Notifier interface {
7 Send(to, msg string) string
8}
9
10// Concrete products. Each one knows how to send in its own way.
11type EmailNotifier struct{}
12type SMSNotifier struct{}
13type SlackNotifier struct{}
14
15func (EmailNotifier) Send(to, msg string) string { return fmt.Sprintf("[email] to %s: %s", to, msg) }
16func (SMSNotifier) Send(to, msg string) string { return fmt.Sprintf("[sms] to %s: %s", to, msg) }
17func (SlackNotifier) Send(to, msg string) string { return fmt.Sprintf("[slack] @%s: %s", to, msg) }
18
19// NewNotifier is the factory: you say WHAT you want, it decides HOW to build it.
20func NewNotifier(kind string) (Notifier, error) {
21 switch kind {
22 case "email":
23 return EmailNotifier{}, nil
24 case "sms":
25 return SMSNotifier{}, nil
26 case "slack":
27 return SlackNotifier{}, nil
28 default:
29 return nil, fmt.Errorf("unknown notifier %q", kind)
30 }
31}
32
33func main() {
34 for _, kind := range []string{"email", "sms", "slack", "pigeon"} {
35 n, err := NewNotifier(kind)
36 if err != nil {
37 fmt.Println("error:", err)
38 continue
39 }
40 fmt.Println(n.Send("gopher", "Your build passed!"))
41 }
42}
terminal
Press ▶ Run to execute this program.

This output was captured by running the program with Go 1.24. Copy it into your editor or the Go Playground and try changing things.

03

When should I use it?

✅ Reach for it when…

  • •The concrete type is chosen at runtime (config, user input, flags).
  • •You want callers to depend on an interface, not a struct.

🚫 Skip it when…

  • •There's only ever one implementation. A plain NewThing() constructor is enough.

🐹 Go tip

In Go, 'factory' usually just means a NewXxx function. Return concrete types by default, and return an interface only when callers genuinely need to swap implementations.

🌍 You've already seen it in

sql.Open(driverName, dsn) and image.Decode both pick an implementation by name or format.

04

Quick check

One question. No pressure.

What does a Go factory function usually return when the caller shouldn't care about the concrete type?

Related: Strategy, Builder