Structuring our Application

February 6, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

Twitter: @Elliot_f

Now that we have a simple app up and running, let’s consider how we are going to structure this in such a way that it is easy to do things like mocking when it comes to it and to easily swap in dependencies as we need them.

Let’s start off by updating our main.go file and create a new app struct:

package main

import "fmt"

// App - the struct which contains things like
// pointers to database connections
type App struct{}

// Run - handles the startup of our application
func (app *App) Run() error {
	fmt.Println("Setting Up Our App")
	return nil

// Our main entrypoint for the application
func main() {
	app := App{}
	if err := app.Run(); err != nil {
		fmt.Println("Error Starting Up")

Now, you may be wondering why we have done it this way? If you are new to Go then the advantages come further down the line when we start to seriously test our application using unit tests, mocks and integration tests to validate that our app works as intended.

With this approach to app setup and initialization we’ve also been able to improve the way our app handles errors in startup instead of just instantly failing within the main function.