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-11-20MongoDB教程:从入门到实践详解
- 2024-11-17执行 Google Ads API 查询后返回的是空数组什么原因?-icode9专业技术文章分享
- 2024-11-17google广告数据不同经理账户下的凭证可以获取对方的api数据吗?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么实现同时向多个邮箱发送邮件?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么设置header 和 标签tag 呢?-icode9专业技术文章分享
- 2024-11-12Cargo deny安装指路
- 2024-11-02MongoDB项目实战:从入门到初级应用
- 2024-11-01随时随地一键转录,Google Cloud 新模型 Chirp 2 让语音识别更上一层楼
- 2024-10-25Google Cloud动手实验详解:如何在Cloud Run上开发无服务器应用
- 2024-10-24AI ?先驱齐聚 BAAI 2024,发布大规模语言、多模态、具身、生物计算以及 FlagOpen 2.0 等 AI 模型创新成果。