Go Programming Fundamentals · Concurrency & Idiomatic Go

Testing, Modules, and Tooling

Verify behavior and manage dependencies

Testing, Modules, and Tooling

Go's built-in testing package requires no external framework. Test files end in _test.go, and test functions start with Test and take *testing.T.

func TestDivide(t *testing.T) {
    got, err := divide(10, 2)
    if err != nil || got != 5 {
        t.Errorf("divide(10, 2) = %v, %v; want 5, nil", got, err)
    }
}

Run tests with go test ./... to check every package in the module.

Modules

Go modules (go.mod/go.sum) track dependency versions so builds are reproducible. go get <package>@version adds or upgrades a dependency, and go mod tidy removes unused ones.

Practice questions

What naming conventions does Go's testing package require for a test to be picked up?

Answer

Summary

The file must end in _test.go, and the function must start with Test and take a single *testing.T parameter — go test finds and runs it automatically.

Longer Answer

The file must end in _test.go, and the function must start with Test and take a single *testing.T parameter — go test finds and runs it automatically. This convention-over-configuration approach is why Go testing needs no external framework to get started. In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview.

Key Points

  • The file must end in _test.go, and the function must start with Test and take a single *testing.T parameter — go test finds and runs it automatically
  • This convention-over-configuration approach is why Go testing needs no external framework to get started
  • In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview
  • Use the answer to connect the concept back to a realistic production or interview scenario for 'What naming conventions does Go's testing package require for a test to be picked up?'

Comparison / Tradeoffs

Aspect Notes
Core idea The file must end in _test.go, and the function must start with Test and take a single *testing.T parameter — go test finds and runs it automatically.
Practical tradeoff This convention-over-configuration approach is why Go testing needs no external framework to get started.

Why

Summary

This convention-over-configuration approach is why Go testing needs no external framework to get started.

Longer Answer

This convention-over-configuration approach is why Go testing needs no external framework to get started. In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview.

Key Points

  • This convention-over-configuration approach is why Go testing needs no external framework to get started
  • In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview
  • In an interview, relate 'What naming conventions does Go's testing package require for a test to be picked up?' to implementation decisions, tradeoffs, and operational impact
  • Use the answer to connect the concept back to a realistic production or interview scenario for 'What naming conventions does Go's testing package require for a test to be picked up?'

Comparison / Tradeoffs

Aspect Notes
Core idea This convention-over-configuration approach is why Go testing needs no external framework to get started.
Practical tradeoff In an interview, relate 'What naming conventions does Go's testing package require for a test to be picked up?' to implementation decisions, tradeoffs, and operational impact.
What does `go mod tidy` do, and why would you run it after changing your imports?

Answer

Summary

It adds any missing dependencies your code now imports and removes ones that are no longer used, keeping go.mod/go.sum accurate.

Longer Answer

It adds any missing dependencies your code now imports and removes ones that are no longer used, keeping go.mod/go.sum accurate. Without running it, go.mod can drift out of sync with your actual import statements, which causes confusing build errors for collaborators. In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview.

Key Points

  • It adds any missing dependencies your code now imports and removes ones that are no longer used, keeping go.mod/go.sum accurate
  • Without running it, go.mod can drift out of sync with your actual import statements, which causes confusing build errors for collaborators
  • In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview
  • Use the answer to connect the concept back to a realistic production or interview scenario for 'What does go mod tidy do, and why would you run it after changing your imports?'

Comparison / Tradeoffs

Aspect Notes
Core idea It adds any missing dependencies your code now imports and removes ones that are no longer used, keeping go.mod/go.sum accurate.
Practical tradeoff Without running it, go.mod can drift out of sync with your actual import statements, which causes confusing build errors for collaborators.

Why

Summary

Without running it, go.mod can drift out of sync with your actual import statements, which causes confusing build errors for collaborators.

Longer Answer

Without running it, go.mod can drift out of sync with your actual import statements, which causes confusing build errors for collaborators. In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview.

Key Points

  • Without running it, go.mod can drift out of sync with your actual import statements, which causes confusing build errors for collaborators
  • In practice, explain where this shows up in production code, what tradeoffs matter, and how you would justify the choice in an interview
  • In an interview, relate 'What does go mod tidy do, and why would you run it after changing your imports?' to implementation decisions, tradeoffs, and operational impact
  • Use the answer to connect the concept back to a realistic production or interview scenario for 'What does go mod tidy do, and why would you run it after changing your imports?'

Comparison / Tradeoffs

Aspect Notes
Core idea Without running it, go.mod can drift out of sync with your actual import statements, which causes confusing build errors for collaborators.
Practical tradeoff In an interview, relate 'What does go mod tidy do, and why would you run it after changing your imports?' to implementation decisions, tradeoffs, and operational impact.

Open in the app to mark lessons complete, take notes and track your progress.