英文:
Go project workspace (Goland) of a library package behaves differently in Windows and Linux
问题
我正在尝试使用Go创建一个新的库包项目,但是我在设置项目并使工作空间在我的Windows
和Linux
环境中表现一致方面遇到了困难。我创建了一个新的文件夹,并使用go mod init myname/mypackage
命令来设置一个新的Golang项目。
我的go.mod
文件如下所示:
module myname/mypackage
go 1.20
我打算创建一个具有一组子包的库项目,所以我没有添加一个定义main
包的main.go
文件。我添加了一堆包含Go文件的子文件夹来布置我的库包结构。每个子文件夹代表一个子包(至少,这是我尝试实现的),所以子文件夹中的所有代码文件共享相同的包名。
现在,编译器抱怨没有主包,我不知道如何正确配置它。子文件夹还包含测试,我可以在Linux环境中直接从Goland IDE中执行这些测试。
更新
这是我的库包项目的文件夹层次结构;mypackage
文件夹是存储库根目录,不包含任何Go文件。
mypackage/
go.mod
build/
sub_package1/
module1.go
module1_test.go
sub_package2/
module2.go
module3.go
当我运行go build -o build/
命令时,我收到以下错误:no Go files in <mypackage-path>
。
除了编译器错误之外,在我的Windows环境中,Goland工作空间似乎存在解析依赖项的问题:我的库包引用外部包,在我的Linux环境中可以正确解析这些包。这些被引用的包是常见的,在我的其他非库包和应用程序中可以正常工作;有趣的是,Visual Studio Code提供代码补全,但JetBrains Goland却没有。
英文:
I am trying to create a new library package project using Go, but I am struggling to make the project setup work and make the workspace behave equally in my Windows
and Linux
environments. I created a new folder and used the go mod init myname/mypackage
command to set up a new Golang project.
My go.mod
file looks as follows:
module myname/mypackage
go 1.20
I intend to create a library project that has a set of sub-packages, so I did not add a main.go
file defining a main
package. I have added a bunch of sub-folders with Go files to lay out my library package structure. Each sub-folder represents a sub-package (at least, this is what I try to accomplish), so all code files within a sub-folder share the same package name.
Now, the compiler complains that there is no main package, and I have no clue how to configure it correctly. The subfolders also hold tests, which I can execute in my Linux environment right from within the Goland IDE.
Update
This is the folder hierarchy of my library package project; the mypackage
folder is the repository root, which does not contain any Go files.
mypackage/
go.mod
build/
sub_package1/
module1.go
module1_test.go
sub_package2/
module2.go
module3.go
When I run the go build -o build/
command, I receive the following error: no Go files in <mypackage-path>
.
Aside from the compiler error, in my Windows environment, the Goland workspace seems to have issues resolving dependencies: My library package references external packages that get properly resolved in my Linux environment. The referenced packages are common and work without issues in my other non-library packages and apps; interestingly, Visual Studio Code provides code completions, but JetBrains Goland does not.
答案1
得分: 1
要在Go中创建一个库包项目,你不需要有一个主包或一个main.go文件。主包只对可执行程序是必需的。
要正确设置你的库项目,请按照以下步骤进行操作:
-
创建一个新的文件夹用于你的项目。假设文件夹名为"mypackage"。
-
打开终端并导航到"mypackage"文件夹。
-
运行以下命令来初始化一个新的Go模块:
go mod init myname/mypackage
这将在"mypackage"文件夹中创建一个go.mod文件。
-
现在,你可以通过将代码组织到子文件夹中来开始创建你的库包。每个子文件夹应该代表一个单独的包。
例如,你可以有如下结构:
- mypackage
- subpackage1
- file1.go
- file2.go
- subpackage2
- file3.go
- file4.go
- subpackage1
确保每个Go文件顶部的包声明与子文件夹名称匹配。例如,如果在subpackage1中有一个file1.go文件,那么该文件中的包声明应为
package subpackage1
。 - mypackage
-
除非你想在开发过程中用本地副本替换依赖项,否则你不需要在go.mod文件中使用
replace
指令进行版本控制。如果你只想使用严格的版本控制,可以从go.mod文件中删除replace
指令。
你的go.mod文件应该类似于这样:
module myname/mypackage
请确保将myname/mypackage
替换为你的包的实际名称。
- 要在另一个项目中使用你的库包,可以使用在go.mod文件中指定的模块路径进行导入。
就是这样!你已经在Go中设置了你的库包项目。记得将你的代码组织到子文件夹中的单独包中,并确保你的Go文件中的包声明与子文件夹名称匹配。
英文:
To create a library package project in Go, you don't need to have a main package or a main.go file. The main package is only required for executable programs.
To set up your library project correctly, follow these steps:
-
Create a new folder for your project. Let's say the folder name is "mypackage".
-
Open a terminal and navigate to the "mypackage" folder.
-
Run the following command to initialize a new Go module:
go mod init myname/mypackage
This will create a go.mod file in the "mypackage" folder.
-
Now you can start creating your library packages by organizing your code into sub-folders. Each sub-folder should represent a separate package.
For example, you can have a structure like this:
- mypackage
- subpackage1
- file1.go
- file2.go
- subpackage2
- file3.go
- file4.go
- subpackage1
Make sure that the package declaration at the top of each Go file matches the sub-folder name. For example, if you have a file1.go in subpackage1, the package declaration in that file should be
package subpackage1
. - mypackage
-
You don't need to use the
replace
directive in your go.mod file for versioning unless you want to replace a dependency with a local copy during development. If you just want to use strict versioning, you can remove thereplace
directive from your go.mod file.
Your go.mod file should look similar to this:
module myname/mypackage
Make sure to replace myname/mypackage
with the actual name of your package.
- To use your library package in another project, you can import it using the module path specified in your go.mod file.
That's it! You have set up your library package project in Go. Remember to organize your code into separate packages within sub-folders, and ensure that the package declarations in your Go files match the sub-folder names.
答案2
得分: 1
完整的回答如下:
如果你按照正确的步骤进行操作,并确保每个子文件夹中都包含有与子文件夹名称匹配的包声明的 Go 文件,那么 go build
命令应该能够顺利完成,没有任何错误。
然而,有几点需要注意:
-
go.mod 文件中的
replace
指令用于在开发过程中使用本地副本替换依赖项。如果你没有需要替换的依赖项,可以从 go.mod 文件中删除 replace 指令。 -
确保 go.mod 文件中指定的模块路径与你的包的实际名称匹配。在你的情况下,应该是
module myname/mypackage
。确保这个模块路径在所有的 Go 文件中保持一致。 -
检查代码中是否有任何编译错误是很重要的。即使项目结构和 go.mod 文件设置正确,如果你的 Go 文件中存在语法错误或其他问题,
go build
命令可能会失败。检查你的代码是否有错误,并进行相应的修复。
如果你已经正确地完成了所有步骤,并且代码中没有错误,go build
命令应该能够成功编译你的库包,没有任何问题。
英文:
a complete answer for you comment:
If you followed the steps correctly and ensured that each sub-folder contains Go files with package declarations matching the sub-folder names, the go build
command should complete without any errors.
However, there are a few things to note:
-
The
replace
directive in the go.mod file is used to replace a dependency with a local copy during development. If you don't have any dependencies that need to be replaced, you can remove the replace directive from your go.mod file. -
Make sure that the module path specified in your go.mod file matches the actual name of your package. In your case, it should be
module myname/mypackage
. Ensure that this module path is consistent across all your Go files. -
It's important to check for any compilation errors in your code. Even though the project structure and go.mod file may be set up correctly, if there are syntax errors or other issues in your Go files, the
go build
command may fail. Review your code for any errors and fix them accordingly.
If you have done everything correctly and there are no errors in your code, the go build
command should successfully compile your library package without any issues.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论