Golang — Memento Pattern

Matthias Bruns
3 min readMar 25, 2023

Games often require some kind of save and load system to allow the player to resume their progress from where they left off. The “Memento Pattern” is a perfect fit for this kind of system. In this article, we will discuss how to use the “Memento Pattern” in Golang to implement a load and save system for games.

Components of the Load and Save System

The “Memento Pattern” consists of three components: Originator, Memento, and Caretaker. In the context of the load and save system, we can define them as follows:

  • Originator: This is the object whose state needs to be saved and restored. In the case of a game, it could be the player’s character, the game world or any other object that needs to be saved and restored.
  • Memento: This represents the saved state of the Originator. In the case of a game, it could be a file that contains the player’s progress or the state of the game world.
  • Caretaker: This is responsible for storing and retrieving the Memento. In the case of a game, it could be a save file manager that stores and retrieves the player’s progress or the state of the game world.

Implementation

Let’s implement the load and save system for a game using the “Memento Pattern” in Golang.

--

--

Matthias Bruns

Senior Freelancer & Technical Lead Working as a Golang developer since 2020. as a mobile developer since 2013. Focussed on architecture & testability.