posts-go/docs/2022-07-31-experiment-go.html

202 lines
6.6 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.5.1/github-markdown.min.css"
/>
<title>haunt98 posts</title>
</head>
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
font-family:
Shantell Sans Normal,
Rec Mono Casual,
SF Pro,
Inter,
sans-serif;
font-weight: 500;
}
.markdown-body pre {
font-family:
Berkeley Mono,
IBM Plex Mono,
SF Mono,
Jetbrains Mono,
monospace;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
<body class="markdown-body">
<h2>
<a href="index.html"><code>~</code></a>
</h2>
<div class="markdown-heading">
<h1 class="heading-element">Experiment Go</h1>
<a
id="user-content-experiment-go"
class="anchor"
aria-label="Permalink: Experiment Go"
href="#experiment-go"
><span aria-hidden="true" class="octicon octicon-link"></span
></a>
</div>
<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>
<div class="markdown-heading">
<h2 class="heading-element">
Design API by trimming down the interface/struct or whatever
</h2>
<a
id="user-content-design-api-by-trimming-down-the-interfacestruct-or-whatever"
class="anchor"
aria-label="Permalink: Design API by trimming down the interface/struct or whatever"
href="#design-api-by-trimming-down-the-interfacestruct-or-whatever"
><span aria-hidden="true" class="octicon octicon-link"></span
></a>
</div>
<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> {
<span class="pl-c1">GetUser</span>()
<span class="pl-c1">AddUser</span>()
<span class="pl-c1">GetAccount</span>()
<span class="pl-c1">RemoveAccount</span>()
}
<span class="pl-c">// c is Client</span>
<span class="pl-s1">c</span>.<span class="pl-en">GetUser</span>()
<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> {
<span class="pl-c1">User</span> <span class="pl-smi">ClientUser</span>
<span class="pl-c1">Account</span> <span class="pl-smi">ClientAccount</span>
}
<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>()
}
<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>()
}
<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>()
<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> -&gt;
<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>
<div class="markdown-heading">
<h2 class="heading-element">
Find alternative to
<a href="https://github.com/grpc/grpc-go">grpc/grpc-go</a>
</h2>
<a
id="user-content-find-alternative-to-grpcgrpc-go"
class="anchor"
aria-label="Permalink: Find alternative to grpc/grpc-go"
href="#find-alternative-to-grpcgrpc-go"
><span aria-hidden="true" class="octicon octicon-link"></span
></a>
</div>
<p>
Why?
<a
href="https://github.com/grpc/grpc-go/issues?q=is%3Aissue+compatibility+is%3Aclosed"
>See for yourself</a
>.
</p>
<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>
<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>
<div class="markdown-heading">
<h1 class="heading-element">Thanks</h1>
<a
id="user-content-thanks"
class="anchor"
aria-label="Permalink: Thanks"
href="#thanks"
><span aria-hidden="true" class="octicon octicon-link"></span
></a>
</div>
<ul>
<li>
<a
href="https://blog.gopheracademy.com/advent-2019/api-clients-humans/"
rel="nofollow"
>API Clients for Humans</a
>
</li>
</ul>
<div>
Feel free to ask me via
<a href="mailto:hauvipapro+posts@gmail.com">email</a> or
<a rel="me" href="https://hachyderm.io/@haunguyen">Mastodon</a>.
<br />Source code is available on
<a href="https://github.com/haunt98/posts-go">GitHub</a>
<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>
</div>
</body>
</html>