Go库包名
Go library package names
我对外部 Go 库的包命名有一些疑问。
我想知道使用像“text
”这样的通用名称是否被认为是一种好的做法?考虑到我不能声明 "nested package" 并且我正在构建的库处理文本处理,可以将包命名为“text
”还是我应该坚持库名称作为包名也是?
我正在构建一组库(不同的项目),我想将它们组合在同一个包下。这也有问题吗?我是 Go 社区的新手,仍然不确定包污染是否是一个问题(只要我在我的代码中导入几个包,我就没有发现问题)。
关于该命名主题的引用是“blog: Package names”
它包括:
Avoid unnecessary package name collisions.
While packages in different directories may have the same name, packages that are frequently used together should have distinct names. This reduces confusion and the need for local renaming in client code. For the same reason, avoid using the same name as popular standard packages like io or http.
还要检查您的 package publishing practice,因为这将有助于消除您的“text
”包与其他包的歧义。
如“An Introduction to Programming in Go / Packages”所示:
math
is the name of a package that is part of Go's standard distribution, but since Go packages can be hierarchical we are safe to use the same name for our package. (The real math package is just math, ours is golang-book/chapter11/math
)
When we import our math
library we use its full name (import "golang-book/chapter11/math"
), but inside of the math.go
file we only use the last part of the name (package math
).
We also only use the short name math when we reference functions from our library. If we wanted to use both libraries in the same program Go allows us to use an alias:
import m "golang-book/chapter11/math"
func main() {
xs := []float64{1,2,3,4}
avg := m.Average(xs)
fmt.Println(avg)
}
m
is the alias.
如 by elithrar, Dave Cheney has some additional tips:
In other languages it is quite common to ensure your package has a unique namespace by prefixing it with your company name, say com.sun.misc.Unsafe
.
If everyone only writes packages corresponding to domains that they control, then there is little possibility of a collision.
In Go, the convention is to include the location of the source code in the package’s import path, ie
$GOPATH/src/github.com/golang/glog
This is not required by the language, it is just a feature of go get
.
我对外部 Go 库的包命名有一些疑问。
我想知道使用像“text
”这样的通用名称是否被认为是一种好的做法?考虑到我不能声明 "nested package" 并且我正在构建的库处理文本处理,可以将包命名为“text
”还是我应该坚持库名称作为包名也是?
我正在构建一组库(不同的项目),我想将它们组合在同一个包下。这也有问题吗?我是 Go 社区的新手,仍然不确定包污染是否是一个问题(只要我在我的代码中导入几个包,我就没有发现问题)。
关于该命名主题的引用是“blog: Package names”
它包括:
Avoid unnecessary package name collisions.
While packages in different directories may have the same name, packages that are frequently used together should have distinct names. This reduces confusion and the need for local renaming in client code. For the same reason, avoid using the same name as popular standard packages like io or http.
还要检查您的 package publishing practice,因为这将有助于消除您的“text
”包与其他包的歧义。
如“An Introduction to Programming in Go / Packages”所示:
math
is the name of a package that is part of Go's standard distribution, but since Go packages can be hierarchical we are safe to use the same name for our package. (The real math package is just math, ours isgolang-book/chapter11/math
)When we import our
math
library we use its full name (import "golang-book/chapter11/math"
), but inside of themath.go
file we only use the last part of the name (package math
).We also only use the short name math when we reference functions from our library. If we wanted to use both libraries in the same program Go allows us to use an alias:
import m "golang-book/chapter11/math"
func main() {
xs := []float64{1,2,3,4}
avg := m.Average(xs)
fmt.Println(avg)
}
m
is the alias.
如
In other languages it is quite common to ensure your package has a unique namespace by prefixing it with your company name, say
com.sun.misc.Unsafe
.
If everyone only writes packages corresponding to domains that they control, then there is little possibility of a collision.In Go, the convention is to include the location of the source code in the package’s import path, ie
$GOPATH/src/github.com/golang/glog
This is not required by the language, it is just a feature of
go get
.