G
G
Gregory2019-09-30 11:34:35
go
Gregory, 2019-09-30 11:34:35

Project structure in Golang?

How to properly structure a Go project? As far as I know, in Go it is not customary to split files into small files. Those. in one file there can be a model, a service and a repository at once. Are there any best practices on this issue?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2019-09-30
@EvgenyMamonov

Good afternoon.
This is an attempt to standardize the structure of the project (many are guided by it)
https://github.com/golang-standards/project-layout
Go-Kit is a very competent structure, very
https://github.com/go-kit/kit
And be sure to worth a look
https://12factor.net/ru/
As for "Ie, in one file there can be a model, a service and a repository at once. Are there any best practices on this issue?" - then this is not entirely true, in one package (folder) there can be a model, and a service, and a repository. "For example, like this:
yourpackage/service.go
yourpackage/repo.go
yourpackage/model.go
And all this will be available in within a package.
Personally, I use this scheme:
cmd/ - commands of the executable file
conf/ - application configs (env files)
init/ - configs for logrotated, nginx, systemd, etc.
pkg/ - public packages
pkg/packagename/ - interfaces (service, repository, etc.) are defined here in different files
pkg/packagename/endpoints/http - endpoints for HTTP
pkg/packagename/repo/mysql - implementation of the repository on MySQL
pkg/packagename/repo/gorm - GORM repository implementation (for example)
pkg/packagename/service/ service implementation
I wrote very briefly, if something is not clear or you have questions - write, I will describe in more detail

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question