douyang5943 2018-11-21 01:40
浏览 280

Go Modules-目录和包的命名约定

I understand Go Modules are yet an experimental opt-in feature, and perhaps because of that, I cannot find clear guidance on how to name directories and packages.
In these Package names in Go Blog post and Package name in Effective Go, they talk about that the directory should match the package name - but I wasn't certain if Go Modules would follow the same pattern.

If I want to bundle my business logic in package business with many files, is it reasonable to create subdirectory validators/ and keep the same package name package business?

someDir
├── business
│   ├── businessA.go // package business
│   ├── businessB.go // package business
│   ├── businessC.go // package business
│   ├── go.mod      // module example.com/business
│   └── validators
│       ├── businessValidatorX.go // package business, or validators?
│       ├── businessValidatorY.go // package business, or validators?
│       └── businessValidatorZ.go // package business, or validators?
└── main.go

Approach 1.

If I were to go with the same package name:

// main.go
package main

import (
        "example.com/business"
        "example.com/business/validators"
)

// both imports would be combined to the same `business` package?
func main() {
        b := business.SomeLogic()
        business.ValidateX(b) // validator from the same package
}

This looks to be prone to export conflict - but it is simple.

Approach 2.

If the validators/ path maps to package validators, the consuming code would look like the below instead.

// main.go
package main

import (
        "example.com/business"
        "example.com/business/validators"
)

func main() {
        b := business.SomeLogic()
        validators.ValidateX(b) // validator from a separate package
}

How should I manage a package consisted of many files? Is the Approach 1. reasonable, although it somewhat contradicts from the blog post and doc above?
Or should I go with Approach 2., complying with the convention, and add an alias as necessary in main.go?

  • 写回答

1条回答 默认 最新

  • dsfsad089111 2018-11-22 22:59
    关注

    Approach 2 is correct.

    Being a Go newbie, I had mistakenly thought that Approach 1 was one possible way, as Go does allow having package name different from directory name.

    As Volker helped in the comments, Approach 1 is definitely not possible.
    You will get a straight compilation error when trying to combine packages.

    compilation error

    The introduction of Go Modules does not affect the best practices outlined in existing documents, such as:

    As a side note, I also came to know that package name should be singular form.

    So that would leave me with the following structure along with Approach 2:

    structure following the convention

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题