What are slices and how do they differ from arrays?

Beginner

Answer

Arrays have fixed size, while slices are dynamic:

// Array - fixed size
var arr [5]int = [5]int{1, 2, 3, 4, 5}

// Slice - dynamic size
var slice []int = []int{1, 2, 3, 4, 5}
slice = append(slice, 6) // Can grow

// Creating slices
slice1 := make([]int, 5)      // length 5, capacity 5
slice2 := make([]int, 5, 10)  // length 5, capacity 10

Slices are more commonly used because of their flexibility and built-in functions like append().