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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors6Idiomatic Go: Sets
The Set Idiom in GoCreating a SetAdding to a SetChecking for MembershipRemoving from a SetIterating Over a SetRecap - Unique Usernames9Packages and Scope
What is a Package?Exported vs UnexportedCreating a Simple PackagePackage AliasingThe Blank Identifier `_`The `init` functionRecap - Building a Utility