I want to avoid crashing this code and want the result to be printed by this interface. I tried a lot of ways but it didn’t work. My point is to call function 1 in function 2 and vice versa and to avoid nil dereference, I wrote this
You are not embedding anything and it looks like you are misusing the interface. Interface describes functions which type should implement to fulfil the interface. Embedding on the other hand is a “subclassing” to implement golang view on inheritance.
@lemarkar My main point in which I am trying to solve a problem:
I have interfaces called TotalAmount, and BudgetAmount.
I write the TotalAmount method in the TotalAmount interface, the BudgetAmount method in the BudgetAmount interface and then there is a time when these two methods call each other also because they depend on each other and this gives me the import cycle. I don’t know how to avoid it in the interface perspective.
Because the solution that you provided is you divided the methods into two modules but my case is different in which module 1 is dependent on module 2 and vice versa.
I hope you understood my point dealing with import cycle in the interfaces.
My solution was with the examples you’d given from the start. In this case you can try to add 3d interface and embed other 2. Like for example io.ReadWriter in standard library.
type TotalBudget interface {
TotalAmount
BudgetAmount
}
It’s hard to say without code and logic. But if you’ve run into import cycle there might be a deep problem with project design.