Structure your Go projects into sub-directories
Tags: go, programming, help, motivation
Published: 2021-01-14 21:56:09

Hey there!

One of the things that left me puzzled through my journey of learning Go was that no tutorial had any kind of folder structure whatsoever.

Every single file was dropped on the main folder running amok or functions were just lazily declared on the main file (Everything obviously under the main package).

## 🧰 Prerequisites

I'm assuming that you know your way through the terminal (just basically, nothing fancy here). And how to compile a Go application and some of it's syntax.

## 🎈 Motivation

Why would you want this? Well, I come from a Java background and most (if not every) project that I've worked with was structured with sub-directories. 

Also, as applications grow bigger and bigger, having a simple folder for multiple files is tedious and obnoxious.

## 🔩 Go Modules

So, first things first, let's create our structure. Let's assume you are going to create an MVC application:

```bash
mkdir goModules                                                                             
cd goModules                                                                                
mkdir model                                                                                 
mkdir view                                                                                  
mkdir controller                                                                            
```

Now we need to define our project as a Go Module:

```bash
go mod init github.com/tomassirio/goModules
```

**Attention** I used the final repository where I'm hosting this solution as the name of the module. But you don't need to use Github nor use a link for the module. You can use Whatever name comes into mind.

## 📁 Our files

Moving on. We have the folder structure and within each folder, I created a single .go file for this example. 

```golang
//model.go
package model

type ModelStruct struct {
	Feature string
}
```

```golang
//view.go
package view

import "fmt"

func ViewFunction() {
	fmt.Println("A view Function doing View Stuff")
}
```

```golang
//controller.go
package controller

import "fmt"

func ControllerFunction() {
	fmt.Println("Just Controller Stuff")
}
```

As you can see, the view and controller files have functions. However, the model file has a struct. And you are going to see why in the next section:

## 🎁 Wrapping Up

Let's head to our main.go file which will be situated on our project's root directory.

```golang
package main

import (
	"fmt"

	"github.com/tomassirio/goModules/controller"
	"github.com/tomassirio/goModules/model"
	"github.com/tomassirio/goModules/view"
)

func main() {
	m := &model.ModelStruct{Feature: "A Feature"}
	fmt.Printf("Our Model Feature looks like this '%s'\n", m.Feature)
	view.ViewFunction()
	controller.ControllerFunction()
}
```

What's important here is to note that we have to import manually our 'packages' on the import section. But later on, in order to use them, we have to declare the package were both a struct and/or method come from. This is why I emphasized the model struct before.

Our result on the screen will look like this:

```bash
go run main.go
Our Model Feature looks like this 'A Feature'
A view Function doing View Stuff
Just Controller Stuff
```

I'll leave you a [link to the repository](https://github.com/tomassirio/goModules) if you want to check out how the solution is actually done

Happy Coding!