DEV Community

loading...
Cover image for Structure your Go projects into sub-directories

Structure your Go projects into sub-directories

Tomas Sirio
I'm a Barbie Dev, In a Barbie World.Wide.Web Backend Developer at Ualá. Master of memes.
・2 min read

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:

mkdir goModules                                                                             
cd goModules                                                                                
mkdir model                                                                                 
mkdir view                                                                                  
mkdir controller                                                                            
Enter fullscreen mode Exit fullscreen mode

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

go mod init github.com/tomassirio/goModules
Enter fullscreen mode Exit fullscreen mode

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.

//model.go
package model

type ModelStruct struct {
    Feature string
}
Enter fullscreen mode Exit fullscreen mode
//view.go
package view

import "fmt"

func ViewFunction() {
    fmt.Println("A view Function doing View Stuff")
}
Enter fullscreen mode Exit fullscreen mode
//controller.go
package controller

import "fmt"

func ControllerFunction() {
    fmt.Println("Just Controller Stuff")
}
Enter fullscreen mode Exit fullscreen mode

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.

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()
}
Enter fullscreen mode Exit fullscreen mode

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:

go run main.go
Our Model Feature looks like this 'A Feature'
A view Function doing View Stuff
Just Controller Stuff
Enter fullscreen mode Exit fullscreen mode

I'll leave you a link to the repository if you want to check out how the solution is actually done

Happy Coding!

Discussion (0)

Forem Open with the Forem app