I am trying to create a new library package project using Golang, but I am struggling to make the project setup work. I created a new folder and used the go mod init myname/mypackage
command to set up a new Golang project. To use strict versioning, I added a version number to the go.mod
file using replace
(not sure, if that is correct).
My go.mod
file looks as follows:
module myname/mypackage
go 1.20
replace myname/mypackage => myname/mypackage v0.1.0
As said, I intend to create a library project that provides a set of sub-packages, so I did not add a main.go
file defining a main
package. I tried to add a bunch of sub-folders to lay out my library package structure and added multiple Go files to each sub-folder. 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. Any guidance regards library development in Golang is appreciated.
Update
This is the folder hierarchy of my library package project; mypackage
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>
.
As recommended, I removed the replace
definition from the go.mod
file.
module myname/mypackage
go 1.20
require (
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.8.4
)
Aside from the compiler error, the workspace also seems to have issues resolving dependencies: My library package references the two external packages as shown in the go.mod
file above. Those are quite common packages that work without issues in my other non-library packages and apps; Visual Studio Code does provide code completions, but JetBrains Goland does not.
The behavior equals on my Windows and Linux development machines.
2
Answers
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:
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:
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
.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:
Make sure to replace
myname/mypackage
with the actual name of your package.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.
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.