更新时间:2023-02-14 22:43:44
它用于导入一个包,只是为了它的副作用.
It's for importing a package solely for its side-effects.
来自 Go 规范:
要仅为其副作用(初始化)导入包,请使用空白标识符作为显式包名称:
To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name:
导入_lib/数学"
对于 go-sqlite3,下划线import 用于在 init()
函数中将 sqlite3
驱动注册为数据库驱动的副作用,不导入任何其他函数:
In the case of go-sqlite3, the underscore import is used for the side-effect of registering the sqlite3
driver as a database driver in the init()
function, without importing any other functions:
sql.Register("sqlite3", &SQLiteDriver{})
一旦以这种方式注册,sqlite3 就可以在您的代码中与标准库的 sql
接口一起使用,如示例所示:
Once it's registered in this way, sqlite3 can be used with the standard library's sql
interface in your code like in the example:
db, err := sql.Open("sqlite3", "./foo.db")