feat: add experiment go (wip)
parent
00241d974b
commit
971b11a16c
|
@ -0,0 +1,29 @@
|
|||
<!doctype html><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link href="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap" rel=stylesheet><link rel=stylesheet href=styles.css><a href=index>Index</a><h1>Experiment Go</h1><p>There come a time when you need to experiment new things, new style, new approach.<br>So this post serves as it is named.<h1>Design API by trimming down the interface/struct or whatever</h1><p>Instead of:<pre><code class=language-go>type Client interface {
|
||||
GetUser()
|
||||
AddUser()
|
||||
GetAccount()
|
||||
RemoveAccount()
|
||||
}
|
||||
|
||||
// c is Client
|
||||
c.GetUser()
|
||||
c.RemoveAccount()
|
||||
</code></pre><p>Try:<pre><code class=language-go>type Client struct {
|
||||
User ClientUser
|
||||
Account ClientAccount
|
||||
}
|
||||
|
||||
type ClientUser interface {
|
||||
Get()
|
||||
Add()
|
||||
}
|
||||
|
||||
type ClientAccount interface {
|
||||
Get()
|
||||
Remove()
|
||||
}
|
||||
|
||||
// c is Client
|
||||
c.User.Get()
|
||||
c.Account.Remove()
|
||||
</code></pre><p>The difference is <code>c.GetUser()</code> -> <code>c.User.Get()</code>.<p>For example we have client which connect to bank.<br>There are many functions like <code>GetUser</code>, <code>GetTransaction</code>, <code>VerifyAccount</code>, ...<br>So split big client to many children, each child handle single aspect, like user or transaction.<p>My concert is we replace an interface with a struct which contains multiple interfaces aka children.<br>I don't know if this is the right call.<p>This pattern is used by <a href=https://github.com/google/go-github>google/go-github</a>.<h1>Thanks</h1><ul><li><a href=https://blog.gopheracademy.com/advent-2019/api-clients-humans/>API Clients for Humans</a></ul><a href=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>
|
|
@ -1 +1 @@
|
|||
<!doctype html><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link href="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap" rel=stylesheet><link rel=stylesheet href=styles.css><a href=index>Index</a><h1>Index</h1><p>This is where I dump my thoughts.<ul><li><a href=2022-06-08-backup>Backup my way</a><li><a href=2022-06-08-dockerfile-go>Dockerfile for Go</a><li><a href=2022-07-10-bootstrap-go>Bootstrap Go</a><li><a href=2022-07-12-uuid-or-else>UUID or else</a><li><a href=2022-07-19-migrate-to-buf>Migrate to buf</a></ul><a href=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>
|
||||
<!doctype html><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link href="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap" rel=stylesheet><link rel=stylesheet href=styles.css><a href=index>Index</a><h1>Index</h1><p>This is where I dump my thoughts.<ul><li><a href=2022-06-08-backup>Backup my way</a><li><a href=2022-06-08-dockerfile-go>Dockerfile for Go</a><li><a href=2022-07-10-bootstrap-go>Bootstrap Go</a><li><a href=2022-07-12-uuid-or-else>UUID or else</a><li><a href=2022-07-19-migrate-to-buf>Migrate to buf</a><li><a href=2022-07-31-sql>SQL</a><li><a href=2022-07-31-experiment-go>Experiment go</a></ul><a href=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>
|
|
@ -0,0 +1,59 @@
|
|||
# Experiment Go
|
||||
|
||||
There come a time when you need to experiment new things, new style, new approach.
|
||||
So this post serves as it is named.
|
||||
|
||||
# Design API by trimming down the interface/struct or whatever
|
||||
|
||||
Instead of:
|
||||
|
||||
```go
|
||||
type Client interface {
|
||||
GetUser()
|
||||
AddUser()
|
||||
GetAccount()
|
||||
RemoveAccount()
|
||||
}
|
||||
|
||||
// c is Client
|
||||
c.GetUser()
|
||||
c.RemoveAccount()
|
||||
```
|
||||
|
||||
Try:
|
||||
|
||||
```go
|
||||
type Client struct {
|
||||
User ClientUser
|
||||
Account ClientAccount
|
||||
}
|
||||
|
||||
type ClientUser interface {
|
||||
Get()
|
||||
Add()
|
||||
}
|
||||
|
||||
type ClientAccount interface {
|
||||
Get()
|
||||
Remove()
|
||||
}
|
||||
|
||||
// c is Client
|
||||
c.User.Get()
|
||||
c.Account.Remove()
|
||||
```
|
||||
|
||||
The difference is `c.GetUser()` -> `c.User.Get()`.
|
||||
|
||||
For example we have client which connect to bank.
|
||||
There are many functions like `GetUser`, `GetTransaction`, `VerifyAccount`, ...
|
||||
So split big client to many children, each child handle single aspect, like user or transaction.
|
||||
|
||||
My concert is we replace an interface with a struct which contains multiple interfaces aka children.
|
||||
I don't know if this is the right call.
|
||||
|
||||
This pattern is used by [google/go-github](https://github.com/google/go-github).
|
||||
|
||||
# Thanks
|
||||
|
||||
- [API Clients for Humans](https://blog.gopheracademy.com/advent-2019/api-clients-humans/)
|
|
@ -8,3 +8,4 @@ This is where I dump my thoughts.
|
|||
- [UUID or else](2022-07-12-uuid-or-else)
|
||||
- [Migrate to buf](2022-07-19-migrate-to-buf)
|
||||
- [SQL](2022-07-31-sql)
|
||||
- [Experiment go](2022-07-31-experiment-go)
|
||||
|
|
Loading…
Reference in New Issue