I'm new to golang and exploring the language. I am creating a linked list, but it seems to go out of scope and does not store variables correctly.
When I print the list using this code, it prints blank, when aa, bb is expected.
package main
import "fmt"
type Node struct {
data string
nextNode *Node
}
type MyLinkedList struct {
head *Node
}
func (ll MyLinkedList) pushFront(data string) *Node {
node := Node{data, nil}
if ll.head == nil {
ll.head = &node
return ll.head
}
node.nextNode = ll.head
ll.head = &node
return ll.head
}
func print(ll MyLinkedList) {
currentNode := ll.head
for currentNode != nil {
fmt.Print(currentNode.data + " ")
currentNode = currentNode.nextNode
}
}
func main() {
var m = MyLinkedList{}
m.pushFront("aa")
m.pushFront("bb")
print(m)
}