go: generics tricks
parent
04fcedc7db
commit
cb533946f4
|
@ -320,6 +320,26 @@ internal
|
||||||
<span class="pl-s1">result</span> <span class="pl-c1">:=</span> <span class="pl-en">make</span>([]<span class="pl-smi">byte</span>, <span class="pl-s1">b</span>.<span class="pl-en">Len</span>())
|
<span class="pl-s1">result</span> <span class="pl-c1">:=</span> <span class="pl-en">make</span>([]<span class="pl-smi">byte</span>, <span class="pl-s1">b</span>.<span class="pl-en">Len</span>())
|
||||||
<span class="pl-en">copy</span>(<span class="pl-s1">result</span>, <span class="pl-s1">b</span>.<span class="pl-en">Bytes</span>())
|
<span class="pl-en">copy</span>(<span class="pl-s1">result</span>, <span class="pl-s1">b</span>.<span class="pl-en">Bytes</span>())
|
||||||
<span class="pl-k">return</span> <span class="pl-s1">result</span>, <span class="pl-c1">nil</span>
|
<span class="pl-k">return</span> <span class="pl-s1">result</span>, <span class="pl-c1">nil</span>
|
||||||
|
}</pre>
|
||||||
|
</div>
|
||||||
|
<h3 id="user-content-generics-with-some-tricks">
|
||||||
|
<a class="heading-link" href="#generics-with-some-tricks"> </a
|
||||||
|
><a href="https://go.dev/doc/tutorial/generics" rel="nofollow"
|
||||||
|
>Generics</a
|
||||||
|
>
|
||||||
|
with some tricks<span
|
||||||
|
aria-hidden="true"
|
||||||
|
class="octicon octicon-link"
|
||||||
|
></span>
|
||||||
|
</h3>
|
||||||
|
<p>
|
||||||
|
Take value then return pointer, useful with database struct full of
|
||||||
|
pointers:
|
||||||
|
</p>
|
||||||
|
<div class="highlight highlight-source-go">
|
||||||
|
<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">&</span><span class="pl-s1">v</span>
|
||||||
}</pre>
|
}</pre>
|
||||||
</div>
|
</div>
|
||||||
<h2 id="user-content-external-libs">
|
<h2 id="user-content-external-libs">
|
||||||
|
@ -856,6 +876,13 @@ fieldalignment -fix ./internal/business/<span class="pl-k">*</span>.go</pre>
|
||||||
>Advanced Go Concurrency</a
|
>Advanced Go Concurrency</a
|
||||||
>
|
>
|
||||||
</li>
|
</li>
|
||||||
|
<li>
|
||||||
|
<a
|
||||||
|
href="https://danielms.site/zet/2023/go-generic-non-ptr-to-ptr/"
|
||||||
|
rel="nofollow"
|
||||||
|
>Go generic: non-ptr to ptr</a
|
||||||
|
>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
|
|
|
@ -190,6 +190,17 @@ func MarshalWithoutEscapeHTML(v any) ([]byte, error) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### [Generics](https://go.dev/doc/tutorial/generics) with some tricks
|
||||||
|
|
||||||
|
Take value then return pointer, useful with database struct full of pointers:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Ptr takes in non-pointer and returns a pointer
|
||||||
|
func Ptr[T any](v T) *T {
|
||||||
|
return &v
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## External libs
|
## External libs
|
||||||
|
|
||||||
### No need `vendor`
|
### No need `vendor`
|
||||||
|
@ -486,3 +497,4 @@ go clean -cache -testcache -modcache -fuzzcache -x
|
||||||
- [Speed Up GoMock with Conditional Generation](https://jonwillia.ms/2019/12/22/conditional-gomock-mockgen)
|
- [Speed Up GoMock with Conditional Generation](https://jonwillia.ms/2019/12/22/conditional-gomock-mockgen)
|
||||||
- [Making SQLite faster in Go](https://turriate.com/articles/making-sqlite-faster-in-go)
|
- [Making SQLite faster in Go](https://turriate.com/articles/making-sqlite-faster-in-go)
|
||||||
- [Advanced Go Concurrency](https://encore.dev/blog/advanced-go-concurrency)
|
- [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/)
|
||||||
|
|
Loading…
Reference in New Issue