当前位置:网站首页>GO语言-init()函数-包初始化
GO语言-init()函数-包初始化
2022-06-24 13:03:00 【一边学习一边哭】
init函数和主函数的异同
- init() main()都是go语言中的保留函数。由go程序自动调用,不可以手动调用。
- init()函数的定义和主函数类似,不能有参数和返回值。init()函数会在主函数执行前执行。
- init()函数可以定义在任意包中,且可以重复定义多个。主函数只能在main包下,且只能有一个。
- 一个package可以被其他的包导入多次,但是初始化只能执行一次。
我们可以在代码中定义init()函数。此函数会在包被导入时执行。
例如。如果实在main中导入包,包中存在init()函数,那么init()中的代码会在main()函数执行前执行,用于初始化包所需要的特定资料。
init()函数执行顺序
- 在同一个包中,init()函数时从上到下运行。
- 在同一个package的不同go文件中,init()函数是根据go文件名的字符串进行排序。
- 不同的package中,如果不互相依赖的话,根据import顺序执行init()函数。
- 如果package存在依赖,init()函数调用顺序为:最后被依赖的包,最先执行。
边栏推荐
- 数据库注意事项
- Jerry's serial port receiving IO needs to set the digital function [chapter]
- v-if 和 v-show 的区别
- unity 等高线创建方法
- Harmony os. (2)
- [deep learning] storage form of nchw, nhwc and chwn format data
- SaaS management system solution of smart Park: enabling the park to realize information and digital management
- JS remove string spaces
- 鲲鹏arm服务器编译安装PaddlePaddle
- Jerry has opened a variety of decoding formats, and the waiting time from card insertion to playback is long [chapter]
猜你喜欢
随机推荐
Return to new list
[untitled]
[learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
远程办公之:在家露营办公小工具| 社区征文
Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)
在CVS中恢复到早期版本
Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
Kotlin shared mutable state and concurrency
智源社区周刊#86:Gary Marcus谈大模型研究可借鉴的三个语言学因素;谷歌提出媲美Imgen的文生图模型Parti;OpenAI提出视频预训练模型VPT,可玩MC游戏
Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
Jerry's seamless looping [chapter]
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
JS remove string spaces
2022 construction elevator driver (construction special type of work) examination questions and online simulation examination
[R language data science] (XIV): random variables and basic statistics
Record various sets of and or of mongotemplate once
【深度学习】NCHW、NHWC和CHWN格式数据的存储形式
Gatling performance test
SAP Marketing Cloud 功能概述(三)
Overview of SAP marketing cloud functions (III)





![Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]](/img/4e/de0951c8be5ddd765b15b773c04fa7.png)


