Entity Component System (ECS) usually comes up in optimization talks—when tens of thousands of bullets fill the screen and the CPU needs tight cache packing. Paradoxically, 2D Go games pick it up for a different reason. A small indie game doesn't care about cache misses; its code structure starts cracking long before performance ever does.
Go has no class inheritance. Trying to port an Actor -> Enemy -> FlyingEnemy -> Boss hierarchy using struct embedding quickly creates bloated types with dozens of fields, where adding a mechanic like poison forces you to refactor half the codebase. Pairing the 2D engine Ebitengine with the ECS library Donburi solves that cleanly.
How ECS works and where composition fits
The whole design rests on three roles:
- an
Entity— a bare numeric ID with no fields (donburi.Entitywrappingint64); - a
Component— a plain Go struct holding data (position, velocity) with no logic; - a
System— a function that queries entities with matching components and updates them each frame.
Instead of an inheritance tree, you assemble objects by composition, like building blocks. Under the hood, Donburi uses archetype storage (inspired by Rust's legion): entities sharing the same set of components sit side by side in dense arrays. Go generics provide strict type safety with no reflection or external dependencies.
Splitting an enemy and a rock into components
Instead of a heavy Enemy struct with X, Y, VX, VY, HP, Sprite, IsChasing, Donburi builds an enemy on the fly: world.Create(Position, Velocity, Health, Sprite, EnemyTag).
The movement system only looks at (Position, Velocity)—it doesn't care whether it's moving a player, an enemy, or a bullet. The renderer reads (Position, Sprite) and knows nothing about health. Need a static rock? Call world.Create(Position, Sprite) without velocity, and physics skips it automatically.
Ebitengine and Donburi together in code
Ebitengine stays minimal and doesn't force an architecture: your game only needs the three-method ebiten.Game interface (Update, Draw, Layout). Donburi v1.15.8 (MIT) requires Go 1.23+:
# Install Ebitengine v2 and Donburi
go get github.com/hajimehoshi/ebiten/v2
go get github.com/yohamta/donburi
In the latest API, you create queries directly with donburi.NewQuery (the old query package is gone) and loop over entities using the Go 1.23 Iter iterator:
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/filter"
)
// Components are plain data structs with no methods
type PositionData struct{ X, Y float64 }
type VelocityData struct{ X, Y float64 }
var (
Position = donburi.NewComponentType[PositionData]()
Velocity = donburi.NewComponentType[VelocityData]()
)
type Game struct {
world donburi.World
moveQuery *donburi.Query
}
func NewGame() *Game {
w := donburi.NewWorld()
// Create an entity and set initial data
entry := w.Entry(w.Create(Position, Velocity))
Position.SetValue(entry, PositionData{X: 100, Y: 100})
Velocity.SetValue(entry, VelocityData{X: 1.5, Y: 0.5})
return &Game{
world: w,
moveQuery: donburi.NewQuery(
filter.Contains(Position, Velocity),
),
}
}
// Movement system inside the Ebitengine update loop
func (g *Game) Update() error {
for entry := range g.moveQuery.Iter(g.world) {
pos := Position.Get(entry)
vel := Velocity.Get(entry)
pos.X += vel.X
pos.Y += vel.Y
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {}
func (g *Game) Layout(w, h int) (int, int) { return 640, 480 }
Iter returns iter.Seq[*donburi.Entry], so Update reads like a standard Go loop (the moveQuery.Each callback is still available too). As your game grows, the library also includes filter, features/events, features/transform, features/hierarchy, and features/math.
When ECS pays off and when it gets in the way
Composition shines when a game has many overlapping mechanics: movement, collisions, damage, status effects, inventory, and AI. You add a new feature like poison by creating one new component and one system, without touching existing code.
In a simple arcade game with two or three object types—like Snake, Pong, or Tetris—ECS is overkill. Setting up a world and queries for a couple of struct slices adds needless boilerplate where a plain []Bullet is shorter and clearer.
If you adopt Donburi, keep two details in mind. You control system order in Update manually (input, movement, collisions, cleanup)—getting it wrong introduces a one-frame delay. Also, calling world.Remove(entity) immediately invalidates the entity (world.Valid(entity) == false), so never cache a *donburi.Entry across frames without checking.



