Thomas McGoey-Smith

How Should I Structure My Golang Web App?

So I just launched and deployed Has It Shipped Yet. A simple Shopify order tracking app.

I’ve always found that the best way to actually pick up a language is to build something - particularly something real, that doesn’t get thrown away.

I decided to structure the app like this:

main.go <-- holds the server setup code
/app    <-- all the app code
    /router      <-- just a single route file
    /controllers <-- handles HTTP calls
    /models      <-- interacts with my DB
    /views       <-- holds all my templates (grouped by controllers)
    /helpers     <-- holds my external API code
    /public      <-- anything static
    database.go  <-- central db connect
    logger.go    <-- sets up my logger

I wanted to use an MVC style setup, which was largely borrowed from developing a handful of Rails apps.

When it came to deploying, I found my templates to be kinda messy. Basically, I needed to recreate this structure just for my templates to be parsed.

For my next project (Trolley). I think I’m going to try something like this reddit commenter said:

.../project/main.go = executable, only Go file here
.../project/db/... = Go code for database access
.../project/web/... = Go code for web (dispatch, handlers)
.../project/model/... = Go code for models
.../project/(other)/... = Go code for other things (several of these)
.../project/templates/... = HTML templates (compile-time)
.../project/deb/... = .deb file structure
.../project/vendor/... = vendor directories (jquery, etc.)

The beauty with Golang is that you’re not tied down to an opinionated framework, so you can always make things a bit leaner.

I’m always fascinated by how others structure their Go projects. How do You structure yours?

@tamcgoey on Aug 11, 2015

Enjoyed the article? Subscribe to my newsletter for more.

© Thomas McGoey-Smith (2014-2018). RSS.