Hi,
So today I received the “import cycle not allowed” compile error. I like the way Go forces you to think about your program design better., or rather how Go internals make it possible to write projects that adhere to good design principles as explained in @dfc 's excellent SOLID Go Design blog and video.
It seems the ‘import cycle not allowed’ problem is because the Dependency Inversion Principle (The “D” in SOLID) is not adhere to.
So basically I had the A depends on B
, B depends on A
scenario.
package router
import (
"net"
"project/connection"
)
type MessageRouter struct {
connections map[net.Addr]*ClientConnection
}
package connection
import "project/router"
type ClientConnection struct {
router *MessageRouter
}
I managed to quickly solve the problem by making the map of connections to rather use net.Conn
, which is an interface:
type MessageRouter struct {
connections map[net.Addr]*net.Conn
}
This compiled
But this made me wonder if this is really the right solution. What if I could not use net.Conn
?
Should one have to create a new package with an interface type? And which of the 2 packages should then implement said interface?
Pieter