Golang microservice project structure
I'm at an initial stage of creating a microservice application in Go, but due to the way that the import paths and directories are handled I'm not quite sure what's best way to structure the project files.
Normally, the project would look something like this in Java:
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
Now if I do it the same way in Go, the import paths become somewhat cumbersome:
import (
"fmt"
"github.com/user/myproject/gateway_microservice/src/package1"
"github.com/user/myproject/gateway_microservice/src/package2"
)
Additionally, I hear that the idiomatic way is to put all main.go
files in a separate cmd
directory, which adds to the confusion. Would it look something like this:
|-- cmd
|-- gateway_microservice
|-- main.go
|-- config_microservice
|-- main.go
|-- recommendation_microservice
|-- main.go
|-- users_microservice
|-- main.go
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
What is the 'correct' or idiomatic way of structuring a project like this in Go?
go microservices directory-structure
add a comment |
I'm at an initial stage of creating a microservice application in Go, but due to the way that the import paths and directories are handled I'm not quite sure what's best way to structure the project files.
Normally, the project would look something like this in Java:
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
Now if I do it the same way in Go, the import paths become somewhat cumbersome:
import (
"fmt"
"github.com/user/myproject/gateway_microservice/src/package1"
"github.com/user/myproject/gateway_microservice/src/package2"
)
Additionally, I hear that the idiomatic way is to put all main.go
files in a separate cmd
directory, which adds to the confusion. Would it look something like this:
|-- cmd
|-- gateway_microservice
|-- main.go
|-- config_microservice
|-- main.go
|-- recommendation_microservice
|-- main.go
|-- users_microservice
|-- main.go
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
What is the 'correct' or idiomatic way of structuring a project like this in Go?
go microservices directory-structure
add a comment |
I'm at an initial stage of creating a microservice application in Go, but due to the way that the import paths and directories are handled I'm not quite sure what's best way to structure the project files.
Normally, the project would look something like this in Java:
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
Now if I do it the same way in Go, the import paths become somewhat cumbersome:
import (
"fmt"
"github.com/user/myproject/gateway_microservice/src/package1"
"github.com/user/myproject/gateway_microservice/src/package2"
)
Additionally, I hear that the idiomatic way is to put all main.go
files in a separate cmd
directory, which adds to the confusion. Would it look something like this:
|-- cmd
|-- gateway_microservice
|-- main.go
|-- config_microservice
|-- main.go
|-- recommendation_microservice
|-- main.go
|-- users_microservice
|-- main.go
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
What is the 'correct' or idiomatic way of structuring a project like this in Go?
go microservices directory-structure
I'm at an initial stage of creating a microservice application in Go, but due to the way that the import paths and directories are handled I'm not quite sure what's best way to structure the project files.
Normally, the project would look something like this in Java:
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
Now if I do it the same way in Go, the import paths become somewhat cumbersome:
import (
"fmt"
"github.com/user/myproject/gateway_microservice/src/package1"
"github.com/user/myproject/gateway_microservice/src/package2"
)
Additionally, I hear that the idiomatic way is to put all main.go
files in a separate cmd
directory, which adds to the confusion. Would it look something like this:
|-- cmd
|-- gateway_microservice
|-- main.go
|-- config_microservice
|-- main.go
|-- recommendation_microservice
|-- main.go
|-- users_microservice
|-- main.go
|-- gateway_microservice
|-- src
|-- docker
|-- config_microservice
|-- src
|-- docker
|-- recommendation_microservice
|-- src
|-- docker
|-- users_microservice
|-- src
|-- docker
What is the 'correct' or idiomatic way of structuring a project like this in Go?
go microservices directory-structure
go microservices directory-structure
asked Aug 19 '17 at 21:00
Rtsne42Rtsne42
167210
167210
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
The other answer here advocates putting each microservice into its own repository. There may be valid reasons for splitting things up that way, but there may be equally valid reasons from wanting to keep everything in one repository as well (it really depends on your project / circumstances)
If you want all the code in one repository, you can- you just need to follow Go's package rules. (this is a good read: https://golang.org/doc/code.html#Workspaces)
If you have a mix of commands and libraries, the directory structure you proposed in your question comes close, but you probably don't need the src
directories in there. Here's an example of how a directory structure within a repo with libraries and commands might look:
lib1/
-- some.go
-- source.go
lib2/
-- more.go
-- source.go
cmd/
-- microservice1/
-- main.go
-- microservice2/
-- anothermain.go
To use this repository, you would clone it inside a Go workspace on your system (see the link I shared above). Assuming your repository lives in github.com/mybiz/project, and your GOPATH
was ~/go
, the workspace would look as follows:
~/go/src/github.com/mybiz/
-- project/
<clone repo in here>
The file cmd/microservice1/main.go
would include the library lib1
via a path it expects it in relative to $GOPATH/src
as follows:
import "github.com/mybiz/project/lib1"
Now, your code has access to the exported symbols in that package using the package name declared in the files under lib1
... usually just:
package lib1
In cmd/microservice1/main.go
, with the import above, you could use lib1
symbols as follows:
lib1.CallMe()
I hope that helps clear up how Go's directory structure works.
Thanks, I'm not able to separate the repository into multiple separate repositories so I was thinking along the same lines. I came across a nice article which clarified some things: peter.bourgon.org/go-in-production but your answer also made things clearer.
– Rtsne42
Aug 20 '17 at 1:17
add a comment |
How do I organize my projects?
|-- github.com/avelino/service1
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service2
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service3
|-- Dockerfile
|-- main.go
Packages
All Go code is organized into packages. A package in Go is simply a directory/folder with one or more .go files inside of it. Go packages provide isolation and organization of code similar to how directories/folders organize files on a computer.
All Go code lives in a package and a package is the entry point to access Go code. Understanding and establishing good practices around packages is important to write effective Go code.
1
Thanks for the response, so do you keep each service in a separate repository?
– Rtsne42
Aug 19 '17 at 21:50
Yep, isolating each service in your repository
– avelino
Aug 19 '17 at 23:04
1
yup, this is how microservice should be structured. Because each service should be independent to eachother which mean they should be separated their repo.
– mfathirirhas
Mar 7 '18 at 4:14
add a comment |
I'm structuring it like this; mono-repo per. project approach. Taking into account that these services are closely related:
github.com/user/some_project/
├── pkg/ (common own-created packages for all services)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── sd/
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/
| ├── account/
| | ├── pb/
| | | ├── account.proto
| | | └── account.pb.go
| | ├── handler.go
| | ├── main.go
| | ├── main_test.go
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── frontend/
| └── user/
├── vendor/ (common vendor-packages for all services)
├── Gopkg.lock
├── Gopkg.toml
├── Makefile
├── README.md
└── docker-compose.yml
Later, when you start on your second microservices project, many of the packages in the /pkg directory would be required there too. What to do? Copy/paste? No! Instead, extract these packages from the project, i.e. log, metric... into a new separate repository, so that all projects imports these from that one repo.
Congrats, you are now also a great step on your way to create your own microservices-framework! Or at least your own kit :)
add a comment |
Thanks to @karl-andresen. I was doing research on the same topic and came up with the below structure hope this helps someone
github.com/username/container/
├── pkg/ ('username' created packages - common for all services & reusable in other projects)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── infra/ (sub category in packages)
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/ (where all microservices will be imported as submodules - may or may not be reused)
| ├── account/
| | ├── handler.go
| | ├── handler_test.go (unit testing, note filename with '_test')
| | ├── main.go
| | ├── main_test.go (another unit testing)
| | ├── account.cfg (configuration file for account microservice)
| | ├── submodule/ (sub directory)
| | | ├── submodule.go
| | | └── submodule_test.go (submodule unit test)
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── booking/
| └── user/
├── api/ (OpenAPI/Swagger specs, JSON schema files, protocol definition files.)
| ├── proto/ (protocol buffer files)
| | ├── v1/
| | | ├── account.proto
| | | ├── account.pb.go
| | | ├── booking.proto
| | | └── booking.pb.go
| | └── v2/
| └── rest/ (json files)
| ├── v1/
| | ├── booking.json
| | └── account.json
| └── v2/
├── configs/ (project config settings, default configs, file templates)
├── scripts/ (Scripts to perform various build, install, analysis, etc operations.)
├── build/ (Packaging and Continuous Integration.)
├── test / (system and module level tests)
├── docs/ (project documents folder)
├── examples/ (project examples for service interactions)
├── third_party/ (all open source, third party codes, where applicable fork and add as submodule)
├── githooks/ (project git hooks)
├── assets/ (common assests for all services)
├── Makefile
├── README.md
└── docker-compose.yml
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45776238%2fgolang-microservice-project-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The other answer here advocates putting each microservice into its own repository. There may be valid reasons for splitting things up that way, but there may be equally valid reasons from wanting to keep everything in one repository as well (it really depends on your project / circumstances)
If you want all the code in one repository, you can- you just need to follow Go's package rules. (this is a good read: https://golang.org/doc/code.html#Workspaces)
If you have a mix of commands and libraries, the directory structure you proposed in your question comes close, but you probably don't need the src
directories in there. Here's an example of how a directory structure within a repo with libraries and commands might look:
lib1/
-- some.go
-- source.go
lib2/
-- more.go
-- source.go
cmd/
-- microservice1/
-- main.go
-- microservice2/
-- anothermain.go
To use this repository, you would clone it inside a Go workspace on your system (see the link I shared above). Assuming your repository lives in github.com/mybiz/project, and your GOPATH
was ~/go
, the workspace would look as follows:
~/go/src/github.com/mybiz/
-- project/
<clone repo in here>
The file cmd/microservice1/main.go
would include the library lib1
via a path it expects it in relative to $GOPATH/src
as follows:
import "github.com/mybiz/project/lib1"
Now, your code has access to the exported symbols in that package using the package name declared in the files under lib1
... usually just:
package lib1
In cmd/microservice1/main.go
, with the import above, you could use lib1
symbols as follows:
lib1.CallMe()
I hope that helps clear up how Go's directory structure works.
Thanks, I'm not able to separate the repository into multiple separate repositories so I was thinking along the same lines. I came across a nice article which clarified some things: peter.bourgon.org/go-in-production but your answer also made things clearer.
– Rtsne42
Aug 20 '17 at 1:17
add a comment |
The other answer here advocates putting each microservice into its own repository. There may be valid reasons for splitting things up that way, but there may be equally valid reasons from wanting to keep everything in one repository as well (it really depends on your project / circumstances)
If you want all the code in one repository, you can- you just need to follow Go's package rules. (this is a good read: https://golang.org/doc/code.html#Workspaces)
If you have a mix of commands and libraries, the directory structure you proposed in your question comes close, but you probably don't need the src
directories in there. Here's an example of how a directory structure within a repo with libraries and commands might look:
lib1/
-- some.go
-- source.go
lib2/
-- more.go
-- source.go
cmd/
-- microservice1/
-- main.go
-- microservice2/
-- anothermain.go
To use this repository, you would clone it inside a Go workspace on your system (see the link I shared above). Assuming your repository lives in github.com/mybiz/project, and your GOPATH
was ~/go
, the workspace would look as follows:
~/go/src/github.com/mybiz/
-- project/
<clone repo in here>
The file cmd/microservice1/main.go
would include the library lib1
via a path it expects it in relative to $GOPATH/src
as follows:
import "github.com/mybiz/project/lib1"
Now, your code has access to the exported symbols in that package using the package name declared in the files under lib1
... usually just:
package lib1
In cmd/microservice1/main.go
, with the import above, you could use lib1
symbols as follows:
lib1.CallMe()
I hope that helps clear up how Go's directory structure works.
Thanks, I'm not able to separate the repository into multiple separate repositories so I was thinking along the same lines. I came across a nice article which clarified some things: peter.bourgon.org/go-in-production but your answer also made things clearer.
– Rtsne42
Aug 20 '17 at 1:17
add a comment |
The other answer here advocates putting each microservice into its own repository. There may be valid reasons for splitting things up that way, but there may be equally valid reasons from wanting to keep everything in one repository as well (it really depends on your project / circumstances)
If you want all the code in one repository, you can- you just need to follow Go's package rules. (this is a good read: https://golang.org/doc/code.html#Workspaces)
If you have a mix of commands and libraries, the directory structure you proposed in your question comes close, but you probably don't need the src
directories in there. Here's an example of how a directory structure within a repo with libraries and commands might look:
lib1/
-- some.go
-- source.go
lib2/
-- more.go
-- source.go
cmd/
-- microservice1/
-- main.go
-- microservice2/
-- anothermain.go
To use this repository, you would clone it inside a Go workspace on your system (see the link I shared above). Assuming your repository lives in github.com/mybiz/project, and your GOPATH
was ~/go
, the workspace would look as follows:
~/go/src/github.com/mybiz/
-- project/
<clone repo in here>
The file cmd/microservice1/main.go
would include the library lib1
via a path it expects it in relative to $GOPATH/src
as follows:
import "github.com/mybiz/project/lib1"
Now, your code has access to the exported symbols in that package using the package name declared in the files under lib1
... usually just:
package lib1
In cmd/microservice1/main.go
, with the import above, you could use lib1
symbols as follows:
lib1.CallMe()
I hope that helps clear up how Go's directory structure works.
The other answer here advocates putting each microservice into its own repository. There may be valid reasons for splitting things up that way, but there may be equally valid reasons from wanting to keep everything in one repository as well (it really depends on your project / circumstances)
If you want all the code in one repository, you can- you just need to follow Go's package rules. (this is a good read: https://golang.org/doc/code.html#Workspaces)
If you have a mix of commands and libraries, the directory structure you proposed in your question comes close, but you probably don't need the src
directories in there. Here's an example of how a directory structure within a repo with libraries and commands might look:
lib1/
-- some.go
-- source.go
lib2/
-- more.go
-- source.go
cmd/
-- microservice1/
-- main.go
-- microservice2/
-- anothermain.go
To use this repository, you would clone it inside a Go workspace on your system (see the link I shared above). Assuming your repository lives in github.com/mybiz/project, and your GOPATH
was ~/go
, the workspace would look as follows:
~/go/src/github.com/mybiz/
-- project/
<clone repo in here>
The file cmd/microservice1/main.go
would include the library lib1
via a path it expects it in relative to $GOPATH/src
as follows:
import "github.com/mybiz/project/lib1"
Now, your code has access to the exported symbols in that package using the package name declared in the files under lib1
... usually just:
package lib1
In cmd/microservice1/main.go
, with the import above, you could use lib1
symbols as follows:
lib1.CallMe()
I hope that helps clear up how Go's directory structure works.
answered Aug 20 '17 at 0:58
Eddy R.Eddy R.
66549
66549
Thanks, I'm not able to separate the repository into multiple separate repositories so I was thinking along the same lines. I came across a nice article which clarified some things: peter.bourgon.org/go-in-production but your answer also made things clearer.
– Rtsne42
Aug 20 '17 at 1:17
add a comment |
Thanks, I'm not able to separate the repository into multiple separate repositories so I was thinking along the same lines. I came across a nice article which clarified some things: peter.bourgon.org/go-in-production but your answer also made things clearer.
– Rtsne42
Aug 20 '17 at 1:17
Thanks, I'm not able to separate the repository into multiple separate repositories so I was thinking along the same lines. I came across a nice article which clarified some things: peter.bourgon.org/go-in-production but your answer also made things clearer.
– Rtsne42
Aug 20 '17 at 1:17
Thanks, I'm not able to separate the repository into multiple separate repositories so I was thinking along the same lines. I came across a nice article which clarified some things: peter.bourgon.org/go-in-production but your answer also made things clearer.
– Rtsne42
Aug 20 '17 at 1:17
add a comment |
How do I organize my projects?
|-- github.com/avelino/service1
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service2
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service3
|-- Dockerfile
|-- main.go
Packages
All Go code is organized into packages. A package in Go is simply a directory/folder with one or more .go files inside of it. Go packages provide isolation and organization of code similar to how directories/folders organize files on a computer.
All Go code lives in a package and a package is the entry point to access Go code. Understanding and establishing good practices around packages is important to write effective Go code.
1
Thanks for the response, so do you keep each service in a separate repository?
– Rtsne42
Aug 19 '17 at 21:50
Yep, isolating each service in your repository
– avelino
Aug 19 '17 at 23:04
1
yup, this is how microservice should be structured. Because each service should be independent to eachother which mean they should be separated their repo.
– mfathirirhas
Mar 7 '18 at 4:14
add a comment |
How do I organize my projects?
|-- github.com/avelino/service1
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service2
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service3
|-- Dockerfile
|-- main.go
Packages
All Go code is organized into packages. A package in Go is simply a directory/folder with one or more .go files inside of it. Go packages provide isolation and organization of code similar to how directories/folders organize files on a computer.
All Go code lives in a package and a package is the entry point to access Go code. Understanding and establishing good practices around packages is important to write effective Go code.
1
Thanks for the response, so do you keep each service in a separate repository?
– Rtsne42
Aug 19 '17 at 21:50
Yep, isolating each service in your repository
– avelino
Aug 19 '17 at 23:04
1
yup, this is how microservice should be structured. Because each service should be independent to eachother which mean they should be separated their repo.
– mfathirirhas
Mar 7 '18 at 4:14
add a comment |
How do I organize my projects?
|-- github.com/avelino/service1
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service2
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service3
|-- Dockerfile
|-- main.go
Packages
All Go code is organized into packages. A package in Go is simply a directory/folder with one or more .go files inside of it. Go packages provide isolation and organization of code similar to how directories/folders organize files on a computer.
All Go code lives in a package and a package is the entry point to access Go code. Understanding and establishing good practices around packages is important to write effective Go code.
How do I organize my projects?
|-- github.com/avelino/service1
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service2
|-- Dockerfile
|-- main.go
|-- github.com/avelino/service3
|-- Dockerfile
|-- main.go
Packages
All Go code is organized into packages. A package in Go is simply a directory/folder with one or more .go files inside of it. Go packages provide isolation and organization of code similar to how directories/folders organize files on a computer.
All Go code lives in a package and a package is the entry point to access Go code. Understanding and establishing good practices around packages is important to write effective Go code.
answered Aug 19 '17 at 21:41
avelinoavelino
20315
20315
1
Thanks for the response, so do you keep each service in a separate repository?
– Rtsne42
Aug 19 '17 at 21:50
Yep, isolating each service in your repository
– avelino
Aug 19 '17 at 23:04
1
yup, this is how microservice should be structured. Because each service should be independent to eachother which mean they should be separated their repo.
– mfathirirhas
Mar 7 '18 at 4:14
add a comment |
1
Thanks for the response, so do you keep each service in a separate repository?
– Rtsne42
Aug 19 '17 at 21:50
Yep, isolating each service in your repository
– avelino
Aug 19 '17 at 23:04
1
yup, this is how microservice should be structured. Because each service should be independent to eachother which mean they should be separated their repo.
– mfathirirhas
Mar 7 '18 at 4:14
1
1
Thanks for the response, so do you keep each service in a separate repository?
– Rtsne42
Aug 19 '17 at 21:50
Thanks for the response, so do you keep each service in a separate repository?
– Rtsne42
Aug 19 '17 at 21:50
Yep, isolating each service in your repository
– avelino
Aug 19 '17 at 23:04
Yep, isolating each service in your repository
– avelino
Aug 19 '17 at 23:04
1
1
yup, this is how microservice should be structured. Because each service should be independent to eachother which mean they should be separated their repo.
– mfathirirhas
Mar 7 '18 at 4:14
yup, this is how microservice should be structured. Because each service should be independent to eachother which mean they should be separated their repo.
– mfathirirhas
Mar 7 '18 at 4:14
add a comment |
I'm structuring it like this; mono-repo per. project approach. Taking into account that these services are closely related:
github.com/user/some_project/
├── pkg/ (common own-created packages for all services)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── sd/
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/
| ├── account/
| | ├── pb/
| | | ├── account.proto
| | | └── account.pb.go
| | ├── handler.go
| | ├── main.go
| | ├── main_test.go
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── frontend/
| └── user/
├── vendor/ (common vendor-packages for all services)
├── Gopkg.lock
├── Gopkg.toml
├── Makefile
├── README.md
└── docker-compose.yml
Later, when you start on your second microservices project, many of the packages in the /pkg directory would be required there too. What to do? Copy/paste? No! Instead, extract these packages from the project, i.e. log, metric... into a new separate repository, so that all projects imports these from that one repo.
Congrats, you are now also a great step on your way to create your own microservices-framework! Or at least your own kit :)
add a comment |
I'm structuring it like this; mono-repo per. project approach. Taking into account that these services are closely related:
github.com/user/some_project/
├── pkg/ (common own-created packages for all services)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── sd/
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/
| ├── account/
| | ├── pb/
| | | ├── account.proto
| | | └── account.pb.go
| | ├── handler.go
| | ├── main.go
| | ├── main_test.go
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── frontend/
| └── user/
├── vendor/ (common vendor-packages for all services)
├── Gopkg.lock
├── Gopkg.toml
├── Makefile
├── README.md
└── docker-compose.yml
Later, when you start on your second microservices project, many of the packages in the /pkg directory would be required there too. What to do? Copy/paste? No! Instead, extract these packages from the project, i.e. log, metric... into a new separate repository, so that all projects imports these from that one repo.
Congrats, you are now also a great step on your way to create your own microservices-framework! Or at least your own kit :)
add a comment |
I'm structuring it like this; mono-repo per. project approach. Taking into account that these services are closely related:
github.com/user/some_project/
├── pkg/ (common own-created packages for all services)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── sd/
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/
| ├── account/
| | ├── pb/
| | | ├── account.proto
| | | └── account.pb.go
| | ├── handler.go
| | ├── main.go
| | ├── main_test.go
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── frontend/
| └── user/
├── vendor/ (common vendor-packages for all services)
├── Gopkg.lock
├── Gopkg.toml
├── Makefile
├── README.md
└── docker-compose.yml
Later, when you start on your second microservices project, many of the packages in the /pkg directory would be required there too. What to do? Copy/paste? No! Instead, extract these packages from the project, i.e. log, metric... into a new separate repository, so that all projects imports these from that one repo.
Congrats, you are now also a great step on your way to create your own microservices-framework! Or at least your own kit :)
I'm structuring it like this; mono-repo per. project approach. Taking into account that these services are closely related:
github.com/user/some_project/
├── pkg/ (common own-created packages for all services)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── sd/
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/
| ├── account/
| | ├── pb/
| | | ├── account.proto
| | | └── account.pb.go
| | ├── handler.go
| | ├── main.go
| | ├── main_test.go
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── frontend/
| └── user/
├── vendor/ (common vendor-packages for all services)
├── Gopkg.lock
├── Gopkg.toml
├── Makefile
├── README.md
└── docker-compose.yml
Later, when you start on your second microservices project, many of the packages in the /pkg directory would be required there too. What to do? Copy/paste? No! Instead, extract these packages from the project, i.e. log, metric... into a new separate repository, so that all projects imports these from that one repo.
Congrats, you are now also a great step on your way to create your own microservices-framework! Or at least your own kit :)
edited Jun 16 '18 at 14:28
answered Feb 19 '18 at 18:35
Karl AndresenKarl Andresen
339
339
add a comment |
add a comment |
Thanks to @karl-andresen. I was doing research on the same topic and came up with the below structure hope this helps someone
github.com/username/container/
├── pkg/ ('username' created packages - common for all services & reusable in other projects)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── infra/ (sub category in packages)
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/ (where all microservices will be imported as submodules - may or may not be reused)
| ├── account/
| | ├── handler.go
| | ├── handler_test.go (unit testing, note filename with '_test')
| | ├── main.go
| | ├── main_test.go (another unit testing)
| | ├── account.cfg (configuration file for account microservice)
| | ├── submodule/ (sub directory)
| | | ├── submodule.go
| | | └── submodule_test.go (submodule unit test)
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── booking/
| └── user/
├── api/ (OpenAPI/Swagger specs, JSON schema files, protocol definition files.)
| ├── proto/ (protocol buffer files)
| | ├── v1/
| | | ├── account.proto
| | | ├── account.pb.go
| | | ├── booking.proto
| | | └── booking.pb.go
| | └── v2/
| └── rest/ (json files)
| ├── v1/
| | ├── booking.json
| | └── account.json
| └── v2/
├── configs/ (project config settings, default configs, file templates)
├── scripts/ (Scripts to perform various build, install, analysis, etc operations.)
├── build/ (Packaging and Continuous Integration.)
├── test / (system and module level tests)
├── docs/ (project documents folder)
├── examples/ (project examples for service interactions)
├── third_party/ (all open source, third party codes, where applicable fork and add as submodule)
├── githooks/ (project git hooks)
├── assets/ (common assests for all services)
├── Makefile
├── README.md
└── docker-compose.yml
add a comment |
Thanks to @karl-andresen. I was doing research on the same topic and came up with the below structure hope this helps someone
github.com/username/container/
├── pkg/ ('username' created packages - common for all services & reusable in other projects)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── infra/ (sub category in packages)
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/ (where all microservices will be imported as submodules - may or may not be reused)
| ├── account/
| | ├── handler.go
| | ├── handler_test.go (unit testing, note filename with '_test')
| | ├── main.go
| | ├── main_test.go (another unit testing)
| | ├── account.cfg (configuration file for account microservice)
| | ├── submodule/ (sub directory)
| | | ├── submodule.go
| | | └── submodule_test.go (submodule unit test)
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── booking/
| └── user/
├── api/ (OpenAPI/Swagger specs, JSON schema files, protocol definition files.)
| ├── proto/ (protocol buffer files)
| | ├── v1/
| | | ├── account.proto
| | | ├── account.pb.go
| | | ├── booking.proto
| | | └── booking.pb.go
| | └── v2/
| └── rest/ (json files)
| ├── v1/
| | ├── booking.json
| | └── account.json
| └── v2/
├── configs/ (project config settings, default configs, file templates)
├── scripts/ (Scripts to perform various build, install, analysis, etc operations.)
├── build/ (Packaging and Continuous Integration.)
├── test / (system and module level tests)
├── docs/ (project documents folder)
├── examples/ (project examples for service interactions)
├── third_party/ (all open source, third party codes, where applicable fork and add as submodule)
├── githooks/ (project git hooks)
├── assets/ (common assests for all services)
├── Makefile
├── README.md
└── docker-compose.yml
add a comment |
Thanks to @karl-andresen. I was doing research on the same topic and came up with the below structure hope this helps someone
github.com/username/container/
├── pkg/ ('username' created packages - common for all services & reusable in other projects)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── infra/ (sub category in packages)
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/ (where all microservices will be imported as submodules - may or may not be reused)
| ├── account/
| | ├── handler.go
| | ├── handler_test.go (unit testing, note filename with '_test')
| | ├── main.go
| | ├── main_test.go (another unit testing)
| | ├── account.cfg (configuration file for account microservice)
| | ├── submodule/ (sub directory)
| | | ├── submodule.go
| | | └── submodule_test.go (submodule unit test)
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── booking/
| └── user/
├── api/ (OpenAPI/Swagger specs, JSON schema files, protocol definition files.)
| ├── proto/ (protocol buffer files)
| | ├── v1/
| | | ├── account.proto
| | | ├── account.pb.go
| | | ├── booking.proto
| | | └── booking.pb.go
| | └── v2/
| └── rest/ (json files)
| ├── v1/
| | ├── booking.json
| | └── account.json
| └── v2/
├── configs/ (project config settings, default configs, file templates)
├── scripts/ (Scripts to perform various build, install, analysis, etc operations.)
├── build/ (Packaging and Continuous Integration.)
├── test / (system and module level tests)
├── docs/ (project documents folder)
├── examples/ (project examples for service interactions)
├── third_party/ (all open source, third party codes, where applicable fork and add as submodule)
├── githooks/ (project git hooks)
├── assets/ (common assests for all services)
├── Makefile
├── README.md
└── docker-compose.yml
Thanks to @karl-andresen. I was doing research on the same topic and came up with the below structure hope this helps someone
github.com/username/container/
├── pkg/ ('username' created packages - common for all services & reusable in other projects)
| ├── errors/
| ├── log/
| ├── metrics/
| ├── infra/ (sub category in packages)
| | ├── consul/
| | └── kubernetes/
| └── tracing/
├── services/ (where all microservices will be imported as submodules - may or may not be reused)
| ├── account/
| | ├── handler.go
| | ├── handler_test.go (unit testing, note filename with '_test')
| | ├── main.go
| | ├── main_test.go (another unit testing)
| | ├── account.cfg (configuration file for account microservice)
| | ├── submodule/ (sub directory)
| | | ├── submodule.go
| | | └── submodule_test.go (submodule unit test)
| | ├── Dockerfile
| | └── README.md
| ├── auth/
| ├── booking/
| └── user/
├── api/ (OpenAPI/Swagger specs, JSON schema files, protocol definition files.)
| ├── proto/ (protocol buffer files)
| | ├── v1/
| | | ├── account.proto
| | | ├── account.pb.go
| | | ├── booking.proto
| | | └── booking.pb.go
| | └── v2/
| └── rest/ (json files)
| ├── v1/
| | ├── booking.json
| | └── account.json
| └── v2/
├── configs/ (project config settings, default configs, file templates)
├── scripts/ (Scripts to perform various build, install, analysis, etc operations.)
├── build/ (Packaging and Continuous Integration.)
├── test / (system and module level tests)
├── docs/ (project documents folder)
├── examples/ (project examples for service interactions)
├── third_party/ (all open source, third party codes, where applicable fork and add as submodule)
├── githooks/ (project git hooks)
├── assets/ (common assests for all services)
├── Makefile
├── README.md
└── docker-compose.yml
answered Dec 29 '18 at 10:23
rajeshkrajeshk
3821617
3821617
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45776238%2fgolang-microservice-project-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown