Web App Structure


(Jared) #1

I have just recently started using go. And I am wanting to write a web app in it using the standard library. I was wondering what is the recommended file structure for a web app?


(Anik Hasibul) #2

Possibly the cleanest architecture

├── driver // all db connections (mysql,psql, mongo)
├── handler // all handlers
│   └── http
│   └── websocket
 |── models // all global data models (request json model, response model bla bla bla)
└── repository // database query interface
    └── user //db funcs that queries users
    └── posts // queries posts...

(Onezino Moreira) #3

Go tries to avoid multiple packages/folder in a general app.
A good example to start:

/cmd
.. main.go
/myapp
.. model.go
.. server.go <-- your http handler go here
.. service.go
/internals
.. someobscurestuff.go

(Jared) #4

What is your cmd directory? This will be my first web app in Go. I have always written them in PHP.


(Jared) #5

That makes since to me looks like a good structure. Couple of things I’m not sure about though. What is the handler directory? And where would you put your templets and css? What all would be in main.go would it be just your main logic and routes. Or Would it also have your server information?


(Jared) #6

This is a little new to me. I have always written my projects from scratch In PHP. Didn’t like using frameworks so I would write it from scratch. But then I found out there was some things I could have done better like using templates and such instead of writing more code than I needed to. And using models, I have never used those. After reading about them I found out that they are pretty useful. So I plan to use those things in this next project.


(Sean Noonan) #7

Check this out,

Structuring Applications in Go

I used to place my main.go file in the root of my project so that when someone runs “go get” then my application would be automagically installed. However, combining the main.go file and my application logic in the same package has two consequences:

  1. It makes my application unusable as a library.
  1. I can only have one application binary.

The best way I’ve found to fix this is to simply use a “cmd” directory in my project where each of its subdirectories is an application binary. I originally found this approach used in Brad Fitzpatrick’s Camlistore project where he uses several application binaries:


(Chee How) #8

I found that the recommendations in this Medium post to be useful and intuitive.


(Jared) #9

Thanks. I haven’t been on here for a couple of days and just seen this. Getting ready to read this article.


(Jared) #10

Do you guys think the standard library is suitable to make a web app? Or should I use something else?


(Lutz Horn) #11

Yes.


(Jared) #12

So I shouldn’t have to use like mux or something other than the standard library? The standard library should work for having a blog, search, user authentication, databases and so on. I’m new to this I come from a php background.


(Lutz Horn) #13

Please take a look at this article: “Writing Web Applications”


(Chee How) #14

It is quite easy to get started with the standard library but I do find using a third party library does lend a certain structure to your application code.

Personally I use Mux and find that I can get off the ground much more quickly.