feat: more go
parent
1d367fef4e
commit
8810ffe62a
|
@ -329,13 +329,13 @@ internal
|
||||||
<h3 class="heading-element">
|
<h3 class="heading-element">
|
||||||
Use
|
Use
|
||||||
<a href="https://pkg.go.dev/sync#Pool" rel="nofollow">sync.Pool</a> when
|
<a href="https://pkg.go.dev/sync#Pool" rel="nofollow">sync.Pool</a> when
|
||||||
need to reuse object, mainly for <code>bytes.Buffer</code>
|
need to re-use object, mainly for <code>bytes.Buffer</code>
|
||||||
</h3>
|
</h3>
|
||||||
<a
|
<a
|
||||||
id="user-content-use-syncpool-when-need-to-reuse-object-mainly-for-bytesbuffer"
|
id="user-content-use-syncpool-when-need-to-re-use-object-mainly-for-bytesbuffer"
|
||||||
class="anchor"
|
class="anchor"
|
||||||
aria-label="Permalink: Use sync.Pool when need to reuse object, mainly for bytes.Buffer"
|
aria-label="Permalink: Use sync.Pool when need to re-use object, mainly for bytes.Buffer"
|
||||||
href="#use-syncpool-when-need-to-reuse-object-mainly-for-bytesbuffer"
|
href="#use-syncpool-when-need-to-re-use-object-mainly-for-bytesbuffer"
|
||||||
><span aria-hidden="true" class="octicon octicon-link"></span
|
><span aria-hidden="true" class="octicon octicon-link"></span
|
||||||
></a>
|
></a>
|
||||||
</div>
|
</div>
|
||||||
|
@ -399,6 +399,34 @@ internal
|
||||||
<span class="pl-k">return</span> <span class="pl-s1">zero</span>
|
<span class="pl-k">return</span> <span class="pl-s1">zero</span>
|
||||||
}</pre>
|
}</pre>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="markdown-heading">
|
||||||
|
<h3 class="heading-element">As go evolve, things should change</h3>
|
||||||
|
<a
|
||||||
|
id="user-content-as-go-evolve-things-should-change"
|
||||||
|
class="anchor"
|
||||||
|
aria-label="Permalink: As go evolve, things should change"
|
||||||
|
href="#as-go-evolve-things-should-change"
|
||||||
|
><span aria-hidden="true" class="octicon octicon-link"></span
|
||||||
|
></a>
|
||||||
|
</div>
|
||||||
|
<p>Since go 1.21:</p>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Use <code>slices.SortFunc</code> instead of <code>sort.Slice</code>.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Use <code>ctx.WithoutCancel</code> to disconnect context from parent.
|
||||||
|
</li>
|
||||||
|
<li>Use <code>clear(m)</code> to clear map entirely.</li>
|
||||||
|
</ul>
|
||||||
|
<p>Since go 1.20:</p>
|
||||||
|
<ul>
|
||||||
|
<li>Use <code>errors.Join</code> for multiple errors.</li>
|
||||||
|
</ul>
|
||||||
|
<p>Since go 1.18:</p>
|
||||||
|
<ul>
|
||||||
|
<li>Use <code>any</code> instead of <code>interface{}</code>.</li>
|
||||||
|
</ul>
|
||||||
<div class="markdown-heading">
|
<div class="markdown-heading">
|
||||||
<h2 class="heading-element">External libs</h2>
|
<h2 class="heading-element">External libs</h2>
|
||||||
<a
|
<a
|
||||||
|
@ -653,13 +681,14 @@ internal
|
||||||
<div class="markdown-heading">
|
<div class="markdown-heading">
|
||||||
<h3 class="heading-element">
|
<h3 class="heading-element">
|
||||||
Connect Redis with
|
Connect Redis with
|
||||||
<a href="https://github.com/redis/go-redis">redis/go-redis</a>
|
<a href="https://github.com/redis/go-redis">redis/go-redis</a> or
|
||||||
|
<a href="https://github.com/redis/rueidis">redis/rueidis</a>
|
||||||
</h3>
|
</h3>
|
||||||
<a
|
<a
|
||||||
id="user-content-connect-redis-with-redisgo-redis"
|
id="user-content-connect-redis-with-redisgo-redis-or-redisrueidis"
|
||||||
class="anchor"
|
class="anchor"
|
||||||
aria-label="Permalink: Connect Redis with redis/go-redis"
|
aria-label="Permalink: Connect Redis with redis/go-redis or redis/rueidis"
|
||||||
href="#connect-redis-with-redisgo-redis"
|
href="#connect-redis-with-redisgo-redis-or-redisrueidis"
|
||||||
><span aria-hidden="true" class="octicon octicon-link"></span
|
><span aria-hidden="true" class="octicon octicon-link"></span
|
||||||
></a>
|
></a>
|
||||||
</div>
|
</div>
|
||||||
|
@ -952,6 +981,10 @@ stringer -type=Drink</pre>
|
||||||
>
|
>
|
||||||
to fix them.
|
to fix them.
|
||||||
</p>
|
</p>
|
||||||
|
<p>
|
||||||
|
My heuristic for fieldalignment (not work all the time): pointer ->
|
||||||
|
string -> []byte -> int64 -> int32.
|
||||||
|
</p>
|
||||||
<div class="highlight highlight-source-shell">
|
<div class="highlight highlight-source-shell">
|
||||||
<pre><span class="pl-c"><span class="pl-c">#</span> Install</span>
|
<pre><span class="pl-c"><span class="pl-c">#</span> Install</span>
|
||||||
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
|
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
|
||||||
|
|
|
@ -158,7 +158,7 @@ if err := eg.Wait(); err != nil {
|
||||||
Please don't use external libs for WorkerPool, I don't want to deal with
|
Please don't use external libs for WorkerPool, I don't want to deal with
|
||||||
dependency hell.
|
dependency hell.
|
||||||
|
|
||||||
### Use [sync.Pool](https://pkg.go.dev/sync#Pool) when need to reuse object, mainly for `bytes.Buffer`
|
### Use [sync.Pool](https://pkg.go.dev/sync#Pool) when need to re-use object, mainly for `bytes.Buffer`
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
@ -210,6 +210,22 @@ func Zero[T any]() T {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### As go evolve, things should change
|
||||||
|
|
||||||
|
Since go 1.21:
|
||||||
|
|
||||||
|
- Use `slices.SortFunc` instead of `sort.Slice`.
|
||||||
|
- Use `ctx.WithoutCancel` to disconnect context from parent.
|
||||||
|
- Use `clear(m)` to clear map entirely.
|
||||||
|
|
||||||
|
Since go 1.20:
|
||||||
|
|
||||||
|
- Use `errors.Join` for multiple errors.
|
||||||
|
|
||||||
|
Since go 1.18:
|
||||||
|
|
||||||
|
- Use `any` instead of `interface{}`.
|
||||||
|
|
||||||
## External libs
|
## External libs
|
||||||
|
|
||||||
### No need `vendor`
|
### No need `vendor`
|
||||||
|
@ -325,7 +341,7 @@ 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
|
after insert/update. So may be you want to use ORM for those cases. I hear that
|
||||||
[go-gorm/gorm](https://github.com/go-gorm/gorm) is good.
|
[go-gorm/gorm](https://github.com/go-gorm/gorm) is good.
|
||||||
|
|
||||||
### Connect Redis with [redis/go-redis](https://github.com/redis/go-redis)
|
### Connect Redis with [redis/go-redis](https://github.com/redis/go-redis) or [redis/rueidis](https://github.com/redis/rueidis)
|
||||||
|
|
||||||
Be careful when use [HGETALL](https://redis.io/commands/hgetall/). If key not
|
Be careful when use [HGETALL](https://redis.io/commands/hgetall/). If key not
|
||||||
found, empty data will be returned not nil error. See
|
found, empty data will be returned not nil error. See
|
||||||
|
@ -472,6 +488,9 @@ If you get `fieldalignment` error, use
|
||||||
[fieldalignment](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment)
|
[fieldalignment](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment)
|
||||||
to fix them.
|
to fix them.
|
||||||
|
|
||||||
|
My heuristic for fieldalignment (not work all the time): pointer -> string ->
|
||||||
|
[]byte -> int64 -> int32.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Install
|
# Install
|
||||||
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
|
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
|
||||||
|
|
Loading…
Reference in New Issue