Go modules in Goland

Robert Hirsch
Level Up Coding
Published in
6 min readAug 20, 2021

--

How the hell do you do this?

I want to start this article by expressely stating that I am new to Golang and Goland. I LOVE the Jet Brains IDE for so many languages and in particular Pycharm. I cant recommend their products enough. So I really wanted to get Goland working. I wrote this to help me get a process down, and hopefully will help you.

The Problem

At this moment, Go modules are relatively new, JB’s implementation of them are even newer, their online docs are out of date, their tutorial is unwatchable, and they made the awful decision to name their Go IDE so similar to the official name of Go. Thus, getting your search results to focus around Goland is difficult when the language you are working with is called Golang. It has lead to much anger and frustration on my part (hence, the intro picture).

Aside from help from my friend Richard (the maker of the amazing BoxWallet for crypto), there are two articles that are written about Go modules that I found really helpful, but neither focused on the Goland implementation. So “Thank you” to Jody LeCompte and Ulas Turkmen for your efforts! The latter article is at a domain name that really appealed to me at the time.

Starting out

You have downloaded Goland and installed it (I am on a Mac, I can only assume that the Windows experience is similar). So, select File -> New… -> Project, and you get this window.

Well, I don’t know about you, but I have my own file structure for all my projects, in all the languages I work in. So that location is not going to work for me. I find that using names like “golandmodules” becomes confusing because it is hard to determine what is unique and what are requirements of the process. So…

We are going to make a project called “footodor”. Press the “create” button. now you get a project window, with the project structure on the left side. Let’s follow a recommended standard Go project structure, and create two of the folders they recommend, cmd and internal. You can do this by right clicking on the “footodor” project name and create those directories.

The next step is the one that really makes me burn. It’s not obvious (to me) how to do this with the Goland interface. In my view, this should just happen, you shouldn’t have to do anything. Instead, we need to enter a command into the command line terminal to make the project work. Here we go: you need to initialize the Go module. So, in the bottom left of the project screen you see a “Terminal” tab. This will launch a terminal placing you in the “footodor” directory. Type the following command:

go mod init fish.smell.bad123/footodor

Now, I did this silliness again, so you could see what was required and what was not. It has become normal to use your github account (like github.com/hirscr for me) as the label for the module. But I found that confusing also, so to point out what was required I did the “fish.smell.bad123” thing. You dont need the periods. It’s just a label, not a website. Pick one, and use it for everything. Your github address is fine. If you have your own domain, use that. It’s just a label and needs to be unique. Make sure you are in the top directory of the project, and that you put the project name after the slash. You will see this:

There will of course be other text, you don’t need to worry about that now. Right now you should see a new go.mod file in you top level project directory. At some point you are going to see a little window on the lower right like this:

Go ahead and enable the integration. I have no idea why it just doesnt do this. After, it will warn you the GOPATH is no longer indexing. Great. Leave me alone.

Make a program

We are going to make a really dumb program (but not as dumb as Hello World). We need a main.go program (and main package) and we need another package. In Goland (and Golang), when you want a package, it should be in its own folder of the same name. So, if we want lots of packages, they should be in their own folders. In the standard structure, we want “internal” packages to be custom packages that are unique to the project, and not used externally to it. So let’s make two packages, “toejam” and “fungus” in the “internal” directory and a “main” folder in the “cmd” directory. Now my structure looks like this:

So, now we are going to write some Go code. In each lowest directory (main, fungus, toejam), make a Go file. You will see that each file you make, will be part of the package of that directory. If you make more files in those directories, Goland will assume they should be part of the same package. Finally we are ready for the dumb program.

First, lets make the functions in the internal packages:

Yes these are totally stupid, but they are functions in a package. Now let’s make a main function that calls these. The reason I show it this way, is that Goland nicely does the importing for you. You don’t have to write this stuff up in the import area, it does it.

You can see the Goland puts in the label we chose earlier all by itself. It also code completes the function from the two internal packages on its own. so everything is now operating as you would expect, and we can code away! At the end of all this my project structure looks like this:

And how do we run this?

I was put off again when I wanted to run. In Pycharm there is a run button and you just press it. In Goland, a run button was faded out, and it appeared that I needed to set up a configuration, and none of the configurations seemed like what I wanted. None of these seemed right:

So, I went ot the run menu, and chose “Run…” and then this window came up.

By doing this it made the run configuration for me. And every thing worked!

Conclusion

I didn’t find this intuitive at all. I hope this article was helpful for you to get started with Goland, I wished this was written for me. I hope Jet Brains will work to make this process much easier than it currently is.

If you have another way to get started, or you have an easier process than I put here, PLEASE put it into the comments.

--

--

Author, Maker, Father, Dreamer. Robert received his Ph.D. from RPI in Mechatronics. Since then, consumer devices, renewable energy, and now blockchain.