Go Project: Difference between revisions
Line 22: | Line 22: | ||
├── [[#pkg|pkg]] | ├── [[#pkg|pkg]] | ||
├── [[#internal|internal]] | ├── [[#internal|internal]] | ||
├── [[#scripts|scripts]] | |||
├── Makefile | ├── Makefile | ||
├── README.md | ├── README.md | ||
Line 49: | Line 50: | ||
===<tt>pkg</tt>=== | ===<tt>pkg</tt>=== | ||
===<tt>internal</tt>=== | ===<tt>internal</tt>=== | ||
===<tt>scripts</tt>=== | |||
=Microservice-based Project Layout= | =Microservice-based Project Layout= |
Revision as of 19:16, 14 December 2023
External
Internal
Overview
This page collects recommendation and facts about Go projects and it was mainly written with the assumption that one project lives in its own repository and contains a single module, as described in Packages, Modules, Projects and Repositories. Of course project can contain multiple modules, but that is not recommended.
Project Layout
This layout is inspired by:
. ├── cmd │ ├── myserver │ │ └── main.go │ └── myclient │ └── main.go ├── pkg ├── internal ├── scripts ├── Makefile ├── README.md ├── go.mod ├── go.sum └── .gitignore
Directories
cmd
The directory contains the main applications for this project. If the project has multiple executables, their code should live under their own directory, and the name of the directory should match the name of the executable. The Go code file name can be either main.go
(preferred) or the name of the command (myserver.go
). Different projects use one of these two conventions. Do not put a lot of code in the /cmd
directory. If you think the code can be imported and used in other projects, put it in the /pkg
directory. If the code is not reusable, put it in the /internal
directory. It's common to have a small main
function that imports and invokes the code from the /internal
and /pkg
directories and nothing else.
main.go
Some project name this file myserver.go
.
package main
import "fmt"
func main() {
fmt.Print("starting my service ...\n")
}
pkg
internal
scripts
Microservice-based Project Layout
TODO
Integrate Vendoring.