Lesson Overview

Hello! Today, we're exploring a fundamental facet of Go: Composition. Composition is a design principle in Go that enables code reuse. Unlike languages that use inheritance, Go opts for Composition. This lesson aims to understand Composition and how it applies in Go.

Understanding Struct Buzzwords

Composition in Go allows for the construction of complex types using simpler ones. In Go's Composition, you frequently encounter terms like Embedding and Anonymous Fields. Embedding involves including one struct inside another, creating a parent-child relationship. Anonymous Fields are declared in a struct without a name, and their type becomes their name.

In the example above, Point is an anonymous field in the Circle.

Working with Composition in Go: Part 1

Now, let's see Composition in action in Go, using structs:

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal