Stage 1 · Lesson 4 of 10
Interfaces
A Go is a list of methods. Any type that has those methods fits the interface automatically, with no implements keyword. That makes small interfaces very powerful.
io.Reader has just one method, and files, network connections, bodies and buffers all fit it. Define an interface where it is used, not where the types are declared, and keep it to one or two methods. The usual advice is "accept interfaces, return structs". This is how you will later swap a real database for a fake one in tests.
Common trapa stored in an interface. The interface itself is not nil, so a check like err != nil is true, even though the value inside is a nil pointer.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with
- CourseFreeA Tour of Go
The Go Authors · Beginner
The official interactive tour: basics, methods, interfaces, generics and concurrency
Practise
Keep handy
Also worth your time
The DNS, TCP, TLS and HTTP steps behind every request your backend will handle