Search code examples
gogoland

Go project workspace (Goland) of a library package behaves differently in Windows and Linux


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.


Solution

  • 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:

    1. Create a new folder for your project. Let's say the folder name is "mypackage".

    2. Open a terminal and navigate to the "mypackage" folder.

    3. 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.

    4. 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

      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.

    5. 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 the replace 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.

    1. 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.