<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daichi Ando on Medium]]></title>
        <description><![CDATA[Stories by Daichi Ando on Medium]]></description>
        <link>https://medium.com/@canigetyourwhatwhat?source=rss-fa3377eb82cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LF6zgqN0VeTex8Lq0fnOhw.jpeg</url>
            <title>Stories by Daichi Ando on Medium</title>
            <link>https://medium.com/@canigetyourwhatwhat?source=rss-fa3377eb82cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 05:22:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@canigetyourwhatwhat/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Are you using Interface correctly?]]></title>
            <link>https://canigetyourwhatwhat.medium.com/are-you-using-interface-correctly-6583d988d2c6?source=rss-fa3377eb82cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/6583d988d2c6</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Daichi Ando]]></dc:creator>
            <pubDate>Tue, 05 Aug 2025 09:24:25 GMT</pubDate>
            <atom:updated>2025-08-05T09:24:25.564Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the most elegant features of Go is its interface system. It allows us to write loosely coupled, testable, and modular applications. But with great power comes great responsibility — and many Go developers, especially those new to the language, fall into the trap of misplacing interfaces.</p><p>In this article, I’ll explain a <strong>common mistake</strong> that rookies make when working with interfaces, using a real-world example from a simple fruit order service. You’ll also learn the <strong>right place</strong> to define interfaces and <strong>why</strong> this design decision matters.</p><h3>The Rookie Mistake</h3><p>Let’s say you’re building a fruit order service with the following layers:</p><ul><li><strong>API Layer</strong>: Handles HTTP requests.</li><li><strong>Database Layer</strong>: Implements logic for storing and retrieving data.</li></ul><p>Here’s how many beginners structure their code:</p><pre>// db/database.go<br><br>type OrderDB interface {<br> SaveOrder(order Order) error<br> GetOrder(id string) (Order, error)<br>}<br><br>type orderDB struct {<br> // Some DB connection, e.g. *sql.DB<br>}<br><br>func (db *orderDB) SaveOrder(order Order) error {<br> panic(&quot;implement me&quot;)<br>}<br><br>func (db *orderDB) GetOrder(id string) (Order, error) {<br> panic(&quot;implement me&quot;)<br>}</pre><pre>// api/handler.go<br><br>type Handler struct {<br>    db db.FruitOrderDB<br>}</pre><p>At first glance, this looks clean and organized. You put the interface next to the implementation. <strong>But here’s the problem</strong>: the interface is defined where it’s implemented, not where it’s consumed.</p><h3>Correct Way: Define Interfaces at the consumer side</h3><p>Go interfaces are <strong>implicitly implemented</strong>. This means you should <strong>define the interface in the consumer package</strong>, not the provider.</p><p>Instead of putting FruitOrderDB in the db package, move it into the api package like so:</p><pre>// db/database.go<br><br>type orderDB struct {<br>    // DB connection<br>}<br><br>func (r *orderDB) SaveOrder(order Order) error {<br>    panic(&quot;implement me&quot;)<br>}<br><br>func (r *orderDB) GetOrder(id string) (Order, error) {<br>    panic(&quot;implement me&quot;)<br>}</pre><pre>// api/server.go<br><br>type DBHandler interface {<br>    SaveOrder(order Order) error<br>    GetOrder(id string) (Order, error)<br>}<br><br>type Handler struct {<br>    db DBHandler<br>}<br><br>repo := &amp;db.FruitOrderRepository{}<br>handler := &amp;Handler{db: repo}</pre><p>api/server.go builds the server, and functions belonging to the Handler struct can use the “DBHandler” interface.</p><h3>What’s the benefit of using the correct interface usage?</h3><h4>Encapsulation</h4><p>When the interface lives in the consumer package, you only expose the methods you actually need, not all the methods in that package.</p><p>You don’t want your API layer to have access to DeleteAll() just because it exists in the DB layer. Instead, <strong>define a narrow interface</strong> with only what the handler uses.</p><h4>Decoupling</h4><p>The API layer becomes decoupled from the actual database logic. This makes it easier to:</p><ul><li>Swap out the DB implementation for a mock (e.g., during testing).</li><li>Use a different DB implementation later (e.g., switch from SQL to NoSQL).</li><li>Avoid circular imports.</li></ul><h4>Testability</h4><p>You can easily use the <a href="https://github.com/vektra/mockery">mockery</a> library to mock functions in unit tests. You want to test a function in api layer, but you use functions from several other packages? Use mockery and boom! Nearly 100% of test coverage is achievable.</p><h3>A Rule of Thumb</h3><blockquote><strong><em>Define interfaces where they are consumed, not where they are implemented.</em></strong></blockquote><p>This practice aligns with Go idioms and helps keep your codebase maintainable and scalable.</p><p>You can even find this philosophy echoed in Go’s own standard library. Take a look at packages likeio, those that define interfaces like io.Reader or io.Writer—even though they don’t implement them. These interfaces are defined for consumption, not production.</p><h3>Final Thoughts</h3><p>Misplacing interfaces is one of those subtle design flaws that may not hurt initially, but will lead to bloated interfaces, tight coupling, and hard-to-test code over time.</p><p>When in doubt, ask yourself:<br> <strong>“Who is using this interface?”</strong><br> That’s where it belongs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6583d988d2c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Authenticate JWT at Middleware with Go + Echo + GraphQL (Login/Sign In)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://canigetyourwhatwhat.medium.com/authenticate-jwt-at-middleware-with-go-echo-graphql-login-sign-in-49828f16e02a?source=rss-fa3377eb82cf------2"><img src="https://cdn-images-1.medium.com/max/2600/1*9cDAfGLg-oDa_nv86foIMg.jpeg" width="7730"></a></p><p class="medium-feed-snippet">Want to reuse user info in ther service layer? Want to implement login feature? Get faimiliar with Echo middleware? This article is for&#x2026;</p><p class="medium-feed-link"><a href="https://canigetyourwhatwhat.medium.com/authenticate-jwt-at-middleware-with-go-echo-graphql-login-sign-in-49828f16e02a?source=rss-fa3377eb82cf------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://canigetyourwhatwhat.medium.com/authenticate-jwt-at-middleware-with-go-echo-graphql-login-sign-in-49828f16e02a?source=rss-fa3377eb82cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/49828f16e02a</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[middleware]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Daichi Ando]]></dc:creator>
            <pubDate>Sat, 09 Mar 2024 10:50:24 GMT</pubDate>
            <atom:updated>2024-03-09T10:50:24.125Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Authenticate JWT at middleware with Go + Echo + GraphQL (Login/Sign In)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://blog.stackademic.com/authenticate-jwt-at-middleware-with-go-echo-graphql-login-sign-in-cef5feac4e05?source=rss-fa3377eb82cf------2"><img src="https://cdn-images-1.medium.com/max/2600/1*EUR9lVOykpkd_Tcr25kJRw.jpeg" width="7730"></a></p><p class="medium-feed-snippet">How to build Login / Sign In functionality in Go</p><p class="medium-feed-link"><a href="https://blog.stackademic.com/authenticate-jwt-at-middleware-with-go-echo-graphql-login-sign-in-cef5feac4e05?source=rss-fa3377eb82cf------2">Continue reading on Stackademic »</a></p></div>]]></description>
            <link>https://blog.stackademic.com/authenticate-jwt-at-middleware-with-go-echo-graphql-login-sign-in-cef5feac4e05?source=rss-fa3377eb82cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/cef5feac4e05</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[middleware]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[echo-framework]]></category>
            <dc:creator><![CDATA[Daichi Ando]]></dc:creator>
            <pubDate>Thu, 15 Feb 2024 07:17:28 GMT</pubDate>
            <atom:updated>2024-02-18T12:37:44.113Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[All you need to know about logging in Go using slog]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://blog.stackademic.com/all-you-need-to-know-about-logging-in-go-using-slog-281ed46f9b49?source=rss-fa3377eb82cf------2"><img src="https://cdn-images-1.medium.com/max/2600/1*LnihSwpiKA6gwjx-bhlCEw.jpeg" width="6000"></a></p><p class="medium-feed-snippet">It is a tutorial/master guide to use slog package. It introduces not just a tutorial but also various use case and tips about logging.</p><p class="medium-feed-link"><a href="https://blog.stackademic.com/all-you-need-to-know-about-logging-in-go-using-slog-281ed46f9b49?source=rss-fa3377eb82cf------2">Continue reading on Stackademic »</a></p></div>]]></description>
            <link>https://blog.stackademic.com/all-you-need-to-know-about-logging-in-go-using-slog-281ed46f9b49?source=rss-fa3377eb82cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/281ed46f9b49</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[slog]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Daichi Ando]]></dc:creator>
            <pubDate>Sun, 28 Jan 2024 14:07:30 GMT</pubDate>
            <atom:updated>2024-02-13T01:45:29.941Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Cron / Job Scheduler Mechanism in Golang/Go]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://blog.stackademic.com/cron-job-scheduler-mechanism-in-golang-go-dfd5fe8ea8ae?source=rss-fa3377eb82cf------2"><img src="https://cdn-images-1.medium.com/max/2600/1*qq6GXc0TsRWXzRCZyB4Daw.jpeg" width="7730"></a></p><p class="medium-feed-snippet">A step-by-step blueprint for constructing a powerful job scheduler in the robust language.</p><p class="medium-feed-link"><a href="https://blog.stackademic.com/cron-job-scheduler-mechanism-in-golang-go-dfd5fe8ea8ae?source=rss-fa3377eb82cf------2">Continue reading on Stackademic »</a></p></div>]]></description>
            <link>https://blog.stackademic.com/cron-job-scheduler-mechanism-in-golang-go-dfd5fe8ea8ae?source=rss-fa3377eb82cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfd5fe8ea8ae</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[job-scheduling]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Daichi Ando]]></dc:creator>
            <pubDate>Sun, 14 Jan 2024 15:03:11 GMT</pubDate>
            <atom:updated>2024-02-13T01:45:14.505Z</atom:updated>
        </item>
    </channel>
</rss>