diff --git a/docs/2022-07-10-bootstrap-go.html b/docs/2022-07-10-bootstrap-go.html index c8a17e5..57dff78 100644 --- a/docs/2022-07-10-bootstrap-go.html +++ b/docs/2022-07-10-bootstrap-go.html @@ -20,4 +20,35 @@ internal | | | service.go | | | repository.go | | | models.go -

All business codes are inside internal.
Each business has a different directory (business_1, business_2).

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

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

handler must exist inside grpc, http.
But service, repository, models can exist directly inside business if both grpc, http has same business/logic.

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.

External libs

Don't use cli libs (spf13/cobra, urfave/cli) just for Go service

What is the point to pass many params (--abc, --xyz) when what we only need is start service?

In my case, service starts with only config, and config should be read from file or environment like The Twelve Factors guide.

Don't use grpc-ecosystem/grpc-gateway

Just don't.

Use protocolbuffers/protobuf-go, grpc/grpc-go for gRPC.

Write 1 for both gRPC, REST sounds good, but in the end, it is not worth it.

Don't use uber/prototool, use bufbuild/buf

prototool is deprecated, and buf can generate, lint, format as good as prototool.

Use gin-gonic/gin for REST.

Don't use gin.Context when pass context from handler layer to service layer, use gin.Context.Request.Context() instead.

If you want log, just use uber-go/zap

It is fast!

Don't overuse ORM libs, no need to handle another layer above SQL.

Each ORM libs has each different syntax.
To learn and use those libs correctly is time consuming.
So just stick to plain SQL.
It is easier to debug when something is wrong.

But database/sql has its own limit.
For example, it is hard to get primary key after insert/update.
So may be you want to use ORM for those cases.

If you want test, just use stretchr/testify.

It is easy to write a suite test, thanks to testify.
Also, for mocking, there are many options out there.
Pick 1 then sleep peacefully.

Replace go fmt, goimports with mvdan/gofumpt.

gofumpt provides more rules when format Go codes.

Use golangci/golangci-lint.

No need to say more.
Lint or get the f out! \ No newline at end of file +

All business codes are inside internal.
Each business has a different directory (business_1, business_2).

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

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

handler must exist inside grpc, http.
But service, repository, models can exist directly inside business if both grpc, http has same business/logic.

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() {
+	s := NewS(WithA(1), WithB("b"))
+	fmt.Printf("%+v\n", s)
+}
+
+type S struct {
+	fieldA int
+	fieldB string
+}
+
+type OptionS func(s *S)
+
+func WithA(a int) OptionS {
+	return func(s *S) {
+		s.fieldA = a
+	}
+}
+
+func WithB(b string) OptionS {
+	return func(s *S) {
+		s.fieldB = b
+	}
+}
+
+func NewS(opts ...OptionS) *S {
+	s := &S{}
+	for _, opt := range opts {
+		opt(s)
+	}
+	return s
+}
+

In above example, I construct s with WithA and WithB option.
No need to pass direct field inside s.

External libs

Don't use cli libs (spf13/cobra, urfave/cli) just for Go service

What is the point to pass many params (--abc, --xyz) when what we only need is start service?

In my case, service starts with only config, and config should be read from file or environment like The Twelve Factors guide.

Don't use grpc-ecosystem/grpc-gateway

Just don't.

Use protocolbuffers/protobuf-go, grpc/grpc-go for gRPC.

Write 1 for both gRPC, REST sounds good, but in the end, it is not worth it.

Don't use uber/prototool, use bufbuild/buf

prototool is deprecated, and buf can generate, lint, format as good as prototool.

Use gin-gonic/gin for REST.

Don't use gin.Context when pass context from handler layer to service layer, use gin.Context.Request.Context() instead.

If you want log, just use uber-go/zap

It is fast!

Don't overuse ORM libs, no need to handle another layer above SQL.

Each ORM libs has each different syntax.
To learn and use those libs correctly is time consuming.
So just stick to plain SQL.
It is easier to debug when something is wrong.

But database/sql has its own limit.
For example, it is hard to get primary key after insert/update.
So may be you want to use ORM for those cases.

If you want test, just use stretchr/testify.

It is easy to write a suite test, thanks to testify.
Also, for mocking, there are many options out there.
Pick 1 then sleep peacefully.

Replace go fmt, goimports with mvdan/gofumpt.

gofumpt provides more rules when format Go codes.

Use golangci/golangci-lint.

No need to say more.
Lint or get the f out!

Thanks

\ No newline at end of file diff --git a/posts/2022-07-10-bootstrap-go.md b/posts/2022-07-10-bootstrap-go.md index fe8379f..6bc3dc8 100644 --- a/posts/2022-07-10-bootstrap-go.md +++ b/posts/2022-07-10-bootstrap-go.md @@ -60,6 +60,51 @@ So in the future, service D which needs to call C will not need to copy libs to 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](https://go.dev/play/p/0XnOLiHuoz3): + +```go +func main() { + s := NewS(WithA(1), WithB("b")) + fmt.Printf("%+v\n", s) +} + +type S struct { + fieldA int + fieldB string +} + +type OptionS func(s *S) + +func WithA(a int) OptionS { + return func(s *S) { + s.fieldA = a + } +} + +func WithB(b string) OptionS { + return func(s *S) { + s.fieldB = b + } +} + +func NewS(opts ...OptionS) *S { + s := &S{} + for _, opt := range opts { + opt(s) + } + return s +} +``` + +In above example, I construct `s` with `WithA` and `WithB` option. +No need to pass direct field inside `s`. + ## External libs ### Don't use cli libs ([spf13/cobra](https://github.com/spf13/cobra), [urfave/cli](https://github.com/urfave/cli)) just for Go service @@ -121,3 +166,8 @@ Pick 1 then sleep peacefully. No need to say more. Lint or get the f out! + +## Thanks + +- [Uber Go Style Guide](https://github.com/uber-go/guide/blob/master/style.md) +- [Functional options for friendly APIs](https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis)