← All Exercises

Go Struct Embedding

intermediatego~20min

Background

Struct embedding is Go alternative to inheritance, promoting fields and methods from embedded types.

Problem

Create a Vehicle struct embedding an Engine struct, demonstrating method promotion and field access.

Input

Vehicle and Engine specifications.

Output

A Vehicle with access to embedded Engine methods and fields.

Sample tests

Sample 1

Input: Vehicle{Engine{power:200}, "Truck"}
Expected: power=200, name=Truck

Your solution

Learning objective

Learn struct embedding, field/method promotion, and Go composition over inheritance.