How to iterate over the elements of a linked list in Go?

In Go, iterating over elements of a linked list involves traversing through each node and accessing its value. Here's an example of how you can iterate over a linked list in Go:

package main import "fmt" type Node struct { value int next *Node } func main() { // Create a linked list head := &Node{value: 1} second := &Node{value: 2} third := &Node{value: 3} head.next = second second.next = third // Iterate over the linked list current := head for current != nil { fmt.Println(current.value) current = current.next } }

In this example, we create a simple linked list with three nodes, where each node has a value and a next pointer to the next node. We start the iteration by assigning the head node to the current variable. Then, we use a for loop to keep traversing the list until the current node is nil (end of the list). Inside the loop, we print the value of the current node and update the current variable to the next node in the list.