Complete the code to declare an array of integers with 5 elements in Go.
var numbers [[1]]intThe array size is specified inside the square brackets. Here, 5 means the array has 5 elements.
Complete the code to define a linked list node struct with an integer value and a pointer to the next node.
type Node struct {
value int
next *[1]
}The next field points to the next Node in the linked list, so its type is *Node.
Fix the error in the code to append a new node to the end of a linked list.
func appendNode(head *Node, val int) *Node {
newNode := &Node{value: val}
if head == nil {
return newNode
}
current := head
for current.next [1] nil {
current = current.next
}
current.next = newNode
return head
}The loop should continue while current.next is not nil to reach the last node.
Fill both blanks to create a slice from an array and iterate over it.
arr := [5]int{1, 2, 3, 4, 5} slice := arr[[1]:[2]] for i, v := range slice { fmt.Println(i, v) }
The slice starts at index 0 and goes up to but not including index 3, so it includes elements 1, 2, and 3.
Fill all three blanks to create a map from string to int with a condition.
words := []string{"go", "code", "list", "array"}
lengths := map[string]int{}
for _, word := range words {
if len(word) [1] [2] {
lengths[[3]] = len(word)
}
}This code adds words longer than 3 characters to the map with their lengths.
