A concrete type in Go defines exactly what a value is and how it is stored, such as a struct, an int or a []string. An interface defines only what a value can do, as a set of method signatures, and any type that has those methods satisfies it automatically. Use concrete types for data and implementation, and interfaces at the boundaries where you want to accept different implementations.
Go interfaces are satisfied implicitly: there is no implements keyword. That single design choice shapes how Go programs are structured, tested and extended.
A concrete type has a fixed memory layout and a fixed set of methods. You can create a value of it, take its size and access its fields directly.
Here Rectangle is concrete. The compiler knows its layout, calls Area directly and lets you read r.Width. Built-in types such as int, string, maps and slices are concrete too.
An interface type lists method signatures. A value of an interface type holds some concrete value plus the information needed to call its methods.
You cannot create a "plain" Shape; you can only hold a concrete value in a variable of type Shape. Both Rectangle and Circle satisfy Shape without declaring it.
TotalArea works with every present and future type that has an Area() float64 method, even types written in other packages.
| Aspect | Concrete type | Interface type |
|---|---|---|
| Describes | Data layout and behaviour | Behaviour only |
| Can hold fields | Yes | No |
| Can be instantiated directly | Yes | No, it holds a concrete value |
| Method call | Static, can be inlined | Dynamic dispatch through a method table |
| Zero value | Type-specific (0, "", empty struct) | nil |
| Satisfaction | Is what it is | Implicit, checked by the compiler |
Method calls through an interface use dynamic dispatch, which usually costs a few nanoseconds and can prevent inlining. For most code this does not matter, but measure before you rewrite a hot loop.
An interface value is conceptually a pair: a pointer to type information and a pointer (or small value) to the data. This has one famous consequence, the typed nil.
The interface holds the type *MyErr with a nil pointer, so it is not equal to nil. Return a literal nil when there is no error, never a typed nil pointer.
To get the concrete value back, use a type assertion or a type switch.
Always use the two-value form (c, ok := s.(Circle)) unless a failed assertion should panic. Needing many type switches is a sign the interface may be too small or the design should change.
io.Reader and io.Writer.In a test you pass a tiny fake that implements Get, with no mocking framework.
Since Go 1.18, type parameters let you write code that works over many concrete types while keeping static types. Use an interface when behaviour differs per type, and a type parameter when the algorithm is the same but the types vary, such as func Map[T, U any](xs []T, f func(T) U) []U. The any alias is an interface type (interface{}) that every type satisfies, and it throws away static type information, so avoid it where a specific type or a type parameter works.
No. If the type has all the methods with matching signatures, it satisfies the interface automatically. You can add a compile-time check with var _ Shape = (*Circle)(nil).
It depends on the method receivers. If a method has a pointer receiver, only *T has it in its method set, so only *T satisfies the interface; value receivers are available on both T and *T.
Calls through an interface are dynamic and may cause heap allocation when a value is boxed. The cost is small, and you should only optimise it after profiling.
Returning a concrete type exposes all its methods and fields and lets the caller decide which interface to depend on. Returning an interface hides information and forces assertions.
Concrete types define what data is and how it behaves; interfaces define only the behaviour a caller needs. Go satisfies interfaces implicitly, so you can define small interfaces at the point of use, accept them in function parameters and return concrete types. Watch for the typed nil trap, prefer type parameters over any when the algorithm is generic, and add an interface only when more than one implementation or a test double requires it.