187 lines
6.0 KiB
HTML
187 lines
6.0 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.2.0/github-markdown-dark.min.css"
|
|
/>
|
|
<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=Inter&family=JetBrains+Mono&display=swap"
|
|
rel="stylesheet"
|
|
/>
|
|
<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;
|
|
}
|
|
}
|
|
|
|
.markdown-body {
|
|
font-family: "Inter", sans-serif;
|
|
}
|
|
|
|
.markdown-body code,
|
|
.markdown-body pre {
|
|
font-family: "JetBrains Mono", monospace;
|
|
}
|
|
</style>
|
|
<body class="markdown-body">
|
|
<div><a href="index.html">Index</a></div>
|
|
<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>()
|
|
}
|
|
|
|
<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> ->
|
|
<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
|
|
>.
|
|
</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>
|
|
<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>
|
|
|
|
<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>. Source
|
|
code is available on
|
|
<a href="https://github.com/haunt98/posts-go">GitHub</a>
|
|
</div>
|
|
</body>
|
|
</html>
|