Arrays keep elements next to each other in memory. This means the computer can calculate where any element is quickly using its index. Linked lists store elements scattered in memory and use pointers to connect them, so you must follow links to reach an element, which takes more time.
package main import "fmt" func main() { arr := [5]int{0, 0, 0, 0, 0} arr[0] = 10 arr[3] = 20 fmt.Println(arr) }
The array starts with all zeros. Setting arr[0] = 10 changes the first element. Setting arr[3] = 20 changes the fourth element. The rest remain zero.
package main import "fmt" func main() { arr := [3]int{1, 2, 3} fmt.Println(arr[3]) }
Arrays in Go have fixed size. Indexes go from 0 to length-1. Accessing arr[3] when length is 3 is out of range and causes a runtime error.
Arrays provide fast access by index but are not good for frequent insertions or deletions because elements must be shifted. They also have fixed size, so dynamic resizing is costly. Complex relationships are better handled by other structures.
package main import "fmt" func main() { arr := [3]int{5, 10, 15} ptr := &arr[0] fmt.Println(*ptr) ptr = &arr[2] fmt.Println(*ptr) }
The pointer ptr first points to arr[0], which is 5, then to arr[2], which is 15. Printing *ptr shows these values.
