Home > Net >  How to isolate modules in golang?
How to isolate modules in golang?

Time:09-19

I would like to know if it is possible to create isolated modules with golang, the application always starts in a file, can i separate several REST modules and upload them depending on the conditions?

For example. I have a products module with Rest and database, and I have a users module with Rest and database. Can I create a way to choose if I will run only one or both dynamically? Without having to import everything into a main.go file?

CodePudding user response:

Go is compiled language and import happens at the build time, not at the run time. Each module source is imported and compiled into the final binary and that's what you run then.

To achieve what you are asking for you'd probably need to split your program into multiple binaries each containing only relevant portion of code for its responsibility. Then you would need also to provide some communication between them (e.g. having gateway that intercepts incoming request and make relevant request(s) internally to e.g. Users service or Products service, etc.). This is often called microservice architecture.

For smaller project I recommend just put everything together first; and as it grows (and causes problems, if ever) eventually refactor to microservices.

  •  Tags:  
  • go
  • Related