<?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 Ahmet ILDIRIM on Medium]]></title>
        <description><![CDATA[Stories by Ahmet ILDIRIM on Medium]]></description>
        <link>https://medium.com/@ahmetildirim?source=rss-fb032ad9ed87------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*niQveK7N8ZuCfdmSO999DQ.jpeg</url>
            <title>Stories by Ahmet ILDIRIM on Medium</title>
            <link>https://medium.com/@ahmetildirim?source=rss-fb032ad9ed87------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 01:48:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmetildirim/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[Idiomatic Clean Architecture with Golang]]></title>
            <link>https://medium.com/@ahmetildirim/idiomatic-clean-architecture-with-golang-691e0baca7f4?source=rss-fb032ad9ed87------2</link>
            <guid isPermaLink="false">https://medium.com/p/691e0baca7f4</guid>
            <dc:creator><![CDATA[Ahmet ILDIRIM]]></dc:creator>
            <pubDate>Fri, 20 Oct 2023 20:41:54 GMT</pubDate>
            <atom:updated>2023-12-18T06:20:03.562Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever read a blog post about an architecture, gotten excited, and attempted to implement it, only to find it too abstract or disconnected from real-world scenarios? The problems showcased are often oversimplified, leaving readers without a comprehensive understanding.</p><p>My aim with this post is to present a more holistic view of the implementation of the architecture suitable for both straightforward and complex projects. There are numerous number of clean architecture examples out there. But Golang is not Java, so we need to adapt the idioms of the language.</p><p>TLWR: here is the <a href="https://github.com/ahmetildirim/morgan"><strong><em>repo</em></strong></a></p><h3>Objectives</h3><p>Before delving into the design, it’s essential to set the architectural objectives:</p><ul><li>Scalability in terms of codebase and the number of teams collaborating.</li><li>Not a one-size-fits-all solution.</li></ul><h3>Case Study: A Social Media App, Morgan</h3><p>To make things tangible, we’ll use a rudimentary social media application, similar to ex-Twitter (pun intended).</p><ul><li>User registration and login.</li><li>Posting messages.</li><li>Following other users.</li><li>Liking and commenting on posts.</li><li>Viewing a feed of posts from followed users.</li></ul><p>The focus will be on the API’s architecture. For clarity, some components like logging, configuration management, and integration tests are omitted.</p><h3>Fundamental Reasoning Behind the Package Structure</h3><p>If you are already familiar with traditional clean architecture concepts, you would expect to see packages for layers; <strong>service</strong>, <strong>domain,</strong> and <strong>repository</strong>. But we are not going to do that here. Creating packages for layers is a dogmatic habit.</p><p>Golang is idiomatic, making it imperative to structure the code in sync with its idioms.</p><p>The crux of our architecture revolves around Go’s encapsulation method. Instead of classes found in many languages, Go employs packages to encapsulate both data and behavior. <strong>This means we structure packages around the problems they address, not the component types they contain.</strong></p><p>For example, we’ll have a ‘user’ package handling user-related use cases, rather than separate packages for user entities and repositories. Both the entity and repository play roles in the user use case.</p><p>We can even name this approach <strong>vertical slice</strong> rather than a <strong>clean architecture</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zolAqCkEPtUsAtR_70TPXg.jpeg" /><figcaption>Package Diagram</figcaption></figure><p>Have a look at the project structure:</p><h3>Project Structure</h3><pre>📦morgan<br> ┣ 📂cmd<br> ┃ ┗ 📜main.go<br> ┣ 📂config<br> ┃ ┗ 📜config.go<br> ┣ 📂internal<br> ┃ ┣ 📂auth<br> ┃ ┃ ┣ 📜handler.go<br> ┃ ┃ ┣ 📜middleware.go<br> ┃ ┃ ┣ 📜service.go<br> ┃ ┃ ┗ 📜token.go<br> ┃ ┣ 📂feed<br> ┃ ┃ ┣ 📜feed.go<br> ┃ ┃ ┣ 📜handler.go<br> ┃ ┃ ┗ 📜service.go<br> ┃ ┣ 📂follow<br> ┃ ┃ ┣ 📜follow.go<br> ┃ ┃ ┣ 📜handler.go<br> ┃ ┃ ┣ 📜repo.go<br> ┃ ┃ ┗ 📜service.go<br> ┃ ┣ 📂post<br> ┃ ┃ ┣ 📂comment<br> ┃ ┃ ┃ ┣ 📜comment.go<br> ┃ ┃ ┃ ┗ 📜repo.go<br> ┃ ┃ ┣ 📂like<br> ┃ ┃ ┃ ┣ 📜like.go<br> ┃ ┃ ┃ ┗ 📜repo.go<br> ┃ ┃ ┣ 📜handler.go<br> ┃ ┃ ┣ 📜post.go<br> ┃ ┃ ┣ 📜repo.go<br> ┃ ┃ ┗ 📜service.go<br> ┃ ┗ 📂user<br> ┃ ┃ ┣ 📜handler.go<br> ┃ ┃ ┣ 📜repo.go<br> ┃ ┃ ┣ 📜service.go<br> ┃ ┃ ┗ 📜user.go<br> ┣ 📂migrations<br> ┣ 📂scripts<br> ┣ 📂test</pre><h3>Dependency Rule</h3><p>Each component depends on the interfaces it defines. Dependencies are injected into the component. The order of the dependencies is as follows: Handler -&gt; Service -&gt; Repository.</p><h3>Cross-Domain Dependencies</h3><p>We identify two categories of cross-domain dependencies:</p><ol><li>Dependencies between components within the same bounded context.</li><li>Dependencies between components across different bounded contexts.</li></ol><p>The concept of a bounded context originates from DDD. It signifies a perimeter around components sharing a common domain. For instance, <strong><em>post</em></strong>, <strong><em>comment</em></strong>, and <strong><em>like</em></strong> packages can be seen as sharing a bounded context due to their strong coupling. If one changes, the others will likely change as well.</p><p>In the <strong><em>post</em></strong> package, <strong>like</strong> and <strong>comment</strong> entities are accessed via repositories since they share the context. These sub-packages don’t need their respective services since they are only used within the <strong><em>post</em></strong> package.</p><p>Conversely, in the ‘follow’ package, the ‘user’ domain is accessed using the ‘user’ service, maintaining domain boundaries without direct repository imports.</p><h3>Conclusion</h3><blockquote>The architecture we’ve explored offers both simplicity and flexibility, making it an ideal choice for many Golang projects. It is not a perfect solution, but it is a good starting point. As your project grows, you can adapt it to suit your needs.</blockquote><p>Here is the GitHub repository for the project: <a href="https://github.com/ahmetildirim/morgan">morgan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=691e0baca7f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>