golang package和module使用
2022/4/3 0:03:48
本文主要是介绍golang package和module使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Go has no sub-packages
When you want to split your packages to organize your BIG package, you may make some of the internals of your package visible to the outside world, and so, anyone can import them. You may not want this.
Internal package convention prevents your packages to be imported from unwanted parties. I explain internal packages here in this post.
Put tests into the same directory
Do not put your testing code into separate test directories.
Keep them together like: miner.go and miner_test.go into the same directory, they live happily there together.
Put the data needed for testing into testdata directories as a sub-directory. Go tools will respect that convention.
miner/ miner.go miner_test.go testdata/ hashes.data .
Use common abbreviations
Only if they’re familiar to the programmers (or to the programmers in the domain of the application you’re building). For example: Instead of formattedIO, stdlib uses fmt.
Avoid generic package names
Not like this: api, models, common, utils, helper etc.
For example: Instead of a model package, define a package that does user things named as user. Or, define another package for a client order as order.
Do not put everything inside a models folder, split them into more smaller packages.
However, this goes to some extent. If your separate packages need some relationship between them, you may cluster them together depending on your goal.
Do not use underscores or camel-casing
Just use lowercase letters.
参考:https://blog.learngoprogramming.com/code-organization-tips-with-packages-d30de0d11f46
Nested Packages in Go
go中嵌套包
Example
Create a directory named learn. Let’s create a module with import path as “sample.com/learn” in the learn directory.
go mod init sample.com/learn
It will create a go.mod file
go.mod
module sameple.com/learn go 1.14
Let’s create below files and directories
- learn/main.go
- learn/math/math.go
- learn/math/advanced/advanced.go
You can see that advanced is a nested package inside the math package.
learn/math/math.go
package math func Add(a, b int) int { return a + b } func Subtract(a, b int) int { return a - b }
learn/math/advanced/advanced.go
package advanced func Square(a int) int { return a * a }
learn/main.go
package main import ( "fmt" "sample.com/learn/math" "sample.com/learn/math/advanced" ) func main() { fmt.Println(math.Add(2, 1)) fmt.Println(math.Subtract(2, 1)) fmt.Println(advanced.Square(2)) }
Let’s run this program
learn $ go install learn $ learn 3 1 4
Points to note about above program
- We imported the advanced package in main.go with full qualified path i.e, import “sample.com/learn/math/advanced”
- Square function is referred to using advanced package i.e, advanced.Square(2)
注意点:导入子包必须是子包的完全路径。 另外使用子包也是子包的名字
https://golangbyexample.com/nested-packages-golang/ https://www.cnblogs.com/dcz2015/p/10763254.html https://www.callicoder.com/golang-packages/
这篇关于golang package和module使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24MongoDB资料:新手入门完全指南
- 2024-12-20go-zero 框架的 RPC 服务 启动start和停止 底层是怎么实现的?-icode9专业技术文章分享
- 2024-12-19Go-Zero 框架的 RPC 服务启动和停止的基本机制和过程是怎么实现的?-icode9专业技术文章分享
- 2024-12-18怎么在golang中使用gRPC测试mock数据?-icode9专业技术文章分享
- 2024-12-15掌握PageRank算法核心!你离Google优化高手只差一步!
- 2024-12-15GORM 中的标签 gorm:"index"是什么?-icode9专业技术文章分享
- 2024-12-11怎么在 Go 语言中获取 Open vSwitch (OVS) 的桥接信息(Bridge)?-icode9专业技术文章分享
- 2024-12-11怎么用Go 语言的库来与 Open vSwitch 进行交互?-icode9专业技术文章分享
- 2024-12-11怎么在 go-zero 项目中发送阿里云短信?-icode9专业技术文章分享
- 2024-12-11怎么使用阿里云 Go SDK (alibaba-cloud-sdk-go) 发送短信?-icode9专业技术文章分享