Skip to content
GDP for Noobs
☰ All patterns
StructuralIntermediate

Proxy

A stand-in that controls access to the real thing.

01

Explain it like I'm new

Real-life analogy

A personal assistant

People ask the assistant for the boss's schedule. If they already know the answer they reply instantly; otherwise they ask the boss once and remember it.

😩 The problem

A weather API is slow and costs money per call. Many parts of your app ask for the same city again and again.

💡 The pattern

Put a proxy that implements the SAME interface in front of the real service. It decides whether to answer from cache, check permissions, or forward the call.

02

Play with it

An interactive model of the pattern. Click around, nothing can break.

Click a city to ask for its temperature. Then click it again.
Requests
0
Real API calls
0
Time saved
0.0s

💡 The caller code is identical either way. It just calls weather.Temperature(city). Only the proxy knows about the cache.

03

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// WeatherService is the interface both the real thing and the proxy implement.
6type WeatherService interface {
7 Temperature(city string) int
8}
9
10// RealWeatherAPI pretends to be a slow, expensive network call.
11type RealWeatherAPI struct{ calls int }
12
13func (r *RealWeatherAPI) Temperature(city string) int {
14 r.calls++
15 fmt.Println(" slow network call for", city)
16 return map[string]int{"Delhi": 31, "Paris": 14}[city] // fake temperatures
17}
18
19// CachingProxy stands in front of the real service and remembers answers.
20type CachingProxy struct {
21 real WeatherService
22 cache map[string]int
23}
24
25func (p *CachingProxy) Temperature(city string) int {
26 if t, ok := p.cache[city]; ok {
27 fmt.Println(" cache hit for", city)
28 return t
29 }
30 t := p.real.Temperature(city)
31 p.cache[city] = t
32 return t
33}
34
35func main() {
36 api := &RealWeatherAPI{}
37 // The caller can't tell it's talking to a proxy. Same interface!
38 var weather WeatherService = &CachingProxy{real: api, cache: map[string]int{}}
39
40 for _, city := range []string{"Delhi", "Paris", "Delhi", "Delhi", "Paris"} {
41 fmt.Printf("%s: %d°C\n", city, weather.Temperature(city))
42 }
43 fmt.Println("Real API calls made:", api.calls, "out of 5 requests")
44}
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.

04

When should I use it?

✅ Reach for it when…

  • •Caching, rate limiting, access control, lazy initialisation, logging remote calls.

🚫 Skip it when…

  • •The extra layer adds complexity without a clear benefit.

🐹 Go tip

For concurrent use, guard the cache map with a sync.Mutex, or use sync.Map for read-heavy workloads.

🌍 You've already seen it in

httputil.ReverseProxy, gRPC client stubs and caching wrappers around repositories.

05

Quick check

One question. No pressure.

How is a Proxy different from a Decorator?

Related: Decorator, Facade