duanhan7001 2015-07-04 08:03
浏览 87
已采纳

转到库包名称

I have some questions on package naming for external Go libraries.

I am interested if using generic names like "text" is considered a good practice? Having in mind that I cannot declare a "nested package" and that the library I am building deals with text processing, is it ok to have the package named "text" or should I stick to the library name as a package name too?

I am building a set of libraries (different projects) and I want to combine them under the same package. Is this also problematic? I am new to the Go community and am still not sure if package pollution is a problem or not (I do not see a problem as long as I import few packages in my code).

  • 写回答

1条回答 默认 最新

  • douge3492 2015-07-04 08:11
    关注

    The reference on that naming topic is "blog: Package names"

    It includes:

    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.

    Check also your package publishing practice, as it will help disambiguate your "text" package from others.

    As illustrated in "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.


    As mentioned in the comments 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题