Skip to content

Commit beb599f

Browse files
authored
feat: introduce v4 (#88)
* chore: initial the new code structure * feat: restrcuture the code directory * chore: add minor comment * fix: use go-generate for mockery * fix: typos * chore: fix typos * chore: rename usecase to service for better clarity * chore: rename to service * chore: fix typo * chore: add author's note
1 parent d5d2bac commit beb599f

35 files changed

+631
-529
lines changed

.air.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ full_bin = "./tmp_app/app/engine"
1616
log = "air_errors.log"
1717
# Watch these filename extensions.
1818
include_ext = ["go", "yaml", "toml"]
19+
# Exclude specific regular expressions.
20+
exclude_regex = ["_test\\.go"]
1921
# Ignore these filename extensions or directories.
2022
exclude_dir = ["tmp_app", "tmp"]
2123
# It's not necessary to trigger build each time file changes if it's too frequent.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ _*
66
.DS_Store
77
engine
88
bin/
9-
*.out
9+
*.out
10+
tmp_app/
11+
.env

.golangci.yaml

Lines changed: 29 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,4 @@
11
linters-settings:
2-
dupl:
3-
threshold: 300
4-
funlen:
5-
lines: 200
6-
statements: 55
7-
goconst:
8-
min-len: 2
9-
min-occurrences: 3
10-
gocritic:
11-
enabled-tags:
12-
- diagnostic
13-
- experimental
14-
- performance
15-
- style
16-
- opinionated
17-
disabled-checks:
18-
- dupImport # https://github.com/go-critic/go-critic/issues/845
19-
- ifElseChain
20-
- octalLiteral
21-
- whyNoLint
22-
- wrapperFunc
23-
- hugeParam
24-
gocyclo:
25-
min-complexity: 20
26-
gomnd:
27-
# don't include the "operation" and "assign"
28-
checks:
29-
- argument
30-
- case
31-
- condition
32-
- return
33-
ignored-numbers:
34-
- "0"
35-
- "1"
36-
- "2"
37-
- "3"
38-
ignored-functions:
39-
- strings.SplitN
402
govet:
413
check-shadowing: true
424
settings:
@@ -46,85 +8,62 @@ linters-settings:
468
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
479
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
4810
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
49-
lll:
50-
line-length: 160
11+
gocyclo:
12+
min-complexity: 50
13+
maligned:
14+
suggest-new: true
15+
dupl:
16+
threshold: 100
17+
goconst:
18+
min-len: 2
19+
min-occurrences: 2
5120
misspell:
5221
locale: US
53-
nolintlint:
54-
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
55-
allow-unused: false # report any unused nolint directives
56-
require-explanation: false # don't require an explanation for nolint directives
57-
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
22+
revive:
23+
confidence: 0.8
24+
lll:
25+
line-length: 160
26+
# tab width in spaces. Default to 1.
27+
tab-width: 1
28+
funlen:
29+
lines: 150
30+
statements: 80
5831

5932
linters:
33+
# please, do not use `enable-all`: it's deprecated and will be removed soon.
34+
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
6035
disable-all: true
6136
enable:
62-
- asciicheck
63-
- bodyclose
64-
# - deadcode
65-
- depguard
66-
- dogsled
67-
- dupl
6837
- errcheck
69-
- exportloopref
7038
- funlen
71-
- gochecknoinits
7239
- goconst
73-
- gocritic
7440
- gocyclo
75-
- gofmt
76-
- goimports
77-
- gomnd
78-
- goprintffuncname
7941
- gosec
8042
- gosimple
43+
- govet
8144
- ineffassign
8245
- lll
8346
- misspell
84-
- nakedret
85-
- noctx
86-
# - nolintlint
47+
- revive
8748
- staticcheck
88-
# - structcheck
89-
- stylecheck
9049
- typecheck
9150
- unconvert
9251
- unparam
9352
- unused
94-
- revive
95-
# - varcheck
96-
- whitespace
9753

9854
# don't enable:
99-
#
100-
# - scopelint
10155
# - gochecknoglobals
10256
# - gocognit
103-
# - godot
10457
# - godox
105-
# - goerr113
106-
# - interfacer
10758
# - maligned
108-
# - nestif
10959
# - prealloc
110-
# - testpackage
111-
# - wsl
112-
113-
issues:
114-
# Excluding configuration per-path, per-linter, per-text and per-source
115-
exclude-rules:
116-
- path: _test\.go
117-
linters:
118-
- gomnd
119-
- dupl
120-
- goconst
12160

122-
# - path: pkg/xerrors/xerrors.go
123-
# text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
124-
- path: app
125-
text: "don't use `init` function"
12661
run:
127-
timeout: 5m
128-
go: "1.17"
12962
skip-dirs:
130-
# - */mocks
63+
# - test/testdata_etc
64+
skip-files:
65+
- ".*_test\\.go$"
66+
67+
issues:
68+
exclude-rules:
69+
#

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Builder
2-
FROM golang:1.19.4-alpine3.17 as builder
2+
FROM golang:1.20.7-alpine3.17 as builder
33

44
RUN apk update && apk upgrade && \
55
apk --update add git make bash build-base

Makefile

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export PATH := $(PWD)/bin:$(PATH)
99
# Default Shell
1010
export SHELL := bash
1111
# Type of OS: Linux or Darwin.
12-
export OSTYPE := $(shell uname -s)
12+
export OSTYPE := $(shell uname -s | tr A-Z a-z)
13+
export ARCH := $(shell uname -m)
14+
1315

1416

1517
# --- Tooling & Variables ----------------------------------------------------------------
@@ -23,7 +25,7 @@ down: docker-stop ## Stop Docker
2325
destroy: docker-teardown clean ## Teardown (removes volumes, tmp files, etc...)
2426

2527
install-deps: migrate air gotestsum tparse mockery ## Install Development Dependencies (localy).
26-
deps: $(MIGRATE) $(AIR) $(GOTESTSUM) $(TPARSE) $(MOCKERY) ## Checks for Global Development Dependencies.
28+
deps: $(MIGRATE) $(AIR) $(GOTESTSUM) $(TPARSE) $(MOCKERY) $(GOLANGCI) ## Checks for Global Development Dependencies.
2729
deps:
2830
@echo "Required Tools Are Available"
2931

@@ -89,11 +91,11 @@ TESTS_ARGS += -test.coverprofile coverage.out
8991
TESTS_ARGS += -test.timeout 5s
9092
TESTS_ARGS += -race
9193

92-
run-tests: $(GOTESTSUM)
94+
tests: $(GOTESTSUM)
9395
@ gotestsum $(TESTS_ARGS) -short
9496

95-
tests: run-tests $(TPARSE) ## Run Tests & parse details
96-
@cat gotestsum.json.out | $(TPARSE) -all -top -notests
97+
tests-complete: tests $(TPARSE) ## Run Tests & parse details
98+
@cat gotestsum.json.out | $(TPARSE) -all -notests
9799

98100
# ~~~ Docker Build ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99101

@@ -105,28 +107,31 @@ image-build:
105107
--tag go-clean-arch \
106108
.
107109

108-
# ~~~ Database Migrations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110+
# Commenting this as this not relevant for the project, we load the DB data from the SQL file.
111+
# please refer this when introducing the database schema migrations.
112+
113+
# # ~~~ Database Migrations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109114

110115

111-
MYSQL_DSN := "mysql://$(MYSQL_USER):$(MYSQL_PASSWORD)@tcp($(MYSQL_ADDRESS))/$(MYSQL_DATABASE)"
116+
# MYSQL_DSN := "mysql://$(MYSQL_USER):$(MYSQL_PASSWORD)@tcp($(MYSQL_ADDRESS))/$(MYSQL_DATABASE)"
112117

113-
migrate-up: $(MIGRATE) ## Apply all (or N up) migrations.
114-
@ read -p "How many migration you wants to perform (default value: [all]): " N; \
115-
migrate -database $(MYSQL_DSN) -path=misc/migrations up ${NN}
118+
# migrate-up: $(MIGRATE) ## Apply all (or N up) migrations.
119+
# @ read -p "How many migration you wants to perform (default value: [all]): " N; \
120+
# migrate -database $(MYSQL_DSN) -path=misc/migrations up ${NN}
116121

117-
.PHONY: migrate-down
118-
migrate-down: $(MIGRATE) ## Apply all (or N down) migrations.
119-
@ read -p "How many migration you wants to perform (default value: [all]): " N; \
120-
migrate -database $(MYSQL_DSN) -path=misc/migrations down ${NN}
122+
# .PHONY: migrate-down
123+
# migrate-down: $(MIGRATE) ## Apply all (or N down) migrations.
124+
# @ read -p "How many migration you wants to perform (default value: [all]): " N; \
125+
# migrate -database $(MYSQL_DSN) -path=misc/migrations down ${NN}
121126

122-
.PHONY: migrate-drop
123-
migrate-drop: $(MIGRATE) ## Drop everything inside the database.
124-
migrate -database $(MYSQL_DSN) -path=misc/migrations drop
127+
# .PHONY: migrate-drop
128+
# migrate-drop: $(MIGRATE) ## Drop everything inside the database.
129+
# migrate -database $(MYSQL_DSN) -path=misc/migrations drop
125130

126-
.PHONY: migrate-create
127-
migrate-create: $(MIGRATE) ## Create a set of up/down migrations with a specified name.
128-
@ read -p "Please provide name for the migration: " Name; \
129-
migrate create -ext sql -dir misc/migrations $${Name}
131+
# .PHONY: migrate-create
132+
# migrate-create: $(MIGRATE) ## Create a set of up/down migrations with a specified name.
133+
# @ read -p "Please provide name for the migration: " Name; \
134+
# migrate create -ext sql -dir misc/migrations $${Name}
130135

131136
# ~~~ Cleans ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132137

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,24 @@
1010
Proposed on 2018, archived to v2 branch on 2020 <br>
1111
Desc: Improvement from v1. The story can be read here: https://medium.com/@imantumorang/trying-clean-architecture-on-golang-2-44d615bf8fdf
1212

13-
- **v3**: master branch <br>
13+
- **v3**: checkout to the [v3 branch](https://github.com/bxcodec/go-clean-arch/tree/v3) <br>
1414
Proposed on 2019, merged to master on 2020. <br>
1515
Desc: Introducing Domain package, the details can be seen on this PR [#21](https://github.com/bxcodec/go-clean-arch/pull/21)
1616

17+
- **v4**: master branch
18+
Proposed on 2024, merged to master on 2024. <br>
19+
Desc:
20+
21+
- Declare Interfaces to the consuming side,
22+
- Introduce `internal` package
23+
- Introduce `Service-focused` package.
24+
25+
Details can be seen in this PR [#88](https://github.com/bxcodec/go-clean-arch/pull/88).<br>
26+
27+
> ### Author's Note
28+
>
29+
> You may notice it diverges from the structures seen in previous versions. I encourage you to explore the branches for each version to select the structure that appeals to you the most. In my recent projects, the code structure has progressed to version 4. However, I do not strictly advocate for one version over another. You may encounter alternative examples on the internet that align more closely with your preferences. Rest assured, the foundational concept will remain consistent or at least bear resemblance. The differences are primarily in the arrangement of directories or the integration of advanced tools directly into the setup.
30+
1731
## Description
1832

1933
This is an example of implementation of Clean Architecture in Go (Golang) projects.
@@ -40,14 +54,13 @@ This project has 4 Domain layer :
4054
![golang clean architecture](https://github.com/bxcodec/go-clean-arch/raw/master/clean-arch.png)
4155

4256
The original explanation about this project's structure can read from this medium's post : https://medium.com/@imantumorang/golang-clean-archithecture-efd6d7c43047.
43-
44-
It may different already, but the concept still the same in application level, also you can see the change log from v1 to current version in Master.
57+
It may be different already, but the concept still the same in application level, also you can see the change log from v1 to current version in Master.
4558

4659
### How To Run This Project
4760

4861
> Make Sure you have run the article.sql in your mysql
4962
50-
Since the project already use Go Module, I recommend to put the source code in any folder but GOPATH.
63+
Since the project is already use Go Module, I recommend to put the source code in any folder but GOPATH.
5164

5265
#### Run the Testing
5366

@@ -69,6 +82,9 @@ $ git clone https://github.com/bxcodec/go-clean-arch.git
6982
#move to project
7083
$ cd go-clean-arch
7184

85+
# copy the example.env to .env
86+
$ cp example.env .env
87+
7288
# Run the application
7389
$ make up
7490

@@ -80,7 +96,7 @@ $ curl localhost:9090/articles
8096

8197
### Tools Used:
8298

83-
In this project, I use some tools listed below. But you can use any simmilar library that have the same purposes. But, well, different library will have different implementation type. Just be creative and use anything that you really need.
99+
In this project, I use some tools listed below. But you can use any similar library that have the same purposes. But, well, different library will have different implementation type. Just be creative and use anything that you really need.
84100

85101
- All libraries listed in [`go.mod`](https://github.com/bxcodec/go-clean-arch/blob/master/go.mod)
86102
- ["github.com/vektra/mockery".](https://github.com/vektra/mockery) To Generate Mocks for testing needs.

0 commit comments

Comments
 (0)