GoLand Project Structure

Answered

Hello... I'm mostly a newb to GO and GoLand. I've developed a few small CLI tools in GoLand for my job and it's been great so far. But I'm confused by what I'm sure is a simple thing. In the previous projects I've created in GoLand, I simply created a project path containing $GOPATH/pkg, $GOPATH/src and $GOPATH/bin directories. I'd set my project $GOPATH to the project path, put my main.go in the base and other source files in my own package folder under the src directory. If I ran a go get for some modules off of github they would download into the src directory and then sometimes into the pkg  directory, or at least that's where the .a files for the modules would be built.

Now I'm trying to structure a new project "correctly" as I understand it, but I hit a snag. I have a new project folder at $GOPATH. It contains $GOPATH/src, $GOPATH/pkg and $GOPATH/bin dirs in it, but in the $GOPATH/src directory I have a go.mod file and a single main.go which references a local package I've created which sits inside the $GOPATH/src/utils directory. That all works fine.

When I do a go mod tidy from the terminal, all the modules from github and such are downloaded into $GOPATH/pkg/mod. The problem I have is that my source files in $GOPATH/src/utils cannot see the modules for import. I have module Integration enabled so not sure how to get the source code to see the modules. I'm using GO 1.19.1 and GoLand version 2022.2.4. The path structure looks like this:

$GOPATH
    src/
        go.mod
        main.go
        utils/
            local package go files with imports to external modules
    pkg/
        mod/
            github.com/
                xxx
                xxx
            gopkg.in/
                xxx
                xxx

Any assistance is greatly appreciated. I've been trying to figure this out for awhile now and searched and read a lot about go project structure but nothing is clicking with me for some reason.

Thanks... Jim

0
3 comments

Hello Jim,

I think there is some confusion about $GOPATH.

You don't have to create a project structure with $GOPATH variable at all with Go modules.

With Go modules, $GOPATH uses to download dependencies and GoLand handles it automatically.

To create a new and correct project structure you need the following:

  • Create a new Go modules project from the Welcome Screen in GoLand.
  • Start developing your application, create new packages (it is also not nececarry to create all of it in $GOPATH).
  • If there is some 3rd-party dependency: hover over it and invoke Sync Dependencies action (Alt+Enter).

Please see our Quick start guide: https://www.jetbrains.com/help/go/quick-start-guide-goland.html

You can also start by exploring some Go libraries e.g. clone (anywhere on your system) and open https://github.com/sirupsen/logrus to see how GoLand handles dependencies (you should invoke Alt+Enter in go.mod file to synchronize dependencies).

I hope it helps.

1

Thank Danill... It does make snse. What started me down this path was that I developed a tool on my Mac that linked in a native C library. I couldn't cross compile it on the Mac so I had to stand up a Linux VM and bring the project into the VM to compile it to run on Linux. Rather than setup XWindows and JetBrains to simply compile the project I just ran go build from the CLI and with everything in the same folder, I got a bunch of modules not found and had to rework the GOPATH and such. I did get it to compile but I just thought maybe I should restructure my original Mac project to "correctly" match what go build cli expects. I'll do more reading on it. Thanks for your reply it did help.

 

Jim

0

Marlynrasavong, could you please elaborate a bit? Do you want to create a Go project structure?

0

Please sign in to leave a comment.