Go · Chapter 10 of 26

Packages and modules

By the end of this chapter you can read a go.mod, say what an import path points at, decide what a package shows other packages with a capital letter, split a package over several files, keep a package internal to your module, and use go vet, gofmt and go doc, with doc comments that go doc shows. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real programs
  • 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

BijliGo's backend is not one program. The ingest service takes the heartbeats and meter readings, billing works out what each of the 3,600 sessions of a Wednesday cost, and the status API tells the app which chargers are free. All three need to price energy the same way, to the paisa. So far every chapter has been one package main, and the only way to share a function between programs would be to copy it. Three copies of the rounding means three places to fix when Pooja changes a rule, and on the day one copy is missed, the app shows one price and the bill says another.

Go's answer is the package: code written once, in one folder, that any program imports. You have used one since chapter 6, bijligo/bijli. In this chapter you write one, package tariff, that every BijliGo service could import.

What you build: 4 tasks

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

  1. Task 1

    price.go and round.go: one package, two files

    This folder is package tariff, a library with no main: try your code with go vet . and go build . here, and the Check.

    In round.go, roundCost(wh, paisePerKWh int) int (lowercase, as given) returns wh times paisePerKWh divided by 1000, rounded to the nearest paisa (half a paisa up): roundCost(3064, 1400) is 4290.

    In price.go, Price(kind string, wh int) (int, error) prices wh watt-hours on a charger of that kind at the kind's PaisePerKWh from bijli.TariffFor, and must call roundCost to round. Price("DC60", 21250) is 51000, Price("LEV3", 1619) is 1457, Price("AC7", 3064) is 4290, Price("DC30", 17385) is 34770, each with a nil error. For a kind BijliGo does not have, Price("AC22", 5000) returns 0 and an error for which errors.Is(err, ErrUnknownKind) is true.

  2. Task 2

    chargers.go: built on bijli's exported API

    ChargersOfKind(stationID, kind string) ([]string, error) returns the ids of the station's chargers whose Kind is kind, in the order the station lists them, using bijli's exported functions. ChargersOfKind("ST235", "DC60") is [C2151 C2153 C2154 C2155], ChargersOfKind("ST012", "DC30") is [C1067 C1068 C1070], ChargersOfKind("ST001", "LEV3") is [C1007 C1008], and ChargersOfKind("ST009", "DC60") has no ids (an empty or nil slice), all with a nil error.

    For a station BijliGo does not have, ChargersOfKind("ST999", "DC60") returns no ids and an error for which errors.Is(err, ErrUnknownStation) is true.

  3. Task 3

    kinds.go: doc comments that go doc shows

    Kinds() []string returns the four kinds of charger, cheapest kWh first: [LEV3 AC7 DC30 DC60] (work it out from bijli.Tariffs(), whose PaisePerKWh are 900, 1400, 2000 and 2400).

    In kinds.go, also write a package doc comment: a comment right above the line package tariff, with no blank line between them, starting with "Package tariff " and saying what the package does. And a doc comment right above func Kinds, with no blank line between, starting with "Kinds " and saying what it returns. The task's own comment at the top of the file stays where it is, separated by a blank line. go doc . and go doc . Kinds in this folder show your comments.

  4. Task 4 · interview question

    Interview: export.go, the function nobody else could call

    Tenzin's ingest service is another package. It imports this one and calls tariff.FormatKWh(17385), expecting "17.385 kWh", and it does not compile. The function in export.go already works: it turns watt-hours into kWh with three decimals (5333 gives "5.333 kWh"). Make it usable from other packages under the name FormatKWh, doing exactly the same.

    The Check builds a package of its own that imports yours and calls FormatKWh(17385), FormatKWh(5333) and FormatKWh(900), expecting "17.385 kWh", "5.333 kWh" and "0.900 kWh".

    Joseph will ask: why can another package not call a function whose name starts with a small letter?

What you will learn

  • go.mod: the module, its path and its Go version.
  • Import paths: which folder bijligo/bijli is.
  • Exported names: a capital letter makes a name visible to other packages; a small letter keeps it inside.
  • A package over several files, package-level variables and init.
  • internal packages and the module cache (offline here).
  • go vet, gofmt and go doc, and doc comments.

More in Real programs

Build "Packages and modules" 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.