Erlang supports pattern matching and function clauses to express composition of types. This is somewhat different from struct embedding in other languages, but it allows for a similar kind of abstraction and code reuse.
When creating records with literals in Erlang, we initialize the embedded record explicitly. Here, we create a container record that includes a base record.
We can access the base’s fields on Co using the syntax Co#container.base#base.num.
In Erlang, we don’t have methods associated with records, but we can define functions that operate on these records. The describe_base/1 function serves a similar purpose to the describe() method in the original Go code.
Erlang doesn’t have interfaces, but we can achieve similar behavior by defining functions that pattern match on records. The describe/1 function at the end demonstrates this concept.
To run the program:
This example demonstrates how Erlang can achieve similar compositional patterns to struct embedding in Go, albeit with different syntax and concepts.