avatarSteven Rescigno

Summary

The article provides guidance on structuring a scalable Go (Golang) application using Go Modules and subdirectories for better code organization and dependency management.

Abstract

The article titled "How to Structure your next Golang App with Go Mod and Subfolders" emphasizes the importance of organizing Go projects for scalability and maintainability. It begins by acknowledging the tendency of developers to overlook project structure initially, which can lead to difficulties as the project grows. The author then demonstrates an unstructured "Hello World" Go application and explains why it's insufficient for larger projects, particularly due to the inability to link external packages and manage dependencies effectively without Go Modules. The article proceeds to illustrate how to properly set up a Go project using Go Modules, which allows for the inclusion of subdirectories and better dependency management. It walks through initializing a Go module, creating a main.go file that imports a subpackage, and structuring the project with a /app subdirectory containing an app.go file. The author highlights the significance of exporting functions by capitalizing their names to ensure they are accessible across the application. The article concludes by reviewing the go.mod file, running the application, and encouraging the use of Go mod and subfolders for organized coding practices.

Opinions

  • The author believes that using Go Modules is crucial for managing package dependencies and linking external packages in Go projects.
  • It is the author's opinion that structuring a Go application with subdirectories from the beginning is essential for scalability.
  • The article suggests that not using Go Modules is a poor practice because it limits the ability to access sub-packages and manage dependencies.
  • The author advocates for the use of capitalized function names to ensure their exportability and accessibility throughout the application.
  • The article promotes the idea that organizing code into subdirectories with Go Modules leads to a more maintainable and scalable Go application.
  • The author encourages readers to support writers on Medium by signing up for a membership, indicating a belief in the value of content creation and community support on the platform.

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 mygolangapp

A new file will be created within your project root directory. You will see within the root of your project a file named.

go.mod

After 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 app module or the Init handler function, We will want to create a subfolder (or subdirectory) named app within the root of the project with the file name of app.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.14

Next, 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.

Go Modules
Go
Software Engineering
Software Development
Project Management
Recommended from ReadMedium
avatarAI Rabbit
Goodbye Obsidian

7 min read