英文:
Move main out of the repository root in go
问题
我正在创建一个Go项目。由于项目的存储库根目录下已经有很多东西(文档、README.md等),我希望所有的源代码都放在一个名为src的文件夹中,所有的测试代码都放在一个名为test的文件夹中:
\go
\src
\github.com
\user
\my_project
\src
main.go
some_func.go
\test
test_main.go
test_some_func.go
\doc
README.md
但是我有两个问题:
- 当我在my_project文件夹中时,构建命令不起作用。我必须进入my_project/src才能成功运行构建命令。我想要在my_project文件夹中运行构建命令。如何让Go理解my_project的源代码在src文件夹中?
- 然后,go install命令生成的可执行文件名为src。如何更改该可执行文件的名称?
英文:
I am creating a project in go. Since there is already a lot of things at the root of the repository of the project (doc, README.md...), I want all the source code to go in a folder src, and all the test code in a folder named test :
\go
\src
\github.com
\user
\my_project
\src
main.go
some_func.go
\test
test_main.go
test_some_func.go
\doc
README.md
But I have two issues :
- The build command is not working while I am in the my_project folder. I have to go in the my_project/src to successfully run build. I want to do it from the my_project folder. How to inforce go to understand that the source for my_project is in the src code ?
- Then the executable produced by the go install command is named src. How to change the name of that executable ?
答案1
得分: 8
我想要将所有的源代码放在一个名为src的文件夹中,将所有的测试代码放在一个名为test的文件夹中:
Go语言有一种组织源代码的方式。不要与此作斗争。这是系统的工作方式。按照Go语言的要求组织你的代码。不要试图强制Go按照你在其他语言中学到的方式工作。每种语言都有自己的做事方式。没有哪一种是“正确”的。就像Java一样,Go语言对你应该做什么有非常明确的想法。按照这种方式去做。(这不是关于Go是否“正确”或者Go是否“错误”的争论。Go就是Go,它以Go的方式做事。)
特别地,你不应该创建另一个“src”目录。在“go”树的顶部已经有一个“src”目录了。如果你创建另一个多余的“src”目录,那么你的项目的包名将是“github.com/user/my_project/src”,这可能不是你想要的。
为了让可执行文件的名称是你想要的,将它放在一个你想要的目录中(可能是“my_project”)。将测试文件与它们所测试的文件放在一起。这就是Go的工作方式。
所以你的目录结构应该是这样的:
\go
\src
\github.com
\user
\my_project
main.go
some_func.go
main_test.go
some_func_test.go
\doc
README.md
试图做其他事情只会一次又一次地失败,而关于“如何让构建系统做其他事情”的问题将不断返回“按照构建系统的期望方式放置你的代码”。
关于Go的期望和代码组织方式的详细信息,请参阅Go命令文档中的GOPATH环境变量。一旦你按照这种方式构建了你的系统一段时间,你就会开始看到你可以偏离它的地方(比如创建其他目录用于测试工具,但不是测试用例)。在尝试了标准的Go方式之前,不要开始偏离。
英文:
>I want all the source code to go in a folder src, and all the test code in a folder named test :
Go has a way that it organizes source code. Do not fight this. It is how the system works. Organize your code the way Go wants you to. Do not try to force Go to work the way you have learned working in some other language. Every language has its own ways of doing things. None of them are "correct." Like Java, Go has very specific ideas of what you're supposed to do. Do it that way. (This isn't an argument about whether Go is "right" or Go is "wrong." Go is Go, and it does things in the Go way.)
In particular, you should not create another "src" directory. There is already a "src" directory at the top of the "go" tree. If you create another, redundant, "src" directory, then the package name for your project is "github.com/user/my_project/src" which is likely not what you want.
To make the executable be named what you want, put it in a directory named what you want (probably "my_project"). Put test files with the files they test. This is how go works.
So your tree should look like:
\go
\src
\github.com
\user
\my_project
main.go
some_func.go
main_test.go
some_func_test.go
\doc
README.md
Attempts to do something other than this is going to blow up over and over again, and questions of "how do I make the build system do this other thing" will continually return "put your code in the way the build system expects."
For details on what Go expects, and how you should organize your code, see GOPATH environment variable in the Command Go documentation. Once you've built your system this way for a while, you will start to see where you can deviate from it (like creating other directories for test utilities, but not test cases). Don't start deviating until you've tried it the standard Go way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论