diff --git a/docs/2022-07-10-bootstrap-go.html b/docs/2022-07-10-bootstrap-go.html index f766695..7c61f4b 100644 --- a/docs/2022-07-10-bootstrap-go.html +++ b/docs/2022-07-10-bootstrap-go.html @@ -74,10 +74,10 @@ import (
We always get the version of build tools in go.mod
each time we install it.
Future contributors will not cry anymore.
What is the point to pass many params (do-it
, --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.
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.
prototool is deprecated, and buf can generate, lint, format as good as prototool.
Don't use gin.Context
when pass context from handler layer to service layer, use gin.Context.Request.Context()
instead.
It is fast!
Don't overuse func (*Logger) With
. Because if log line is too long, there is a possibility that we can lost it.
Use MarshalLogObject
when we need to hide some field of object when log (field is long or has sensitive value)
Don't use Panic
. Use Fatal
for errors when start service to check dependencies. If you really need panic level, use DPanic
.
If doubt, use zap.Any
.
Use contextID
or traceID
in every log lines for easily debug.
Only init config in main or cmd layer.
Do not use viper.Get...
in business layer or inside business layer.
Why?
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.
I hear that go-gorm/gorm, ent/ent is good.
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.
The first is easy to use but not powerful as the later.
If you want to make sure mock func is called with correct times, use the later.
Example with matryer/moq
:
// Only gen mock if source code file is newer than mock file
// https://jonwillia.ms/2019/12/22/conditional-gomock-mockgen
//go:generate sh -c "test service_mock_generated.go -nt $GOFILE && exit 0; moq -rm -out service_mock_generated.go . Service"
-
Don't cast proto enum:
// Don't
+
Don't cast proto enum:
// Bad
a := cast.ToInt32(servicev1.ReasonCode_ABC)
-// Do
+// Good
a := int32(servicev1.ReasonCode_ABC)
type Drink int
diff --git a/docs/2022-07-31-sql.html b/docs/2022-07-31-sql.html
index a0841c2..8992829 100644
--- a/docs/2022-07-31-sql.html
+++ b/docs/2022-07-31-sql.html
@@ -1,14 +1,15 @@
-IndexSQL
Previously in my fresher time, I rarely write SQL, I always use ORM to wrap SQL.
But time past and too much abstraction bites me.
So I decide to only write SQL from now, no more ORM for me.
But if there is any cool ORM for Go, I guess I try.
This guide is not kind of guide which cover all cases.
Just my little tricks when I work with SQL.
Stay away from database unique id
Use UUID instead.
Stay away from database timestamp
Stay away from all kind of timestamp, timestamp of MySQL or any database.
Just use int64 then pass the timestamp in service layer not database layer.
Why? Because time and date and location are too much complex to handle.
In my business, I use timestamp in milliseconds.
Then I save timestamp as int64 value to database.
Each time I get timestamp from database, I parse to time struct in Go with location or format I want.
No more hassle!
Use index
You should use index for faster query, but not too much.
Don't create index for every fields in table.
Choose wisely!
For example, create index in MySQL:
CREATE INDEX `idx_timestamp`
+IndexSQL
Previously in my fresher software developer time, I rarely write SQL, I always use ORM to wrap SQL.
But time past and too much abstraction bites me.
So I decide to only write SQL from now as much as possible, no more ORM for me.
But if there is any cool ORM for Go, I guess I try.
This guide is not kind of guide which cover all cases.
Just my little tricks when I work with SQL.
Stay away from database unique id
Use UUID instead.
If you can, and you should, choose UUID type which can be sortable.
Stay away from database timestamp
Stay away from all kind of database timestamp (MySQL timestmap, SQLite timestamp, ...)
Just use int64 then pass the timestamp in service layer not database layer.
Why? Because time and date and location are too much complex to handle.
In my business, I use timestamp in milliseconds.
Then I save timestamp as int64 value to database.
Each time I get timestamp from database, I parse to time struct in Go with location or format I want.
No more hassle!
It looks like this:
[Business] time, data -> convert to unix timestamp milliseconds -> [Database] int64
+
Use index!!!
You should use index for faster query, but not too much.
Don't create index for every fields in table.
Choose wisely!
For example, create index in MySQL:
CREATE INDEX `idx_timestamp`
ON `user_upload` (`timestamp`);
-
Be careful with NULL
If compare with field which can be NULL, remember to check NULL for safety.
-- field_something can be NULL
+
Be careful with NULL
If compare with field which can be NULL, remember to check NULL for safety.
-- field_something can be NULL
--- Don't
+-- Bad
SELECT *
FROM table
WHERE field_something != 1
--- Do
+-- Good
SELECT *
FROM table
WHERE (field_something IS NULL OR field_something != 1)
-
Thanks
Feel free to ask me via email
\ No newline at end of file
+
Need clarify why this happpen? Idk :(
VARCHAR
or TEXT
Prefer VARCHAR
if you need to query and of course use index, and make sure size of value will never hit the limit.
Prefer TEXT
if you don't care, just want to store something.
Be super careful when migrate, update database on production and online!!!
Plase read docs about online ddl operations before do anything online (keep database running the same time update it, for example create index, ...)
Tools
- Use sqlfluff/sqlfluff to check your SQL.
- Use k1LoW/tbls to grasp your database reality :)
Thanks
Feel free to ask me via email
\ 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 f7efcfb..b7fff5d 100644
--- a/posts/2022-07-10-bootstrap-go.md
+++ b/posts/2022-07-10-bootstrap-go.md
@@ -261,10 +261,10 @@ Example with `matryer/moq`:
Don't cast proto enum:
```go
-// Don't
+// Bad
a := cast.ToInt32(servicev1.ReasonCode_ABC)
-// Do
+// Good
a := int32(servicev1.ReasonCode_ABC)
```
diff --git a/posts/2022-07-31-sql.md b/posts/2022-07-31-sql.md
index 641a2af..2f271f2 100644
--- a/posts/2022-07-31-sql.md
+++ b/posts/2022-07-31-sql.md
@@ -1,20 +1,21 @@
# SQL
-Previously in my fresher time, I rarely write SQL, I always use ORM to wrap SQL.
+Previously in my fresher software developer time, I rarely write SQL, I always use ORM to wrap SQL.
But time past and too much abstraction bites me.
-So I decide to only write SQL from now, no more ORM for me.
+So I decide to only write SQL from now as much as possible, no more ORM for me.
But if there is any cool ORM for Go, I guess I try.
This guide is not kind of guide which cover all cases.
Just my little tricks when I work with SQL.
-# Stay away from database unique id
+## Stay away from database unique id
Use UUID instead.
+If you can, and you should, choose UUID type which can be sortable.
-# Stay away from database timestamp
+## Stay away from database timestamp
-Stay away from all kind of timestamp, timestamp of MySQL or any database.
+Stay away from all kind of database timestamp (MySQL timestmap, SQLite timestamp, ...)
Just use int64 then pass the timestamp in service layer not database layer.
Why? Because time and date and location are too much complex to handle.
@@ -23,7 +24,13 @@ Then I save timestamp as int64 value to database.
Each time I get timestamp from database, I parse to time struct in Go with location or format I want.
No more hassle!
-# Use index
+It looks like this:
+
+```txt
+[Business] time, data -> convert to unix timestamp milliseconds -> [Database] int64
+```
+
+## Use index!!!
You should use index for faster query, but not too much.
Don't create index for every fields in table.
@@ -36,25 +43,44 @@ CREATE INDEX `idx_timestamp`
ON `user_upload` (`timestamp`);
```
-# Be careful with NULL
+## Be careful with NULL
If compare with field which can be NULL, remember to check NULL for safety.
```sql
-- field_something can be NULL
--- Don't
+-- Bad
SELECT *
FROM table
WHERE field_something != 1
--- Do
+-- Good
SELECT *
FROM table
WHERE (field_something IS NULL OR field_something != 1)
```
-# Thanks
+Need clarify why this happpen? Idk :(
+
+## `VARCHAR` or `TEXT`
+
+Prefer `VARCHAR` if you need to query and of course use index, and make sure size of value will never hit the limit.
+Prefer `TEXT` if you don't care, just want to store something.
+
+## Be super careful when migrate, update database on production and online!!!
+
+Plase read docs about online ddl operations before do anything online (keep database running the same time update it, for example create index, ...)
+
+- [For MySQL 5.7](https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl-operations.html), [Limitations](https://dev.mysql.com/doc/refman/5.7/en/innodb-online-ddl-limitations.html)
+- [For MySQL 8.0](https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html), [Limitations](https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-limitations.html)
+
+## Tools
+
+- Use [sqlfluff/sqlfluff](https://github.com/sqlfluff/sqlfluff) to check your SQL.
+- Use [k1LoW/tbls](https://github.com/k1LoW/tbls) to grasp your database reality :)
+
+## Thanks
- [Use The Index, Luke](https://use-the-index-luke.com/)
- [Essential elements of high performance applications: SQL indexes](https://www.foxhound.systems/blog/essential-elements-of-high-performance-sql-indexes/)