Slices and maps

Knowledge Videos

Short animated explainers, organised by subject.

Slices and maps

A slice is a growing list of one type (append, len, range) and a map connects keys to values (delete, and the comma ok check).

Try it

Transcript

A slice is a growing list of values, all of the same type. You write the type with square brackets in front: slice of string. Append adds an item and gives back the longer slice, and len tells you how many items there are.

Range loops over a slice. On each turn it gives you the index, which is the position, and the value.

A map connects keys to values, like a labelled drawer. A map from string to int could keep scores. You read and write with square brackets, and delete removes a key.

If you read a key that is not there, you get the zero value. To check whether the key exists, use the comma ok form: the second value is true or false.

Remember: a slice is a growing list, a map connects keys to values, and comma ok tells you if a key exists.

More in this series