更新时间:2023-02-14 23:28:58
仅出于副作用导入软件包.
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/math"
import _ "lib/math"
对于 go-sqlite3 ,下划线import用于将sqlite3
驱动程序注册为init()
函数中的数据库驱动程序的副作用,而无需导入任何其他功能:
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")