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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。