go: more generics

main
sudo pacman -Syu 2023-09-24 23:04:22 +07:00
parent 89c8e00244
commit 43c8b7c222
2 changed files with 22 additions and 0 deletions

View File

@ -340,6 +340,13 @@ internal
<pre><span class="pl-c">// Ptr takes in non-pointer and returns a pointer</span>
<span class="pl-k">func</span> <span class="pl-s1">Ptr</span>[<span class="pl-s1">T</span> <span class="pl-en">any</span>](<span class="pl-s1">v</span> <span class="pl-smi">T</span>) <span class="pl-c1">*</span><span class="pl-smi">T</span> {
<span class="pl-k">return</span> <span class="pl-c1">&amp;</span><span class="pl-s1">v</span>
}</pre>
</div>
<p>Return zero value:</p>
<div class="highlight highlight-source-go">
<pre><span class="pl-k">func</span> <span class="pl-en">Zero</span>[<span class="pl-s1">T</span> <span class="pl-s1">any</span>]() <span class="pl-smi">T</span> {
<span class="pl-k">var</span> <span class="pl-s1">zero</span> <span class="pl-smi">T</span>
<span class="pl-k">return</span> <span class="pl-s1">zero</span>
}</pre>
</div>
<h2 id="user-content-external-libs">
@ -883,6 +890,11 @@ fieldalignment -fix ./internal/business/<span class="pl-k">*</span>.go</pre>
>Go generic: non-ptr to ptr</a
>
</li>
<li>
<a href="https://xeiaso.net/blog/gonads-2022-04-24" rel="nofollow"
>Crimes with Go Generics</a
>
</li>
</ul>
<div>

View File

@ -201,6 +201,15 @@ func Ptr[T any](v T) *T {
}
```
Return zero value:
```go
func Zero[T any]() T {
var zero T
return zero
}
```
## External libs
### No need `vendor`
@ -498,3 +507,4 @@ go clean -cache -testcache -modcache -fuzzcache -x
- [Making SQLite faster in Go](https://turriate.com/articles/making-sqlite-faster-in-go)
- [Advanced Go Concurrency](https://encore.dev/blog/advanced-go-concurrency)
- [Go generic: non-ptr to ptr](https://danielms.site/zet/2023/go-generic-non-ptr-to-ptr/)
- [Crimes with Go Generics](https://xeiaso.net/blog/gonads-2022-04-24)