Project Layout for my Rest web service

Currently I am building an E-Commerce API which is getting long and heavy to keep it in a single main.go file.
Any Idea or resource for managing or designing my code for better understanding.

I have seen many post and article which have controllers, models and template folder but I don’t understand how do I implement this.

Sorry, you are asking a generalized question, which can only be answered by writing out the notes, which, as you said yourself, have read a large number, ask more specifically, as I understand you have a cumbersome main.go, then you need to delve deeper into the organization of the code and the principle of object-oriented programming, analyze your code, it is possible to use interfaces somewhere more convenient, you can also break your program into packages that will include frequently used functions, in general, as I said, your question is very vague.

Actually I am not a native english speaker. That is why it is difficult for me to express my problem

I understand perfectly well, storing everything in one file is not good, as I said, try to split it into packages, and since you are developing for API, you can use controllers, for example, describe controllers using the Get(), Post(), Delete(), Put() methods, here an excellent article having studied it, you can understand well the controllers
if you also use the web interface, then for templates and handlers too create a separate directory,
also a folder for the static content of the web resource

Hi, Kunal,

I think n0kk’s point is that a question like “Any Idea or resource for managing or designing my code for better understanding,” is hard to answer without any context. With a general question like that, we can only give you general advice just like you can find anywhere on the internet about breaking problems down into smaller pieces. If you give us a more specific problem (e.g. “Here’s the code to a 500 line function that I don’t know how to simplify”), we could give you more specific advice to answer that specific problem.

If you’re looking for general advice, this can still be a good site to get it, but I think it would be better suited for the #technical-discussion category of the site. The #getting-help section tends to be better for specific problems.

my repo link :-

As you will notice I am building an e-commerce api.
In main.go I only handle CRUD operation for user model. But I want to include item, cart, seller model to work on. By including all the models it make difficult to work on single file main.go.
That is why I want to know what should be my project layout?

Again, this is a general wish and a specific answer cannot be found here, if you want to implement a microservice project (which I advise you) you can use the gRPC, then the project structure will be one or the more popular GraphQL, then the structure will be different, I also know in e-commerce popular BeeGo framework, for using CRUD,
Your question is technical, as you need to build the architecture of the application.
To do this, it is better to take a sheet of paper and a pencil and draw what and with what will communicate and what requests to process and send, then you will imagine where to store what and distribute it yourself.
I can also advise the directory structure is not a standard

Again, I watched how they used СRUD in conjunction with the Cobra package, ( taking out each operation of Get, Create, Delete, Update in separate files to manage with CLI, in general, it’s a matter of taste and dependence on tasks

Kat Zien’s presentation on how to structure go projects is helpful to see what structure works for your project.

I’ve had some good experience with go-chi as a “framework” to build your API with, it’s quite lightweight and comes with a bunch of tooling that one would need.

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