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

174 lines
5.6 KiB
HTML
Raw Normal View History

2023-07-18 18:38:30 +00:00
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
2023-08-06 07:41:38 +00:00
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/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;
}
@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>
2022-12-30 09:26:06 +00:00
<h1>
<a
id="user-content-experiment-go"
class="anchor"
aria-hidden="true"
href="#experiment-go"
><span aria-hidden="true" class="octicon octicon-link"></span></a
>Experiment Go
</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>
<h1>
<a
id="user-content-design-api-by-trimming-down-the-interfacestruct-or-whatever"
class="anchor"
aria-hidden="true"
href="#design-api-by-trimming-down-the-interfacestruct-or-whatever"
><span aria-hidden="true" class="octicon octicon-link"></span></a
>Design API by trimming down the interface/struct or whatever
</h1>
<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>()
2022-07-31 17:10:50 +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> {
<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
}
<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
}
<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
}
<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> -&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>
<h2>
<a
id="user-content-find-alternative-to-grpcgrpc-go"
class="anchor"
aria-hidden="true"
href="#find-alternative-to-grpcgrpc-go"
><span aria-hidden="true" class="octicon octicon-link"></span></a
>Find alternative to
<a href="https://github.com/grpc/grpc-go">grpc/grpc-go</a>
</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>
<h1>
<a
id="user-content-thanks"
class="anchor"
aria-hidden="true"
href="#thanks"
><span aria-hidden="true" class="octicon octicon-link"></span></a
>Thanks
</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:22:39 +00:00
<div>
Feel free to ask me via
<a href="mailto:hauvipapro+posts@gmail.com">email</a> or
2022-12-30 09:55:38 +00:00
<a rel="me" href="https://hachyderm.io/@haunguyen">Mastodon</a>. Source
code is available on
<a href="https://github.com/haunt98/posts-go">GitHub</a>
2022-12-25 19:22:39 +00:00
</div>
</body>
</html>