代码改变世界

golang package和module使用

  youxin  阅读(147)  评论(0编辑  收藏  举报



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/





相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2014-04-02 yii分页
2014-04-02 MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途
2013-04-02 processing ball gravity simulation
2013-04-02 processing bounce ball
2012-04-02 'GetDc' : undeclared identifier
点击右上角即可分享
微信分享提示