Structuring an application

Hello everyone. I’ve been learning golang over the past few weeks (mainly using the Go playground), but now I find myself wondering how to properly structure, or organize, a go application. I’ve read quite a few posts on this topic and what I’m using right now is this

project_directory
  |--bin
  |--pkg
  `--src
       `--github.com

My GOPATH is set to my “project_directory”. Now this means that every folder under github.com/ is basically a repository and therefore under version control on GitHub. I come from the Java world, so it’s been hard for me to grasp the concept that my project directory itself isn’t under version control.
So with that in mind, I made a couple of Google search to find out about the Go modules with the go.mod file which would allow me to structure an app like so (if I understood what I read correctly):

project_directory
  |--go.mod
  `--src
       |--module1
       `--module2

My understanding here is that this would allow me to have the project directory with the ‘go.mod’ file under version control as well as the empty src directory. Empty src because go will fetch the modules from the go.mod file.

So this is my understanding of it, again I come from Java/Maven world so I might be completely off the rails here.

I know there isn’t any agreed upon standard for an application structure, I’ve seen quite a few suggestions out there that seems to be popular. So my questions (finally) are these:

  1. As it is not officially rolled out should I keep using the GOPATH method or can I safely use the go.mod method
  2. Regarding the go.mod way to do things, is there any way you guys think is more appropriate to structure an application that’ll end up being a docker image.

Thank you for all your help

Hi,

I’m a Java guy too (and native french speaker), maybe we’ll better understand each other :slight_smile:
Actually, GOPATH does not point to your project directory but to your workspace under which you’ll put your projects. Think of it as your Eclipse workspace. Read carefully this excellent documentation about GOPATH : https://golang.org/doc/code.html.
In your second part, the go.mod you show in your layout would be common to your two modules. But I imagine you would need a specific one in each of your module directories.
You should also note that GOPATH mode can’t let you specify your version in your go get commands when fetching your dependencies whereas the modules aware mod can : https://golang.org/cmd/go/#hdr-Module_aware_go_get
When you talk about layout for a docker image, you mean using a docker image for building ?

What I meant by “docker image” is I would have something like this:
project_directory
|-- go.mod
|-- Dockerfile
|-- Makefile
`-- src

But the links you provided are informative. I guess it’s my fault for reading more blogs than official documentation.

You may also find this video helpful.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.