当前位置:网站首页>go 包的使用
go 包的使用
2022-07-02 03:45:00 【UPythonFish】
go 包的使用
简介
在工程化的Go语言开发项目中,Go语言的源码复用是建立在包(package)基础之上的。包是多个Go源码的集合,是一种高级的代码复用方案,Go语言为我们提供了很多内置包,如fmt、os、io等。
定义包
包能够便于我们组织代码,将同一个类型代码写在一个包下,能够便于管理。一个包可以简单的理解为一个存放 .go 文件的文件夹,该文件夹下所有的go文件,都需要在代码的第一行添加如下代码,声明该文件归属于那个包。
package 包名
注意事项:
- 一个文件夹下面只能有一个包,同样一个包的文件不能在多个文件夹下。
- 包名可以不和文件夹的名字一样,包名不能包含
-符号。 - 包名为main的包为应用程序的入口包,编译时不包含main包的源代码时不会得到可执行文件。
可见性
如果想在一个包中引用另外一个包里的标识符(如变量、常量、类型、函数等)时,该标识符必须是对外可见的(public)。在Go语言中只需要将标识符的首字母大写就可以让标识符对外可见了。
package test
import "fmt"
var a = 100 // 首字母小写,外部包不可见,只能在包内使用
const Mode = 200 // 首字母大写,可在其他包中使用
func main() {
}
func Test(){
fmt.Println("可在外部包中使用")
}
func test() {
var Age int = 18
fmt.Println(Age) // 函数局部变量,外部包不可见,只能在当前函数类使用
fmt.Println("只能在包内使用")
}
结构体中的字段名和接口中的方法名如果首字母都是大写,外部包可以访问这些字段和方法。例如:
type Student struct {
Name string //可在包外访问的方法
class string //仅限包内访问的字段
}
type Payer interface {
init() //仅限包内访问的方法
Pay() //可在包外访问的方法
}
包的导入
在代码中引用其他包的内容,需要使用import 关键字导入使用的包。具体语法如下:
import "包的路径"
import "day02/package1" // 单行导入
import ( // 多行导入
"day02/test"
"fmt"
)
注意事项:
- import导入语句通常放在文件开头包声明语句的下面。
- 导入的包名需要使用双引号包裹起来。
- 推荐使用go model 管理包,go path是从
$GOPATH/src/后开始计算的,使用/进行路径分隔。 - Go语言中禁止循环导入包。
- 包名和包所在的文件夹可以不一样,但是建议不要修改,采用 包名即文件名即可
- 使用导入包内的标识符,应该以 包名.标识符 方式使用
// 建一个文件夹为test,test下创建s2文件
// package test // 默认包名同文件夹名一样为 test
package t1
const Mode = 200 // 首字母大写,可在其他包中使用
// 在main包中使用如下
package main
import (
"day02/test" // 包的路径
"fmt"
)
func main() {
fmt.Println(t1.Mode) // 包名.标识符
}
main 函数和 main 包
所有可执行的 Go 程序都必须包含一个 main 函数。这个函数是程序运行的入口。main 函数应该放置于 main 包中。
一个项目中,只允许存在一个main函数,只存在一个入口,平时运行单个go文件,需要以文件(file) 的形式运行。
package main
func main() {
}
init 函数
在Go语言程序执行时导入包语句会自动触发包内部
init()函数的调用。需要注意的是:init()函数没有参数也没有返回值。init()函数在程序运行时自动被调用执行,不能在代码中主动调用它。
func init(){ // 感觉和python中__init__差不多的作用
}
init 函数可用于执行初始化任务,也可用于在开始执行之前验证程序的正确性。
包的初始化顺序如下:
- 首先初始化包级别(Package Level)的变量
- 紧接着调用 init 函数。包可以有多个 init 函数(在一个文件或分布于多个文件中),它们按照编译器解析它们的顺序进行调用。
如果一个包导入了另一个包,会先初始化被导入的包。
尽管一个包可能会被导入多次,但是它只会被初始化一次。
Go编译器由此构建出一个树状的包引用关系,再根据引用顺序决定编译顺序,依次编译这些包的代码。在运行时,最后导入的包会最先初始化并调用其
init()函数,如下图示:

go path 转 go moduls
go 在 1.11后才支持go moduls模式,也是现在推荐的包管理模式
如果使用 go path 模式进行开发,所有项目代码都必须放在 go path 路径下的 src 文件下,否则就找不到,包括内置包,第三包,因此及其不方便。
但是,不可避免之前一些老项目使用go path 管理包,因此 go path 转 go moduls 十分有必要。
使用 go moduls,代码不需要放在src 文件夹下,任何路径都可以,但是项目下必须要有个 go.mod 文件夹,生产命令如下:
go mod init 项目名 // go path 转 go mod 执行即可 生成
// 如果有第三方包,需要再执行一下
go mod tidy // 会去下载第三方包
边栏推荐
- "Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
- [personal notes] PHP common functions - custom functions
- Eight steps of agile development process
- 《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
- The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
- How about Ping An lifetime cancer insurance?
- 初识string+简单用法(二)
- Failed to upgrade schema, error: “file does not exist
- 【DesignMode】原型模式(prototype pattern)
猜你喜欢
![[designmode] Prototype Pattern](/img/ee/c4e48c2ce8ff66f50f0bf13e5a0418.png)
[designmode] Prototype Pattern

Exchange rate query interface

The 6th Blue Bridge Cup single chip microcomputer provincial competition
![[ibdfe] matlab simulation of frequency domain equalization based on ibdfe](/img/a1/441f400668e736b70cb36443f2267a.png)
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe

In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews

The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition

Pycharm2021 delete the package warehouse list you added

软件测试人的第一个实战项目:web端(视频教程+文档+用例库)

u本位合约爆仓清算解决方案建议

【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
随机推荐
Pycharm2021 delete the package warehouse list you added
蓝桥杯单片机数码管技巧
regular expression
Blue Bridge Cup SCM digital tube skills
[mv-3d] - multi view 3D target detection network
《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
How should the team choose the feature branch development mode or trunk development mode?
MD5 of Oracle
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
Suggestions on settlement solution of u standard contract position explosion
Kotlin基础学习 17
JVM知识点
NLog使用
Oracle common SQL
MySQL index, transaction and storage engine
js生成随机数
What do you know about stock selling skills and principles
High performance and low power cortex-a53 core board | i.mx8m Mini