Golang Interfaces and Mocks in 5 Minutes
Go interfaces and mocks for beginners, in five minutes, on a real Go program: why a test that calls a real service passes one day and fails the next, what an interface is (a list of methods), how a type satisfies one with no implements keyword, what the compiler says when a method is missing, and how a small fake in the test file lets the test run offline in a millisecond, including a declined card.
One test, two results
Your Go test passes one day and fails the next. Tom sells eggs from a roadside stand with a card reader that calls an online card service. His test for selling two dozen passed in about a second. Then he read the card service's log: his test had charged nine dollars to a real card. On Sunday the card service was down, and the same test failed: connection refused. So how do you test a sale without a card service? By the end, an interface and a small mock make this test pass in a thousandth of a second, with no card service at all.

Why does a test call the card service at all? Here's Sell, the function that makes a sale. It takes a CardReader, and only a CardReader. Nothing else is allowed in. So the only thing a test can hand it is the real card reader, and the real one always calls the card service.
sed -n '28,34p' main.go// Sell charges for the eggs, then hands them over. func Sell(card CardReader, dozens int) (int, error) { if err := card.Charge(dozens * pricePerDozen); err != nil { return 0, err } return dozens * 12, nil }
An interface is a list of methods
An interface fixes that. A method is a function that belongs to a type, like Charge on the card reader. An interface is a type that lists methods, and nothing else. Tom's Payer interface lists one method: Charge, which takes a number of cents and returns an error. Any type with a Charge method like that is a Payer.
sed -n '10,13p' main.go// Payer is anything that can take a payment. type Payer interface { Charge(cents int) error }
Think of the socket on the egg stand's fridge. It takes a kettle or a lamp. All it cares about is the shape of the plug. The interface is the socket. Any type with that Charge method has the right plug.

So Tom changes the first line of Sell: it now takes any Payer, not just a CardReader. The card reader already has that Charge method, so the shop works just as before.

Tom also has an honesty box for coins: a CashBox type with its own Charge method. Some languages make you declare that a type fits an interface. Go doesn't. CashBox has the method, so it fits. Run the program. The same Sell works with both. Twenty-four eggs go by card, twelve from the cash box. The box now holds four hundred and fifty cents, the price of the dozen it just sold.
sed -n '41,49p' main.go// CashBox is the honesty box for coins. type CashBox struct { Total int } func (b *CashBox) Charge(cents int) error { b.Total += cents return nil }
go run .card reader: eggs 24 err <nil> cash box: eggs 12 err <nil> box holds 450
Now Tom renames the cash box's method to Take. Then he runs go build, which compiles the code without running it. Guess first: does Sell still work with it, or does the build fail? It fails. The compiler says CashBox does not implement Payer: missing method Charge. So Go checks every Payer for you, before anything runs.
grep -n 'CashBox) Take' main.go46:func (b *CashBox) Take(cents int) error {
go build .# eggstand ./main.go:56:19: cannot use box (variable of type *CashBox) as Payer value in argument to Sell: *CashBox does not implement Payer (missing method Charge)
Effective Go, the official guide, puts it this way: if something can do this, then it can be used here. It adds that interfaces with one or two methods are common, and a small one is easy to fake.

A fake card reader for tests
Sell now takes any Payer, but Tom's test still passes the real card reader, so it still charges a real card. In a test, he should pass a mock instead: a small fake type called fakePayer, in the test file, with the same Charge method. Two plugs, one socket. The mock never touches the network, so it can't charge anyone.
![Sell only knows the Payer interface: diagram of Sell(p Payer), CardReader, card service, fakePayer, charged: [900], Payers](/img/golang-interfaces-and-mocks-in-5-minutes/hero.jpg)
The mock lives in sell underscore test dot go, a file Go builds only for tests. It holds two values, called fields: a list of every amount it was charged, and an error to return. Charge adds the amount to the list, then returns the error. Normally that's nil, Go's word for nothing. A test can set a real error instead.
sed -n '9,17p' sell_test.gotype fakePayer struct { charged []int err error } func (f *fakePayer) Charge(cents int) error { f.charged = append(f.charged, cents) return f.err }
Tom's first test still sells two dozen, and checks the mock was charged nine hundred cents: the same nine dollars, on no real card. The error field lets him test what the real service won't do on demand: decline a card. His second test tells the mock to decline, then checks that Sell hands over zero eggs. Now run the tests, with the card service still down. Both pass, in a thousandth of a second, and nobody's card was charged.
sed -n '19,27p' sell_test.gofunc TestSellCharges(t *testing.T) { fake := &fakePayer{} eggs, err := Sell(fake, 2) if err != nil || eggs != 24 { t.Fatalf("got %d eggs, err %v", eggs, err) } if len(fake.charged) != 1 || fake.charged[0] != 900 { t.Errorf("charged %v, want [900]", fake.charged) }
sed -n '30,39p' sell_test.gofunc TestSellDeclined(t *testing.T) { fake := &fakePayer{err: errors.New("card declined")} eggs, err := Sell(fake, 2) if err == nil { t.Fatal("want an error") } if eggs != 0 { t.Errorf("handed over %d eggs for a declined card", eggs) } }
go test -v=== RUN TestSellCharges --- PASS: TestSellCharges (0.00s) === RUN TestSellDeclined --- PASS: TestSellDeclined (0.00s) PASS ok eggstand 0.001s
Recap
So: an interface is a list of methods. Any type with those methods fits it automatically. You never declare it. Keep interfaces small, and have functions like Sell accept them, so a test can pass in a fake.

If interfaces finally make sense, subscribing brings the next Go idiom: channels.
All the lab files and commands are linked in the description. Thanks for watching Cloud Explainers.
Sources
Demos: one container on our own Mac standing in for a laptop, and a small stand-in for a card service; the egg stand and its charges are made up. Every command, output and number was real. Watch the full video on YouTube.