Junior — Middle
Do you have experience working with linked lists in projects?
sobes.tech AI
Answer from AI
Yes, in Go projects, linked lists are often implemented and used, especially when dynamic management of collections with frequent insertions and deletions is required.
The standard library in Go includes the container/list package, which implements a doubly linked list. Example of usage:
package main
import (
"container/list"
"fmt"
)
func main() {
l := list.New()
l.PushBack(1)
l.PushBack(2)
l.PushFront(0)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
If you need to implement a linked list yourself, you can define a node structure and methods for insertion, deletion, and traversal:
type Node struct {
value int
next *Node
}
type LinkedList struct {
head *Node
}
func (l *LinkedList) Insert(value int) {
newNode := &Node{value: value}
if l.head == nil {
l.head = newNode
return
}
current := l.head
for current.next != nil {
current = current.next
}
current.next = newNode
}
func (l *LinkedList) Traverse() {
current := l.head
for current != nil {
fmt.Println(current.value)
current = current.next
}
}
This approach allows flexible management of list elements, but in most cases, for convenience and reliability, it's better to use the ready-made container/list.