How to structure a package during development?

huangapple go评论77阅读模式
英文:

How to structure a package during development?

问题

我想在github.com/mylogin/mypackage上发布一个包。为了开发它,我创建了以下文件结构:

mypackage/
├─ mypackage.go
├─ go.mod  // 错误地命名为mypackage.mod
├─ tests/
│  ├─ firsttest_test.go
│  ├─ secondtest_test.go

mypackage.go将有一个函数(mypackage(...))

go.mod目前是这样的:

module github.com/mylogin/mypackage

go 1.17

我的问题是:在编写测试时,我需要导入mypackage

  • 一方面,我不能导入github.com/mylogin/mypackage,因为它还不存在
  • 另一方面,我应该导入github.com/mylogin/mypackage,因为这最终是其他人(或操作)复制测试的方式

我该如何解决这个矛盾?我的文件布局应该是什么样的?

英文:

I would like to publish a package at github.com/mylogin/mypackage. In order to develop it, I created the following file structure

mypackage/
├─ mypackage.go
├─ go.mod  // used to be mypackage.mod by mistake
├─ tests/
│  ├─ firsttest_test.go
│  ├─ secondtest_test.go

mypackage.go will have one function (mypackage(...))

<del>mypackage.mod</del> go.mod is currently

module github.com/mylogin/mypackage

go 1.17

My question: when writing tests, I need to import mypackage:

  • on the one hand I cannot import github.com/mylogin/mypackage because it is not there yet
  • on the other hand I should however import github.com/mylogin/mypackage because it is ultimately the way for others (or Actions) to replicate the tests

How can I get off this contradiction? What should be the proper layout of my files?

答案1

得分: 4

> mypackage.mod目前是这样的

该文件必须被命名为go.mod。其他任何名称都将被Go工具链忽略。

> - 一方面,我无法导入github.com/mylogin/mypackage,因为它还不存在

这实际上并不重要。如果你有一个go.mod文件(参见上面关于更正文件名的说明),Go会首先考虑这个文件,而不是进行网络请求。这意味着它会在你的项目顶层找到go.mod文件,并意识到当前项目是该模块,并使用它自己。

英文:

> mypackage.mod is currently

The file must be called go.mod. Any other name will be ignored by the Go toolchain.

> - on the one hand I cannot import github.com/mylogin/mypackage because it is not there yet

That's not actually relevant. If you have a go.mod file (see above note about correcting the filename), Go knows to consider this first, before making network requests. That means that it will find the go.mod at the top level of your project, and realize that your current project is that module, and use itself.

答案2

得分: 2

> 一方面,我无法导入github.com/mylogin/mypackage,因为它还不存在。

你可以导入"github.com/mylogin/mypackage",因为它是同一个模块中的一个包。

如果由于某种原因无法工作,可能是go.mod文件中的module指令或_test.go文件中的import语句中有拼写错误。


话虽如此,通常github.com/mylogin/mypackage的测试应该位于与mypackage本身相同的目录中:

mypackage/
├─ go.mod
├─ mypackage.go
├─ firsttest_test.go
├─ secondtest_test.go

如果你想从外部用户的角度测试该包的API,可以让_test.go文件声明package mypackage_test而不是package mypackage

(详见go help test了解更多详情。)

英文:

> on the one hand I cannot import github.com/mylogin/mypackage because it is not there yet

You can import &quot;github.com/mylogin/mypackage&quot;, because it is a package in the same module.

If that isn't working for some reason, there may be a typo either in the module directive in your go.mod file or in the import statement in the _test.go file.


That said, typically the tests for github.com/mylogin/mypackage would be located in the same directory as mypackage itself:

mypackage/
├─ go.mod
├─ mypackage.go
├─ firsttest_test.go
├─ secondtest_test.go

If you want to test the package's API from the perspective of an outside user, you can have the _test.go file declare package mypackage_test instead of package mypackage.

(See go help test for more detail.)

huangapple
  • 本文由 发表于 2021年8月25日 18:01:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/68920775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定