網站首頁 編程語言 正文
init函數會在main函數執行之前進行執行、init用在設置包、初始化變量或者其他要在程序運行前優先完成的引導工作。
舉例:在進行數據庫注冊驅動的時候。
這里有init函數
package postgres
package postgres import ( "database/sql" "database/sql/driver" "errors" ) // PostgresDriver provides our implementation for the // sql package. type PostgresDriver struct{} // Open provides a connection to the database. func (dr PostgresDriver) Open(string) (driver.Conn, error) { return nil, errors.New("Unimplemented") } var d *PostgresDriver // init is called prior to main. func init() { d = new(PostgresDriver) sql.Register("postgres", d) }
這里是main函數
// Sample program to show how to show you how to briefly work // with the sql package. package main import ( "database/sql" _ "github.com/goinaction/code/chapter3/dbdriver/postgres" ) // main is the entry point for the application. func main() { sql.Open("postgres", "mydb") }
可以看到這里main函數中使用看sql.Open 就是得益于上面的init函數
_ "github.com/goinaction/code/chapter3/dbdriver/postgres"
下劃線加上包名的作用就是,執行這個包的init函數。
原文鏈接:https://www.cnblogs.com/dcz2015/p/10103220.html
相關推薦
- 2022-04-12 用python繪制極坐標雷達圖_python
- 2022-06-29 Oracle數據庫之PL/SQL使用流程控制語句_oracle
- 2022-03-18 linux下修改文件權限chmod命令詳細解析_Linux
- 2022-04-30 C#操作DataGridView設置單元格只讀_C#教程
- 2022-09-24 Go?類型轉化工具庫cast函數詳解_Golang
- 2022-11-28 Android開發框架MVC-MVP-MVVM-MVI的演變Demo_Android
- 2022-05-31 C++核心編程之內存分區詳解_C 語言
- 2021-11-08 C++繼承模式詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支