5. Arrays and Slices

Almost every programming language has a data structure that it calls an array, but the exact semantics vary considerably. In some languages, an array is a dynamically resizeable ordered collection of objects. In others, it’s a block of memory, with some vague hint that it probably contains variables of a specific type.

In Go, arrays are a very low-level data structure. Like C arrays, they are simply blocks of memory, but there are some important differences. Remember that Go does not permit pointer arithmetic. In C, array subscripting is just another way of writing pointer arithmetic, and you can use array and pointer types almost interchangeably.

In Go, pointers and arrays are distinct types. Because arrays and pointers ...

Get The Go Programming Language Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.