Web App Structure

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?

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...
2 Likes

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
2 Likes

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

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?

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.

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:

1 Like

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

1 Like

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

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

Yes.

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.

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

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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.