go mysql init_以注册mysql驱动举例init()函数的注册行为(golang)
发布日期:2021-08-20 00:48:37 浏览次数:3 分类:技术文章

本文共 1435 字,大约阅读时间需要 4 分钟。

​ Go语言中的database/sql包提供了保证SQL或类SQL数据库的泛用接口,并不提供具体的数据库驱动。使用database/sql包时必须注入(至少)一个数据库驱动。

​ 但是import _ "github.com/go-sql-driver/mysql" 的意义不是很好理解。

​ 为此,我通过自己写的三个简单程序,演示init()的注册行为。

代码的目录组织结构如下:

awesomeProject/talkischeap/main.go //主函数

awesomeProject/talkischeap/showmecode/main.go // 类型定义函数

awesomeProject/talkischeap/showyourcode/main.go //init()函数

主函数

package main

import "awesomeProject/talkischeap/showmecode" //类比database/sql

import _ "awesomeProject/talkischeap/showyourcode" //类比github.com/go-sql-driver/mysql

//import _ "github.com/go-sql-driver/mysql"

//import "database/sql"

func main() {

// 程序运行的顺序 (1)引入的包 (2) 当前包中的常量 (3) 当前包中的变量(4)当前包的init (5)main函数

//本例解释如下

//1. 导入showmecode包时,导入了showmecode的包中的变量driver,这是个引用类型。

//2. 导入showyourcode包时,导入了init函数,init函数调用了showmecode中的Register函数。

//3.driver被赋值。影响了全局的driver。

//4.main函数中调用showmecode.printDriver()函数,把driver的内容打印出来.

//直接打印driver的值

showmecode.PrintDriver()

}

运行结果如下:

map[Driver : yes,It's it!]

showmecode代码包

package showmecode

import "fmt"

//声明map类型数据driver

var driver = make(map[string]string)

//注册函数: 注册driver

func Register(name string, value string) {

driver[name] = value

}

//打印函数: 打印driver

func PrintDriver() {

fmt.Println(driver)

}

showyourcode代码包

package showyourcode

import (

"awesomeProject/talkischeap/showmecode"

)

//init()当作注册使用,调用了showcode的代码

func init() {

showmecode.Register("Driver "," yes,It's it!")

}

​ 简单梳理了init函数的注册行为,但是mysql的驱动注册并没有那么简单,只是抛砖引玉,后续深入理解后再做详细说明。

参考连接:

转载地址:https://blog.csdn.net/weixin_31891047/article/details/113385320 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mysql libs 5.1.71_用python创建数据库监控平台(1)安装MySQL5.7
下一篇:mysql性能检测常用命令_2020-08-14 MySQL查看数据库性能常用命令

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年12月30日 00时09分08秒