0

I have seen an import clause import _ "lib/math" in go Specification.

The Specification states "To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name"

Why would import the package for initialization, if the package cant not be used since the package name is a blank identifier. what exactly is it used for?

1 Answers1

1

This pattern is useful for controlling the timing and order of imports. For example, you could want to force a slow initialization to happen during the program's startup, in an expected place where you don't care about speed, and then have later parts of the program import the package and use it without worrying about hanging due to the package's initialization.

Mureinik
  • 297,002
  • 52
  • 306
  • 350