How to Structure your next Golang App with Go Mod and Subfolders
Coding your next Golang Project using Go Mod and Subfolders.

Maybe you are someone looking to start building a project in Golang. First starting out, you may never consider the structure of the codebase and it is easy to leave out or forget to structure your project for the future.
Here are a few tips for organizing your own project and getting your GoLang codebase to be more scalable for the future.
How NOT to structure your APP in Golang.
I’ve set up a simple Golang app like “Hello world” to demonstrate.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}This may seem correct, however, you really should have started with Go Mod.
The real reason not using Go Mod is a bad idea is because you will not be able to link packages from outside the project root or access sub-packages easily.
In order to keep packages contained within subfolders, you will need to import each module for the Go Compiler to be able to access them directly.
Furthermore, Go Mod Relies on the ability to manage packages as dependencies. We will demonstrate this later on. Let’s first dive into the benefits of using Go Mod.
How to structure your APP in Golang with Go Mod.
A single most important benefit to using Go Mod is you will be able to scale your codebase with subdirectories.
Let’s set up the exact same Golang project as before but instead utilize a few Go Mod commands and create a much better Project Structure using subdirectories.
First, run the Init command, this will initialize a go file in your project root.
go mod init mygolangappA new file will be created within your project root directory. You will see within the root of your project a file named.
go.modAfter this file is created, the Go compiler will be able to handle updating of any required modules and package dependencies. At this point, keep this file blank.
Next, Let's create the same “Hello, World” Golang Project as before but instead use Go Modules inside subfolders. Create a main.go the file that calls an import to a subfolder that which we have not set up yet.
package main
import (
app "mygolangapp/app"
)
func main() {
app.Init()
}Here we have a main.go file in the project root and the import is able to access the package available in a subdirectory. The function we are accessing is written to initialize our real application.
Before the compiler says it cannot find the
appmodule or theInithandler function, We will want to create a subfolder (or subdirectory) named app within the root of the project with the file name ofapp.go
If done correctly, your folder structure should have the following files.
/app
--/ app.go
main.go
go.mod
go.sum Next, let's also create the subfolder /app and the app.go file inside.
package app
import "fmt"
func Init() {
fmt.Println("Hello, World!")
}Take a moment to notice the function (func) is written with a Capital Letter Init().
This is done to make sure the Golang Compiler will know how to export your Function to the entire application.
Without a Capital Letter. Your function will be private to the package.
It is important to remember to create useful function names with a Capital Letter — Only if you would like these functions to be accessible to other parts of your application.
Now let's review the go.mod file, you should see the name of the module you originally entered which is actually the connection binding your main.go and app.go with each other across subfolders. Each package can now speak with each other across subfolders and project files.
module mygolangapp
go 1.14Next, I'll run the command go run . within the project root and see the message appear Hello, world!
Go mod and subfolders are wonderful for keeping organized!
Happy Coding!! Thank you for reading and hope this is helpful for those looking to learn how to use subfolders in a Golang project.
P.S. If you’re a fan of Medium as much as we are, consider supporting me and the thousands of other writers by signing up for a membership.
It only costs $5 per month, and it supports us writers. Thank you, Greatly.





