Go · Chapter 6 of 26

Slices and maps

By the end of this chapter you can hold many values in a slice, grow it with append and say what its length and capacity are, go through it with range, count and look things up with a map, sort with the slices package, and explain the one trap every Go developer meets: two slices that share one array. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The language
  • 4 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go are free. This one comes with the all labs plan.

Why you need this

Pooja's Thursday questions are about all of Wednesday at once. How much energy did Koramangala Mall deliver? How many sessions did each charger have? Which five chargers were the busiest, so the maintenance team checks them first? Wednesday was 3,600 sessions on 1,180 chargers.

Everything so far held one value per variable. You cannot make 3,600 variables, and you cannot write a switch with 1,180 cases to find a charger's count. Go has two answers, and you will use them in almost every program from now on: a slice, an ordered list of values that can grow, and a map, which finds a value by its key (a charger's count by the charger's id) in one step.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    station.go: the energy of one station's sessions

    Write StationEnergy(sessions []bijli.Session, station string) int in station.go: it returns the total energy, in watt-hours, of the sessions at one station. Each session's Station field is its station's id and its EnergyWh field its energy. The sessions are in order of start time, so go through all of them.

    With bijli.Sessions(), StationEnergy returns 345154 for "ST001", 314175 for "ST240", 154354 for "ST007" and 0 for "ST999". StationEnergy(nil, "ST001") returns 0. The Check also tries five sessions of its own, two of them at ST001 (9217 and 4312 Wh): 13529.

  2. Task 2

    percharger.go: sessions per charger, as a map

    Write SessionsPerCharger(sessions []bijli.Session) map[string]int in percharger.go: it returns a map from each charger's id (a session's Charger field) to the number of the sessions that were on it. Only chargers that had a session are keys.

    With bijli.Sessions(), the map has 1159 keys (21 chargers had no session), and its values for C2054, C1042, C1001 and C2180 are 9, 7, 2 and 6. With five sessions of the Check's own (C1002 twice, C1042 once, C1051 twice) it has 3 keys: 2, 1 and 2. With no sessions (nil), it returns an empty map that is not nil, so a caller can add to it.

  3. Task 3

    busiest.go: the five busiest chargers, in order

    Write Busiest(sessions []bijli.Session, n int) []string in busiest.go: it returns the ids of the n chargers with the most sessions, the busiest first. Chargers with the same number of sessions go in order of their id. You may use SessionsPerCharger from task 2.

    With bijli.Sessions(), Busiest(sessions, 5) returns [C2054 C1481 C1625 C1736 C2097] (C2054 had 9 sessions, the others 8), Busiest(sessions, 1) returns [C2054], and Busiest(sessions, 8) returns [C2054 C1481 C1625 C1736 C2097 C2127 C2138 C2177]. Busiest(sessions, 0) returns an empty slice. When fewer than n chargers had sessions, it returns them all: for the Check's five sessions (C1002 twice, C1051 twice, C1042 once), Busiest with n = 10 returns [C1002 C1051 C1042].

  4. Task 4 · interview question

    Interview: fleet.go, the bill that changed the day

    FleetBill(costs []int, from, to int) []int in fleet.go returns the costs from index from up to (not including) to, each with a tenth taken off: the cost minus the cost divided by 10, in whole paise. With costs := []int{36874, 7466, 6037, 22121, 34770}, FleetBill(costs, 1, 3) returns [6720 5434], FleetBill(costs, 0, 5) returns [33187 6720 5434 19909 31293], and FleetBill(costs, 2, 2) returns an empty bill.

    The bill is right, but after Pooja ran it, the day's own costs had changed too. Fix FleetBill so that costs stays [36874 7466 6037 22121 34770] after every call, and so that changing or appending to the bill it returns never changes costs either.

    Joseph will ask: why did changing the bill change the day's costs, when the code never touched costs after slicing it?

What you will learn

  • Arrays: a fixed number of values, and why you rarely use them directly.
  • Slices: len, cap, append, and how append grows, measured.
  • Slicing shares the array: the trap, shown.
  • range with an index and a value, make and copy.
  • Maps: make, the zero value for a missing key, the comma ok form, delete, and a random order on purpose.
  • The slices and maps packages: Sort, SortFunc, Contains, Sorted(maps.Keys(m)).

More in The language

Build "Slices and maps" yourself

BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Go are free. This one comes with the all labs plan.