Best way to organize simple web API server?

Most of my experience as a developer is making CRUD web API servers in C#. I organize them using the Model-View-Controller structure, often with a Repository layer for MySQL queries.

In learning Go, I have learned that the MVC pattern is not recommended in Go (link). Instead, I’m supposed to use ‘package-oriented design’. However, I feel like the examples I have found are designed for larger applications than what I want to start out building.

What sort of design do you use for simple servers? Is there a pattern you keep coming back to?

1 Like

Well i m here for the same…Hopefully someone will post an accurate solution here.

It’s my first time developing an API server (personal project) and I’m currently following the Standard Package Layout approach discussed by Ben Johnson.

1 Like

Have a look at this code: https://github.com/santosh/bookstore-api/blob/master/main.go

It will answer some of your questions. I am also learning golang, so what are thoughts on it?

In my opinion, MVC pattern works fine for “simple” servers. However, the way you implement it is important. For example, naming your packages “controllers”, “models” etc. will make it hard to maintain and scaling up. The most common problem you will face in my experience would be avoiding import cycles, since Go doesn’t allow circular imports. Even if you somehow end up avoiding this, I think ensuring different names for every controller/model is tiresome. Check this out. The approach suggested does avoid these basic concerns and is to some extent, if not totally, package oriented too. Feel free to go through the rest of the forum, many design patterns have been discussed with their pros and cons which you may find useful.
Hope it helps!

I found this talk by Kat Zien How Do You Structure Your Go Apps.

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