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")
}