What It Is
The Prototype pattern creates new objects by cloning an existing one instead of building from zero. You prepare one object as a template, then copy it and change only what differs.
The one thing to get right in Go: slices and maps inside a struct are shared by a plain copy — a proper Clone() method copies them too, so the clone is truly independent.
Example: A Document Template
The template carries the parts every document repeats. Clone() copies it — including the tags slice — so editing the copy leaves the template untouched.
Clone, then change the difference
The report got a new title and an extra tag, and the template still has only [draft] — because Clone() copied the slice instead of sharing it.
Output: Go program and captured output
package main
import "fmt"
type Document struct {
Title string
Footer string
Tags []string
}
// Clone copies the document — including the slice inside.
func (d Document) Clone() Document {
c := d
c.Tags = append([]string{}, d.Tags...)
return c
}
func main() {
template := Document{
Title: "Untitled",
Footer: "PromptCookbook 2026",
Tags: []string{"draft"},
}
report := template.Clone()
report.Title = "Yearly Report"
report.Tags = append(report.Tags, "finance")
fmt.Println("template:", template.Title, template.Tags)
fmt.Println("clone: ", report.Title, report.Tags)
}
When to Use It
Use it when new objects are mostly copies of a prepared one — documents from a template, game entities from a spawner, test data from a fixture.
Skip it when objects are cheap to build fresh, or contain nothing shared. For a flat struct of plain values, Go's ordinary assignment is already a copy.
Pros and Cons
Pros: cheap creation of preconfigured objects; the template documents the defaults; no repeated setup code.
Cons: deep copying is easy to get subtly wrong — a forgotten slice or map quietly links clone and original; clones drift from the template silently over time.