How to avoid crashing the embedded fields

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

function1.MyInterfaces{AllInterfaces: allInterfaces1}

in main package but I don’t know why it is still crushing and not printing the result.

Here is the interface package:

package interfaces

type AllInterfaces interface {
    Function1()
    Function2()
}

Here is the main package:

package main

import (
    function1 "github.com/ibilalkayy/test/two/directory1"
    function2 "github.com/ibilalkayy/test/two/directory2"
)

func main() {
    allInterfaces1 := function1.MyInterfaces{}.AllInterfaces
    allInterfaces2 := function2.MyInterfaces{}.AllInterfaces

    myFunction1 := function1.MyInterfaces{AllInterfaces: allInterfaces1}
    myFunction2 := function2.MyInterfaces{AllInterfaces: allInterfaces2}

    myFunction1.Function1()
    myFunction2.Function2()
}

Here is the function1 package

package function1

import (
    "fmt"

    "github.com/ibilalkayy/test/two/interfaces"
)

type MyInterfaces struct {
    AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) Function1() {
    fmt.Println("This is the function 1")
    m.CallFunction2()
}

func (m MyInterfaces) CallFunction2() {
    m.AllInterfaces.Function2()
}

Here is the function2 package

package function2

import (
    "fmt"

    "github.com/ibilalkayy/test/two/interfaces"
)

type MyInterfaces struct {
    AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) CallFunction1() {
    m.AllInterfaces.Function1()
}

func (m MyInterfaces) Function2() {
    m.CallFunction1()
    fmt.Println("This is the function 2")
}

Hello there, in main those are nil, zero value of the interface.

@lemarkar but I don’t know how to call this with filled values because even I write

function1.MyInterfaces{}.AllInterfaces.Function1()

it gives me this result:

function1.MyInterfaces{}.AllInterfaces.Function1() (no value) used as valuecompilerTooManyValues func (interfaces.AllInterfaces) Function1()

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.

Maybe you can be more specific on what you are trying to achieve, so I can help you with that?

UPD: @ibilalkayy if you just need a solution with your current code, it can be done like this:

main.go

package main

import (
	".../function1"
	".../function2"
)

func main() {
	allInterfaces1 := &function1.MyInterfaces{}
	allInterfaces2 := &function2.MyInterfaces{}

	allInterfaces1.AllInterfaces = allInterfaces2
	allInterfaces2.AllInterfaces = allInterfaces1

	allInterfaces1.Function2()
	allInterfaces2.Function1()
}

interface.go

package interfaces

type AllInterfaces interface {
	Function1()
	Function2()
}

function1.go

package function1

import (
	"fmt"

	".../interfaces"
)

type MyInterfaces struct {
	AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) Function1() {
	fmt.Println("This is function1 from module 1")
}

func (m MyInterfaces) Function2() {
	fmt.Println("I am function2 from module 1")
	m.AllInterfaces.Function2()
}

function2.go

package function2

import (
	"fmt"

	".../interfaces"
)

type MyInterfaces struct {
	AllInterfaces interfaces.AllInterfaces
}

func (m MyInterfaces) Function1() {
	fmt.Println("I am function1 from module 2")
	m.AllInterfaces.Function1()
}

func (m MyInterfaces) Function2() {
	fmt.Println("This is function2 from module 2")
}

output:

% go run main.go 
I am function2 from module 1
This is function2 from module 2
I am function1 from module 2
This is the function 1 from module 1

@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.

then you check this repo: flow

I will rework on my project and see if it works.