From 31462cc014027db8e8cc8f1a3d5a4e049e327cea Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Thu, 4 Aug 2022 13:24:31 +0700 Subject: [PATCH] feat: stop using global var --- docs/2022-07-10-bootstrap-go.html | 2 +- posts/2022-07-10-bootstrap-go.md | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/2022-07-10-bootstrap-go.html b/docs/2022-07-10-bootstrap-go.html index 1c8665e..ea67cc9 100644 --- a/docs/2022-07-10-bootstrap-go.html +++ b/docs/2022-07-10-bootstrap-go.html @@ -15,7 +15,7 @@ internal | | service.go | | repository.go | | models.go -

All business codes are inside internal.
Each business has a different directory business.

Inside each business, there are 2 handlers: http, grpc:

For each handler, there are usually 3 layers: handler, service, repository:

Location:

Do not repeat!

If we have too many services, some of the logic will be overlapped.

For example, service A and service B both need to make POST call API to service C.
If service A and service B both have libs to call service C to do that API, we need to move the libs to some common pkg libs.
So in the future, service D which needs to call C will not need to copy libs to handle service C api but only need to import from common pkg libs.

Another bad practice is adapter service.
No need to write a new service if what we need is just common pkg libs.

Taste on style guide

Use functional options, but don't overuse it!

For simple struct with 1 or 2 fields, no need to use functional options.

Example:

func main() {
+

All business codes are inside internal.
Each business has a different directory business.

Inside each business, there are 2 handlers: http, grpc:

For each handler, there are usually 3 layers: handler, service, repository:

Location:

Do not repeat!

If we have too many services, some of the logic will be overlapped.

For example, service A and service B both need to make POST call API to service C.
If service A and service B both have libs to call service C to do that API, we need to move the libs to some common pkg libs.
So in the future, service D which needs to call C will not need to copy libs to handle service C api but only need to import from common pkg libs.

Another bad practice is adapter service.
No need to write a new service if what we need is just common pkg libs.

Taste on style guide

Stop using global var

If I see someone using global var, I swear I shoot twice in the face.

Why?

Use functional options, but don't overuse it!

For simple struct with 1 or 2 fields, no need to use functional options.

Example:

func main() {
 	s := NewS(WithA(1), WithB("b"))
 	fmt.Printf("%+v\n", s)
 }
diff --git a/posts/2022-07-10-bootstrap-go.md b/posts/2022-07-10-bootstrap-go.md
index 6d71a14..6dc1d41 100644
--- a/posts/2022-07-10-bootstrap-go.md
+++ b/posts/2022-07-10-bootstrap-go.md
@@ -62,6 +62,15 @@ No need to write a new service if what we need is just common pkg libs.
 
 ## Taste on style guide
 
+### Stop using global var
+
+If I see someone using global var, I swear I shoot twice in the face.
+
+Why?
+
+- Can not write unit test.
+- Is not thread safe.
+
 ### Use functional options, but don't overuse it!
 
 For simple struct with 1 or 2 fields, no need to use functional options.