import a package with a blank identifier and an alias in go

To import a package with a blank identifier and an alias in Go, you can use the following syntax:

main.go
import (
    . "pkg" // the . before the package name indicates a blank identifier
    p "pkg" // p is the alias for the imported package
)
140 chars
5 lines

The first import statement with the . before "pkg" indicates a blank identifier, which means that the package will be imported but not directly referenced in the code. This is useful if you want to import a package solely for its side effects, such as registering HTTP handlers.

The second import statement with the alias "p" allows you to refer to the package with a different name in your code. This can be useful if you want to avoid naming conflicts with other packages or if the original package name is too long or difficult to remember.

gistlibby LogSnag