简短变量声明

变量声明(declaration)的通用公式应该如下:

var name type = expression

type =expression 可以二选一。

func init()init()
init()
// main.go
// 步骤 1:导入主要的 SQL 包
import "database/sql"

// 步骤 2:导入一个驱动包来明确要使用的 SQL 数据库
import _ "github.com/mattn/go-sqlite3"

// 步骤 3:用一个注册好的驱动名称来打开一个数据库
func main() {
  // ...
  db, err := sql.Open("sqlite3", "database.db")
  // ...
}
// ---
// go-sqlite3
func init() {
    sql.Register("sqlite3", &SQLiteDriver{})
}
sql.goRegisterdriver.Driver
var (
    driversMu sync.RWMutex
    drivers   = make(map[string]driver.Driver)
)

// Register makes a database driver available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, driver driver.Driver) {
    driversMu.Lock()
    defer driversMu.Unlock()
    if driver == nil {
        panic("sql: Register driver is nil")
    }
    if _, dup := drivers[name]; dup {
        panic("sql: Register called twice for driver " + name)
    }
    drivers[name] = driver
}
sql.OpendriversDBsql.Drivers
编译时import运行时