I am very new to Go so there is probably a very simple fix for this. I am having trouble calling a method of an interface that is within a struct in an array. I think this is best explained through an example, so here is the most basic example I was able to boil my code down to.
package main
import "fmt"
/* Test Types */
type Test interface {
Hello() string
}
type TestRecord struct {
test Test
name string
}
type TestList []TestRecord
/* Other Type */
type Other struct{}
func (o Other) Hello() string {
return "Hello From Other"
}
func main() {
/* Init TestList into t */
t := make(TestList, 1, 100)
/* Create TestRecord and add it to the list */
tr := TestRecord{Other{}, "OtherRecord"}
t = append(t, tr)
/* Loop over list and */
for _, otr := range t {
fmt.Println(otr.name)
fmt.Println(otr.test.Hello())
}
}
When I try to execute this, I get the following error:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x400efd]
How do I properly call Hello while looping through the array in this setup?