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.
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 .
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.
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.
Just use lowercase letters.
参考:https://blog.learngoprogramming.com/code-organization-tips-with-packages-d30de0d11f46
go中嵌套包
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
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
注意点:导入子包必须是子包的完全路径。 另外使用子包也是子包的名字
https://golangbyexample.com/nested-packages-golang/ https://www.cnblogs.com/dcz2015/p/10763254.html https://www.callicoder.com/golang-packages/