Breaking circular dependencies via implicit interfaces

In the following imaginary scenario, we are working for a start-up company that is building the software that's responsible for controlling fully automated warehouses. Autonomous robots equipped with gripping arms and lasers (what could possibly go wrong?) are busy moving around the warehouse floor locating and picking up order items from the shelves and placing them in cardboard boxes that are then shipped to the customers.

This is a tentative definition for a warehouse Robot:

In the preceding code snippet, the Item and Box types live in an external package called entity. All goes well until one day when someone attempts to introduce a new helper method to the Box type, which, unfortunately, introduces an import cycle:

Technically speaking, this is a bad design decision: boxes and items should not really be aware of the robot's existence. However, for the sake of this argument, we will ignore this design flaw and try to work around this problem using Go's support for implicit interfaces. The first step would be to define a Packer interface within the entity package. Secondly, we would need to provide an abstraction for obtaining an instance of Packer, as shown in the following code snippet:

With these two mechanisms in place, the helper method can work without the need to import the warehouse package:

The last bit of the puzzle that we need to address is how we are going to initialize AcquirePacker without importing the warehouse package. The only way we can do that is via a third package that imports the warehouse and entity packages:

In the preceding code snippet, the wireComponents function ensures that the warehouse and entity packages are wired together without triggering any circular dependency errors.