Welcome To Golang By Example

Blank Identifier in import in Go (Golang)

Blank identifier in importing packages means specifying a blank import for the imported package. The syntax for it is

import _ 

What is this blank import and why it is used. For this you have to understand two things

  1. About init function
  2. About blank identifier represented by an underscore (‘_‘)

init() function

init() function is a special function that is used to initialize the global variables of a package. These functions are executed when the package is initialized. Each of the GO source files in a package can have its own init() function. Whenever you import any package in the program, then on the execution of that program, init functions(if present)  in the GO source files belonging to that imported package are called first. Some points to note about init function

About blank identifier

go doesn’t allow any unused variable. Any unused variable can be replaced by a blank identifier (‘_’) .
So now a blank import of a package is used when

So basically a blank import is used when a package is solely imported for its side effects.

As an example mysql package is used as blank import for its side-effect of registering the mysql driver as a database driver in the init()  function of mysql package, without importing any other functions:

_ "github.com/go-sql-driver/mysql"

For using any of the mysql library such as gorm or xorm above blank import is necessary