Menu
Coddy logo textTech

The Blank Identifier `_`

Part of the Logic & Flow section of Coddy's GO journey — lesson 55 of 68.

Sometimes you need to import a package not to use its exported functions or variables, but to trigger its side effects. The most common side effect is running the package's init function, which executes automatically when the package is imported.

Go provides the blank identifier _ for this purpose. When you import a package with the blank identifier, Go imports the package and runs any initialization code, but doesn't require you to use any of its exported identifiers.

import _ "database/sql/driver"

This is particularly useful when you need a package to register itself with another system or perform setup tasks, but you don't directly call any of its functions in your code. Without the blank identifier, Go would give you a compilation error for importing an unused package.

The blank identifier tells Go: "Import this package for its side effects, but I won't be referencing it directly in my code." This keeps your imports clean while still getting the initialization behavior you need.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

Cheat sheet

Use the blank identifier _ to import packages for their side effects without using their exported functions:

import _ "database/sql/driver"

This imports the package and runs its initialization code without requiring you to use any of its exported identifiers. Useful when packages need to register themselves with other systems or perform setup tasks.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow