I tried searching, but can't find how to do this. I want to take instances of my struct and store them in a list, then iterate over them and call a function on them.
type comicBook struct {
Title string
Writer string
Artist string
Publisher string
Year int
Pages int
Grade float32
}
func (c comicBook) PrintInfo() {
fmt.Println(c.Title, "written by", c.Writer, "drawn by", c.Artist, "published by", c.Publisher, "in", c.Year)
fmt.Println("Pages:", c.Pages)
fmt.Println("Grade:", c.Grade)
}
This works fine.
c := comicBook.NewComicBook(
"Mr. GoToSleep",
"Tracey Hatchet",
"Jewel Tampson",
"DizzyBooks Publishing Inc.",
1997,
14,
6.5,
)
c.PrintInfo()
c2 := comicBook.NewComicBook("Epic Vol. 1",
"Ryan N. Shawn",
"Phoebe Paperclips",
"DizzyBooks Publishing Inc.",
2013, 160, 9.0)
c2.PrintInfo()
Storing them in a list does not.
comicBookList := list.New()
comicBookList.PushBack(c)
comicBookList.PushBack(c2)
fmt.Println("\nNow from a list!\n")
for comic := comicBookList.Front(); comic != nil; comic = comic.Next() {
comic.Value.PrintInfo()
}
This fails with comic.Value.PrintInfo undefined (type interface {} is interface with no methods)