So trying out go for the first time today and keep running into an error to do with interfaces, I guess I don't understand them correctly. Ive tried looking around for an answer but the terminology that I'm used to is a little different from other languages so I can't piece it together. As practice I decided to implement a very simple linked list but the error I recieve is:
type INode* is pointer to interface, not interface
when calling .setNext(node *Inode)
What is the reason behind this? what piece of information am I missing with interfaces?
Heres the incomplete implementation:
package main
type object interface{}
type INode interface {
GetData() object
GetNext() *INode
setNext(node *INode)
}
type ILinkedList interface {
Link(node *INode)
Unlink(node *INode)
CurrentLength() int
RemoveAt(idx int)
}
type Node struct {
data object
next *INode
}
func (n *Node) GetData() object {
return n.data
}
func (n *Node) GetNext() *INode {
return n.next
}
func (n *Node) setNext(node *INode) {
n.next = node
}
type LinkedList struct {
cur *INode
last *INode
length int
}
func (l *LinkedList) Link(node *INode) {
if l == nil {
return
}
if l.cur == nil {
l.cur = node
l.last = node
} else {
l.last.setNext(node)
l.last = node
}
l.length = l.length + 1
}