Code of the Day
IntermediatePackages & testing

Lab: packages, modules, and testing

Scenario questions covering package visibility, module versions, table test structure, and benchmark interpretation.

Lab · optionalGoIntermediate15 min
Recommended first
By the end of this lesson you will be able to:
  • Identify which identifiers are exported from a package
  • Reason about module version resolution under MVS
  • Evaluate the structure of a table-driven test
  • Interpret benchmark output correctly

Optional lab. These scenarios reinforce the Packages and testing module. Read each question carefully — some have subtleties that reward close attention.

Package visibility

Knowledge check

  1. 1.
    In a package named 'config', you define:

    type settings struct { Host string; port int }

    Which statement is true from a different package?
  2. 2.
    An init() function can return an error to signal that package initialisation failed.
  3. 3.
    Your module is github.com/acme/app. You place a package at github.com/acme/app/internal/auth. Which of the following can import it?

Module versions and MVS

Knowledge check

  1. 1.
    Your module requires library v1.3.0. Another dependency of yours requires the same library at v1.5.0. Which version does Go use?
  2. 2.
    You add a replace directive to go.mod pointing a dependency to a local directory. When should you remove it?

Table test structure

Knowledge check

  1. 1.
    You have a table-driven test named TestParse with subtests 'empty', 'valid', and 'invalid'. Which command runs only the 'valid' subtest?
  2. 2.
    You should call t.Parallel() in subtests that modify a shared global variable.

Benchmark interpretation

Here is sample benchmark output:

BenchmarkJSON-8         500000    2340 ns/op    896 B/op    12 allocs/op
BenchmarkJSONFast-8    2000000     620 ns/op    128 B/op     2 allocs/op

Knowledge check

  1. 1.
    Looking at the output above, what does 12 allocs/op mean for BenchmarkJSON?
  2. 2.
    Based on the output, BenchmarkJSONFast is approximately how many times faster than BenchmarkJSON?

Where to go next

You've completed the Intermediate tier. The Advanced tier begins with Go's most distinctive feature — concurrency — starting with goroutines and channels.

Finished reading? Mark it complete to track your progress.

On this page