Use Go language recommended directory structure (golang-standards / project-layout) for local (private source) projects

go version go1.15.2

According to Recommended Directory Structure of Go Language Community, the standard package management system "Go Modules / go / wiki / Modules) ”, I was subtly addicted to it. I wrote the same article as a memo on my site, but I also dropped it on Qiita because I wanted Tsukkomi.

Directory configuration example

Names (and subelements) with an exclamation mark (!) Are arbitrary identifiers.

my_project(!)
├── web #External resources used by the web system. js or images
├── assets #Off-system resources used outside the Web system.
├── cmd #Store main module
│   ├── mainexe(!) #Executable file in the project. Build in this unit
│   │   └── main.go
│   └── confexe(!) #Another executable file example
│       └── main.go
├── configs #setting file
│   └── main.yaml(!)
├── docs #document. Because it's for myself, I'm putting a note in it
│   └── readme.md
├── internal #Private module
│   ├── app #A module dedicated to this project. Is it business logic?
│   │   ├── mainexe(!) #Separate by executable file
│   │   │   └ mainview #Service-like feeling in MVCS?
│   │   │       └ mainview.go
│   │   └── confexe(!)
│   │       └ mainview
│   │           └ mainview.go
│   └── pkg #Private general purpose module
│       └ my_lib(!) #Something to reuse
├── scripts #Shell used at build time, etc.
└── go.mod #Files generated by Go Module

Initialization of Go Moduls

cd my_project
go mod init 192.168.0.0/my_project

In that respect, the host name was set to 192.168.0.0.

Not published as a module = It is not obtained by go get, so it has no meaning more than just a character string. (I think) Anything should be fine as long as it contains the period (.) That is the rule of the host name. Initialize including the whole, not under cmd (module unit).

Use modules in internal from main module

package main

import (
	"192.168.0.0/my_project/internal/app/mainexe/mainview"
)

func main() {
	mainview.SomeFunction()
}

You can use it without any problem.

build

go build -o mainexe cmd/mainexe/*.go

The o option specifies the output file name. In the above example, it will be output directly under the project directory. You may create a directory such as dist and include it as the output destination path.

Personally, I created build.sh in the scripts directory and Write the above command and execute it with "./scripts/mainexe_build.sh".

Golang has been abandoned for a long time, so I'm still a beginner, although there are some old articles. If you have any tsukkomi, please m (_ _) m

Recommended Posts

Use Go language recommended directory structure (golang-standards / project-layout) for local (private source) projects