2023-07-18 18:38:30 +00:00
|
|
|
<!doctype html>
|
2022-12-25 19:13:58 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
|
|
<link
|
|
|
|
rel="stylesheet"
|
2023-10-11 06:57:20 +00:00
|
|
|
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.3.0/github-markdown.min.css"
|
2022-12-25 19:13:58 +00:00
|
|
|
/>
|
2023-04-01 03:37:48 +00:00
|
|
|
<title>haunt98 posts</title>
|
2022-12-25 19:13:58 +00:00
|
|
|
</head>
|
|
|
|
<style>
|
|
|
|
.markdown-body {
|
|
|
|
box-sizing: border-box;
|
|
|
|
min-width: 200px;
|
|
|
|
max-width: 980px;
|
|
|
|
margin: 0 auto;
|
|
|
|
padding: 45px;
|
|
|
|
}
|
|
|
|
|
|
|
|
@media (max-width: 767px) {
|
|
|
|
.markdown-body {
|
|
|
|
padding: 15px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<body class="markdown-body">
|
2023-08-09 07:22:58 +00:00
|
|
|
<h2>
|
|
|
|
<a href="index.html"><code>~</code></a>
|
|
|
|
</h2>
|
2023-09-22 16:40:26 +00:00
|
|
|
<h1 id="user-content-experiment-go">
|
|
|
|
<a class="heading-link" href="#experiment-go"
|
|
|
|
>Experiment Go<span
|
|
|
|
aria-hidden="true"
|
|
|
|
class="octicon octicon-link"
|
|
|
|
></span
|
|
|
|
></a>
|
2022-12-30 09:26:06 +00:00
|
|
|
</h1>
|
|
|
|
<p>
|
|
|
|
There come a time when you need to experiment new things, new style, new
|
|
|
|
approach. So this post serves as it is named.
|
|
|
|
</p>
|
2023-09-24 12:17:11 +00:00
|
|
|
<h2
|
2023-09-22 16:40:26 +00:00
|
|
|
id="user-content-design-api-by-trimming-down-the-interfacestruct-or-whatever"
|
|
|
|
>
|
2022-12-30 09:26:06 +00:00
|
|
|
<a
|
2023-09-22 16:40:26 +00:00
|
|
|
class="heading-link"
|
2022-12-30 09:26:06 +00:00
|
|
|
href="#design-api-by-trimming-down-the-interfacestruct-or-whatever"
|
2023-09-22 16:40:26 +00:00
|
|
|
>Design API by trimming down the interface/struct or whatever<span
|
|
|
|
aria-hidden="true"
|
|
|
|
class="octicon octicon-link"
|
|
|
|
></span
|
|
|
|
></a>
|
2023-09-24 12:17:11 +00:00
|
|
|
</h2>
|
2022-12-30 09:26:06 +00:00
|
|
|
<p>Instead of:</p>
|
|
|
|
<div class="highlight highlight-source-go">
|
|
|
|
<pre><span class="pl-k">type</span> <span class="pl-smi">Client</span> <span class="pl-k">interface</span> {
|
2022-12-25 19:13:58 +00:00
|
|
|
<span class="pl-c1">GetUser</span>()
|
|
|
|
<span class="pl-c1">AddUser</span>()
|
|
|
|
<span class="pl-c1">GetAccount</span>()
|
|
|
|
<span class="pl-c1">RemoveAccount</span>()
|
2022-07-31 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-25 19:13:58 +00:00
|
|
|
<span class="pl-c">// c is Client</span>
|
|
|
|
<span class="pl-s1">c</span>.<span class="pl-en">GetUser</span>()
|
2022-12-30 09:26:06 +00:00
|
|
|
<span class="pl-s1">c</span>.<span class="pl-en">RemoveAccount</span>()</pre>
|
|
|
|
</div>
|
|
|
|
<p>Try:</p>
|
|
|
|
<div class="highlight highlight-source-go">
|
|
|
|
<pre><span class="pl-k">type</span> <span class="pl-smi">Client</span> <span class="pl-k">struct</span> {
|
2022-12-25 19:13:58 +00:00
|
|
|
<span class="pl-c1">User</span> <span class="pl-smi">ClientUser</span>
|
|
|
|
<span class="pl-c1">Account</span> <span class="pl-smi">ClientAccount</span>
|
2022-07-31 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-25 19:13:58 +00:00
|
|
|
<span class="pl-k">type</span> <span class="pl-smi">ClientUser</span> <span class="pl-k">interface</span> {
|
|
|
|
<span class="pl-c1">Get</span>()
|
|
|
|
<span class="pl-c1">Add</span>()
|
2022-07-31 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-25 19:13:58 +00:00
|
|
|
<span class="pl-k">type</span> <span class="pl-smi">ClientAccount</span> <span class="pl-k">interface</span> {
|
|
|
|
<span class="pl-c1">Get</span>()
|
|
|
|
<span class="pl-c1">Remove</span>()
|
2022-07-31 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-25 19:13:58 +00:00
|
|
|
<span class="pl-c">// c is Client</span>
|
|
|
|
<span class="pl-s1">c</span>.<span class="pl-c1">User</span>.<span class="pl-en">Get</span>()
|
2022-12-30 09:26:06 +00:00
|
|
|
<span class="pl-s1">c</span>.<span class="pl-c1">Account</span>.<span class="pl-en">Remove</span>()</pre>
|
|
|
|
</div>
|
|
|
|
<p>
|
|
|
|
The difference is <code>c.GetUser()</code> ->
|
|
|
|
<code>c.User.Get()</code>.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
For example we have client which connect to bank. There are many functions
|
|
|
|
like <code>GetUser</code>, <code>GetTransaction</code>,
|
|
|
|
<code>VerifyAccount</code>, ... So split big client to many children, each
|
|
|
|
child handle single aspect, like user or transaction.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
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.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
This pattern is used by
|
|
|
|
<a href="https://github.com/google/go-github">google/go-github</a>.
|
|
|
|
</p>
|
2023-09-22 16:40:26 +00:00
|
|
|
<h2 id="user-content-find-alternative-to-grpcgrpc-go">
|
|
|
|
<a class="heading-link" href="#find-alternative-to-grpcgrpc-go"
|
|
|
|
>Find alternative to </a
|
|
|
|
><a href="https://github.com/grpc/grpc-go">grpc/grpc-go</a>
|
|
|
|
<span aria-hidden="true" class="octicon octicon-link"></span>
|
2022-12-30 09:26:06 +00:00
|
|
|
</h2>
|
|
|
|
<p>
|
|
|
|
Why?
|
|
|
|
<a
|
|
|
|
href="https://github.com/grpc/grpc-go/issues?q=is%3Aissue+compatibility+is%3Aclosed"
|
|
|
|
>See for yourself</a
|
2023-02-08 08:24:01 +00:00
|
|
|
>.
|
2022-12-30 09:26:06 +00:00
|
|
|
</p>
|
2023-02-08 08:24:01 +00:00
|
|
|
<p>Also read:</p>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a href="https://go.dev/blog/protobuf-apiv2" rel="nofollow"
|
|
|
|
>A new Go API for Protocol Buffers</a
|
|
|
|
>
|
|
|
|
to know why <code>v1.20.0</code> is <code>v2</code>.
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="https://jbrandhorst.com/post/plugin-versioning/" rel="nofollow"
|
|
|
|
>Go Protobuf Plugin Versioning</a
|
|
|
|
>.
|
|
|
|
</li>
|
|
|
|
</ul>
|
2022-12-30 09:26:06 +00:00
|
|
|
<p>Currently there are some:</p>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a href="https://github.com/bufbuild/connect-go">bufbuild/connect-go</a
|
|
|
|
>. Comming from buf, trust worthy but need time to make it match feature
|
|
|
|
parity with grpc-go.
|
|
|
|
</li>
|
|
|
|
<li><a href="https://github.com/twitchtv/twirp">twitchtv/twirp</a></li>
|
|
|
|
<li><a href="https://github.com/storj/drpc">storj/drpc</a></li>
|
|
|
|
</ul>
|
2023-09-22 16:40:26 +00:00
|
|
|
<h1 id="user-content-thanks">
|
|
|
|
<a class="heading-link" href="#thanks"
|
|
|
|
>Thanks<span aria-hidden="true" class="octicon octicon-link"></span
|
|
|
|
></a>
|
2022-12-30 09:26:06 +00:00
|
|
|
</h1>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a
|
|
|
|
href="https://blog.gopheracademy.com/advent-2019/api-clients-humans/"
|
|
|
|
rel="nofollow"
|
|
|
|
>API Clients for Humans</a
|
|
|
|
>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2022-12-25 19:13:58 +00:00
|
|
|
|
2022-12-25 19:22:39 +00:00
|
|
|
<div>
|
|
|
|
Feel free to ask me via
|
|
|
|
<a href="mailto:hauvipapro+posts@gmail.com">email</a> or
|
2023-08-20 17:29:13 +00:00
|
|
|
<a rel="me" href="https://hachyderm.io/@haunguyen">Mastodon</a>.
|
|
|
|
<br />Source code is available on
|
2022-12-30 09:55:38 +00:00
|
|
|
<a href="https://github.com/haunt98/posts-go">GitHub</a>
|
2023-08-20 17:29:13 +00:00
|
|
|
<a href="https://codeberg.org/yoshie/posts-go">Codeberg</a>
|
|
|
|
<a href="https://git.sr.ht/~youngyoshie/posts-go">sourcehut</a>
|
|
|
|
<a href="https://gitea.treehouse.systems/yoshie/posts-go">Treehouse</a>
|
|
|
|
<a href="https://gitlab.com/youngyoshie/posts-go">GitLab</a>
|
2022-12-25 19:22:39 +00:00
|
|
|
</div>
|
2022-12-25 19:13:58 +00:00
|
|
|
</body>
|
|
|
|
</html>
|