<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: M Yauri M Attamimi</title>
    <description>The latest articles on DEV Community by M Yauri M Attamimi (@yauritux).</description>
    <link>https://dev.to/yauritux</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F112793%2F16ef8dad-7a86-448e-9dc6-93c473384143.jpg</url>
      <title>DEV Community: M Yauri M Attamimi</title>
      <link>https://dev.to/yauritux</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yauritux"/>
    <language>en</language>
    <item>
      <title>How I Personally Grasp the Essence of Concurrency in Go</title>
      <dc:creator>M Yauri M Attamimi</dc:creator>
      <pubDate>Wed, 25 Mar 2026 18:02:44 +0000</pubDate>
      <link>https://dev.to/yauritux/how-i-personally-grasp-the-essence-of-concurrency-in-go-238e</link>
      <guid>https://dev.to/yauritux/how-i-personally-grasp-the-essence-of-concurrency-in-go-238e</guid>
      <description>&lt;h2&gt;
  
  
  Philosophy, Patterns &amp;amp; The Power of Built-in Tools
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Why Go says “Don’t communicate by sharing memory” and how to build robust systems without external libraries.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you come from a background in Java (like me), Python, or JavaScript, the word “concurrency” might trigger a slight sense of anxiety. We are taught that threads are dangerous, race conditions are lurking around every corner, and debugging deadlocks is a nightmare.&lt;/p&gt;

&lt;p&gt;Then you meet Go.&lt;/p&gt;

&lt;p&gt;Go doesn’t just offer a new way to write concurrent code; it offers a new way to think about it. But beyond the famous mantras, how does this philosophy translate to actual engineering ? And perhaps most importantly, do you need external libraries to make it work ?&lt;/p&gt;

&lt;p&gt;The answer is a resounding NO. Everything you need to build enterprise-grade concurrent systems is built directly into the language and its standard library.&lt;/p&gt;

&lt;p&gt;In this post, we’ll recap the core philosophies of Go concurrency, explore the patterns that make it powerful, and highlight the built-in tools that let you implement them without adding a single dependency.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1. The Core Philosophy: CSP vs. Shared Memory&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The most famous quote in Go concurrency comes from Rob Pike:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Don’t communicate by sharing memory; share memory by communicating.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the principle of CSP (Communicating Sequential Processes). To understand it, we need to contrast it with the traditional model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Traditional Model: Shared Memory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a single-occupancy bathroom in an office or any public places.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Resource: The bathroom (a variable in memory). &lt;/li&gt;
&lt;li&gt;The Problem: Multiple people (threads) need to use it.&lt;/li&gt;
&lt;li&gt;The Solution: A lock on the door (mutex). You must acquire the key, use the bathroom, and return (release/unlock) the key after you use it.&lt;/li&gt;
&lt;li&gt;The Risk: What if someone forgets to unlock the door ? (Deadlock). What if someone sneaks in without the key ? (Race Condition).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Go Model: CSP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a factory conveyor belt (a Channel).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Resource: The parts being built (data).&lt;/li&gt;
&lt;li&gt;The Process: Worker A places a part on the belt. The belt moves it to Worker B.&lt;/li&gt;
&lt;li&gt;The Safety: At any specific moment, only one person is holding the part. When it is on the belt, no one is touching it.&lt;/li&gt;
&lt;li&gt;The Focus: You aren’t protecting a static (shared) variable; You are managing the flow of data.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;In Go: You don’t protect a variable with a lock! You pass the variable over a chan. Ownership is transferred safely from one goroutine to another.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  2. The Engineering Mantra: Sequence and Backpressure
&lt;/h2&gt;

&lt;p&gt;There is a second, practical mantra that follows the philosophy:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Don’t over engineer things by using shared memory and complicated, error prone synchronization primitives; instead, use message-passing between Goroutines so variable and data can be used in the appropriate sequence.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is where Go shines in production. Channels aren’t just for moving data; they are for controlling time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Relay Race Analogy&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared Memory: Workers check a whiteboard every second to see if the previous step is done. (Busy waiting, complex logic)&lt;/li&gt;
&lt;li&gt;Channels: Workers stand in a line. Worker 2 physically cannot start until Worker 1 hands them the baton.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why this matters:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sequence Guaranteed:&lt;/strong&gt; A receive operation ( &lt;code&gt;&amp;lt;-&lt;/code&gt; ) blocks automatically until data arrives. You don’t need to write if done {...} loops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Natural Backpressure:&lt;/strong&gt; If the Consumer is slow, the Channel fills up. The Producer automatically blocks and waits. This prevents your program from consuming all RAM trying to process data faster than the database can save it.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  3. The Producer-Consumer Problem (Solved Simply)
&lt;/h2&gt;

&lt;p&gt;The classic Producer-Consumer pattern is kind of “Hello World” of concurrency. It solves the problem of balancing speed between data creation and data processing.&lt;/p&gt;

&lt;p&gt;In many languages, implementing a &lt;strong&gt;bounded-buffer&lt;/strong&gt; (a queue that stops accepting data when full) requires complex condition variables and locks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Traditional Way (as conceptually handled in Java)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Requires Mutex, Condition Variables, Wait(), Signal(), etc.&lt;/span&gt;
&lt;span class="c1"&gt;// Easy to get wrong. Hard to read.&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;acquire&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isFull&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The “Go” Way&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Go, a &lt;strong&gt;Buffered Channel&lt;/strong&gt; is a thread-safe Producer-Consumer queue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// Create a buffer that holds exactly 5 items&lt;/span&gt;
&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// Producer&lt;/span&gt;
&lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="c"&gt;// Automatically blocks if channel is full (Backpressure!)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nb"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}()&lt;/span&gt;

&lt;span class="c"&gt;// Consumer&lt;/span&gt;
&lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// Automatically blocks if channel is empty&lt;/span&gt;
        &lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;That’s it.&lt;/strong&gt; No locks. No condition variables. No manual signaling. The language runtime handles the synchronization for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. The Go Standard Toolbox (No External Libraries Needed)
&lt;/h2&gt;

&lt;p&gt;One of Go’s greatest strengths is that you do not need third-party frameworks to handle different patterns of concurrency. The standard library is batteries-included.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+---------------------+----------------+------------------+--------------+
|.  Pattern.          |. Tool.         | Package.         |.  Built-In ? | 
+---------------------|----------------|------------------|--------------+
| Lightweight Threads | goroutine      | Language Keyword |  ✅ Yes      |
| Communication       | chan           | Language Keyword |  ✅ Yes      |
| State Protection    | sync.Mutex     | sync             |  ✅ Yes      |
| Wait Groups         | sync.WaitGroup | sync             |  ✅ Yes      |
| High Perf Counters  | atomic         | sync/atomic      |  ✅ Yes      |
| Lifecycle/Timeout   | context        | context          |  ✅ Yes      |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Structure Concurrency with&lt;/strong&gt; &lt;code&gt;context&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In traditional concurrency, knowing when to stop a &lt;code&gt;goroutine&lt;/code&gt; is hard. Go solves this with the &lt;code&gt;context&lt;/code&gt; package. It allows you to cascade cancellation signals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cancel&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WithTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Background&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c"&gt;// Pass ctx to your goroutines. &lt;/span&gt;
&lt;span class="c"&gt;// If the timeout hits, all listening goroutines stop gracefully.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents &lt;strong&gt;goroutine leaks&lt;/strong&gt;, a common bug in other languages where background tasks keep running forever after a request finishes.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. When to Break the Rules
&lt;/h2&gt;

&lt;p&gt;While “Share memory by communicating” is the default path, Go is pragmatic. As an engineer we know that there’s “size fits for all”. It always depends on the usecase / problem we’re trying to solve. There are times when shared memory is better:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Simple State:&lt;/strong&gt; If you just need to protect a simple config map or cache, a &lt;code&gt;sync.Mutex&lt;/code&gt; is often clearer than setting up a channel loop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Channels have a small overhead. For extremely high-frequency counters, &lt;code&gt;sync/atomic&lt;/code&gt; is faster.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internal State:&lt;/strong&gt; If a struct has internal state that shouldn’t be exposed, protecting it with a mutex inside its methods is cleaner.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Rule of Thumb:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;channels&lt;/strong&gt; to orchestrate &lt;strong&gt;flow&lt;/strong&gt; (pipelines, worker pools)&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;mutexes&lt;/strong&gt; to protect state (caches, configs)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  6. Beyond the Basics: Actor Model &amp;amp; Async
&lt;/h2&gt;

&lt;p&gt;Go’s model is unique compared to other ecosystems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Vs. Async/Await (JS/Python):&lt;/strong&gt; Go hides the complexity. You write code that &lt;em&gt;looks synchronous&lt;/em&gt; (e.g., &lt;code&gt;response:= http.Get(...)&lt;/code&gt;), but the runtime parks the &lt;code&gt;goroutine&lt;/code&gt; efficiently. &lt;strong&gt;No callback hell&lt;/strong&gt;, &lt;strong&gt;no explicit&lt;/strong&gt; &lt;code&gt;await&lt;/code&gt; keywords everywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vs. Actor Model (Erlang/Elixir/Akka):&lt;/strong&gt; &lt;strong&gt;Go&lt;/strong&gt; is similar but more flexible. In &lt;em&gt;Actor Model&lt;/em&gt; pattern: mailboxes (&lt;em&gt;channel&lt;/em&gt; in &lt;strong&gt;Go&lt;/strong&gt;) belong to actors. In &lt;strong&gt;Go&lt;/strong&gt;, &lt;em&gt;channels&lt;/em&gt; are independent entities that multiple &lt;code&gt;goroutines&lt;/code&gt; can share. However, you can build an Actor in Go using a &lt;code&gt;goroutine + switch statement + channel&lt;/code&gt;, but you aren’t forced into that paradigm.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Concurrency in Go is not just about doing multiple things at once; its about &lt;strong&gt;composing&lt;/strong&gt; those things safely.&lt;/p&gt;

&lt;p&gt;By adopting the CSP philosophy, you shift your mindset from &lt;strong&gt;protecting resources&lt;/strong&gt; (&lt;em&gt;locks&lt;/em&gt;) to &lt;strong&gt;managing flow&lt;/strong&gt; (&lt;em&gt;channels&lt;/em&gt;). By leveraging the built-in tools like &lt;code&gt;chan&lt;/code&gt;, &lt;code&gt;context&lt;/code&gt;, and &lt;code&gt;sync&lt;/code&gt;, you avoid the dependency hell common in other ecosystems.&lt;br&gt;
You get:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Safety:&lt;/strong&gt; Race conditions are harder to introduce.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backpressure:&lt;/strong&gt; Systems self-regulate under load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity:&lt;/strong&gt; Complex workflows look like simple pipeline.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So.. the next time you face a concurrency problem, remember the mantra: &lt;strong&gt;Don’t communicate by sharing memory. Share memory by communicating&lt;/strong&gt;. And trust the tools that come right out of the box.&lt;/p&gt;

</description>
      <category>go</category>
      <category>goroutine</category>
      <category>multithreading</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Clean Code Architecture in Go</title>
      <dc:creator>M Yauri M Attamimi</dc:creator>
      <pubDate>Sun, 05 Apr 2020 14:40:57 +0000</pubDate>
      <link>https://dev.to/yauritux/clean-code-architecture-in-go-9fj</link>
      <guid>https://dev.to/yauritux/clean-code-architecture-in-go-9fj</guid>
      <description>&lt;p&gt;Everyone can write code to produce a software that functionally works as expected, yet not everyone care about how's their code architecture look like. The best chosen algorithms not always save us from the bad software design. There are many code architectures around, and recently i've been struggling and trying to promote the &lt;a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html"&gt;Clean Code Architecture&lt;/a&gt; from Robert C. Martin a.k.a. &lt;a href="http://cleancoder.com/products"&gt;Uncle Bob&lt;/a&gt;. Let's start with how this sort of code architecture looks like in &lt;a href="https://golang.org/"&gt;Go&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I’m gonna start by implementing some of the functionalities within a shopping cart since it’s pretty straightforward. Bear in mind that what i’ll be showing here is restricted to the &lt;em&gt;bounded context&lt;/em&gt; of the shopping cart, means we’ll just focus with the process of how a user/buyer choose the item and adding that particular item into his cart, and subsequently continue with the checkout. As for the billing/payment part, that will be in the &lt;em&gt;bounded context&lt;/em&gt; of another service such as payment service. (if you’re neither really familiar nor confidence with what &lt;em&gt;bounded context&lt;/em&gt; is, i would suggest you to read another article of mine from &lt;a href="https://medium.com/@yauritux/ddd-part-i-introduction-cabab1d2e27d"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By the way, all of the source code i explain here can be cloned/forked from &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qF2jUiUG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-6a5bca60a4ebf959a6df7f08217acd07ac2bc285164fae041eacb8a148b1bab9.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/yauritux"&gt;
        yauritux
      &lt;/a&gt; / &lt;a href="https://github.com/yauritux/clean-code-architecture"&gt;
        clean-code-architecture
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      My personal opinion on the Clean Code Architecture in the context of Domain Driven Design (DDD), yet i adopted some terms from the Onion Architecture such as domain to avoid any misleading interpretations with the Entity in DDD
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f4cC0FsO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cfgza08oi41iq9ke9us0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f4cC0FsO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cfgza08oi41iq9ke9us0.jpg" alt="Common Shopping Cart Process"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What are highlighted in the grey colors were standing outside of our shopping cart &lt;em&gt;bounded context&lt;/em&gt;. Hence, we won’t tackle the nitty gritty details of those objects within our code implementation since those things would be in other services (e.g. &lt;strong&gt;product catalogue service&lt;/strong&gt; and &lt;strong&gt;user service&lt;/strong&gt;). Nevertheless, we’ll still be having those objects within our code, in the &lt;em&gt;context&lt;/em&gt; of what our shopping cart needs. For instance, we won’t need user credentials information in our shopping cart since that should be catered from the &lt;strong&gt;user service&lt;/strong&gt; context. Basically, we just need user information which is related to the shopping cart itself, such as: &lt;strong&gt;user_session_id&lt;/strong&gt;, &lt;strong&gt;shipping address&lt;/strong&gt;, &lt;strong&gt;billing address&lt;/strong&gt; (furthermore, address information should also be catered from another service, e.g. : &lt;strong&gt;address service&lt;/strong&gt;, even this is also optional).&lt;/p&gt;

&lt;p&gt;There are some rules for this architecture those we must bear within our mind. I've summarized those rules as following: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dependency between layer should be going inward in one direction. As far as i concern, it was designed like this to avoid any &lt;a href="https://en.wikipedia.org/wiki/Circular_dependency"&gt;circular dependencies&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;A layer should depend only on one direct layer (one level) beneath it, it shouldn’t know any layers beyond that level. For instance, &lt;strong&gt;A&lt;/strong&gt; depends on &lt;strong&gt;B&lt;/strong&gt; ( &lt;strong&gt;A → B&lt;/strong&gt; ), and &lt;strong&gt;B&lt;/strong&gt; depends on &lt;strong&gt;C&lt;/strong&gt; ( &lt;strong&gt;B → C&lt;/strong&gt; ), yet &lt;strong&gt;A&lt;/strong&gt; shouldn’t know anything about &lt;strong&gt;C&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Each layer should depends upon abstraction ( interface ) rather than implementation. This complies with what &lt;strong&gt;Uncle Bob&lt;/strong&gt; said about 2 rules to fix &lt;strong&gt;RFI&lt;/strong&gt; issues, you can read it from my old article &lt;a href="https://yauritux.wordpress.com/2011/04/03/history-of-dependency-injection-di/"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s jump to the implementation by creating our project folder from the terminal and also initialize the go module as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir cart-service &amp;amp;&amp;amp; cd cart-service
go mod init github.com/yauritux/cart-svc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Next step is creating our &lt;code&gt;pkg&lt;/code&gt; directory (&lt;code&gt;mkdir pkg&lt;/code&gt;) from the same terminal. This &lt;code&gt;pkg&lt;/code&gt; directory will contain our implementation logic for the &lt;strong&gt;shopping cart&lt;/strong&gt; which complies with the &lt;strong&gt;clean code architecture&lt;/strong&gt; we’re discussing here.&lt;/p&gt;

&lt;p&gt;By referring to the depicted shopping cart process diagram earlier, we’ll be having these following &lt;strong&gt;entities&lt;/strong&gt; within our code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;User&lt;/strong&gt; (this also represents a &lt;strong&gt;Buyer&lt;/strong&gt; in the &lt;strong&gt;shopping cart&lt;/strong&gt; &lt;em&gt;bounded context&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cart&lt;/strong&gt; (as the name implies, this is our cart object which holds our shopping items).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Product&lt;/strong&gt; (used as a placeholder for the product information. And for the sake of simplicity…, we won’t have any product categories because our goal here was merely to discuss the Clean Code Architecture).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I create those 3 &lt;strong&gt;entities&lt;/strong&gt; within my &lt;code&gt;pkg/domain/entity&lt;/code&gt; folder. To keep it simple, i deliberately marked all of the fields as &lt;em&gt;exported fields&lt;/em&gt; (moving forward, we can think about hiding the fields information behind the &lt;em&gt;setter&lt;/em&gt; / &lt;em&gt;getter&lt;/em&gt; which are commons in &lt;strong&gt;Java&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cart Entity&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;code&gt;User Entity&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Product Entity&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And here our &lt;strong&gt;value objects&lt;/strong&gt; ( &lt;code&gt;pkg/domain/valueobject&lt;/code&gt; ):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Cart Items&lt;/strong&gt; (strongly related to &lt;strong&gt;Product&lt;/strong&gt; entity).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Buyer Address&lt;/strong&gt; (comprises of &lt;strong&gt;Billing&lt;/strong&gt; and &lt;strong&gt;Shipping addresses&lt;/strong&gt;, determined by the &lt;em&gt;Address Type&lt;/em&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;CartItem Value Object&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;BuyerAddress Value Object&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Additionally, we will also have our &lt;strong&gt;enums&lt;/strong&gt; as shown below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you’re wondering about the differences between entities and value objects, you can read it from &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@yauritux/ddd-part-ii-b0735ba584ca" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AGc2xPFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2AMoWVt2pb-eE9N4KXCxXfFg.png" alt="M Yauri Attamimi"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@yauritux/ddd-part-ii-b0735ba584ca" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;DDD Part II - M Yauri Attamimi - Medium&lt;/h2&gt;
      &lt;h3&gt;M Yauri Attamimi ・ &lt;time&gt;Jul 15, 2017&lt;/time&gt; ・ 5 min read
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aYMKNcyE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/medium_icon-fbdac08496f06c5bd53be920c7bc8d56d355b69c0fb7e49cac6357a70140af17.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
.

&lt;p&gt;The next important thing to do is to implement our core business logic, which is part of &lt;strong&gt;Entities&lt;/strong&gt; in &lt;strong&gt;Clean Code Architecture&lt;/strong&gt;, or &lt;strong&gt;Core Domain&lt;/strong&gt; in terms of &lt;a href="https://www.thinktocode.com/2018/08/16/onion-architecture/"&gt;Onion Architecture&lt;/a&gt;. This is something that is related to the enterprise business rules out of the application service rule.&lt;/p&gt;

&lt;p&gt;In order to implement this kinda thing, we need to setup our &lt;strong&gt;aggregate root&lt;/strong&gt; (i call it as &lt;strong&gt;user_cart aggregate&lt;/strong&gt;). Again, take a look at what i wrote &lt;a href="https://medium.com/@yauritux/ddd-part-ii-b0735ba584ca"&gt;here&lt;/a&gt; if you’re still wondering about what is the aggregate all about.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Another to-do thing is to setup our &lt;strong&gt;repositories&lt;/strong&gt;. Bear in mind, that when we’re talking about repository, it’s not always about the &lt;strong&gt;database&lt;/strong&gt;. The storage implementation could be anything such as: &lt;strong&gt;web services&lt;/strong&gt;, &lt;strong&gt;file&lt;/strong&gt;, &lt;strong&gt;in-memory&lt;/strong&gt;, etc.., and they should be abstracted by following some contracts defined on the interface. In that way, we can use them interchangeably later since we make our client code depends on the interface rather than directly on the implementation. In order to make it possible, we should start by abstracting our &lt;strong&gt;repositories&lt;/strong&gt; with &lt;strong&gt;interfaces&lt;/strong&gt; such as following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cart Repository Interface&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;User Repository Interface&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Product Repository Interface&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I would assume that some of you will be asking about those &lt;strong&gt;empty interfaces&lt;/strong&gt; :-). Why do they exist ? Why do we use them ?.&lt;/p&gt;

&lt;p&gt;As far as i concern, we’ll be having different models for every layer in order to make our architecture more loosely-coupled. In that case, we should abstract our interface model as far as we can, thus we can have more flexibility within our code. Let’s say for the user repository interface, we can have 2 implementations for it, one implementation to get the user information from the database, and another one to get the information from a web service (e.g. REST). Therefore, definitely we’ll have 2 models here, one based on the database model, and another one is based on the web request-response model. That’s the reason why we use &lt;strong&gt;empty interface&lt;/strong&gt; ( &lt;code&gt;interface{}&lt;/code&gt; ) within our repository interface. You get it right ? :-). Don’t worry, everything will become clear once we implement our interface.&lt;/p&gt;

&lt;p&gt;For the sake of simplicity, we'll be implementing just &lt;strong&gt;in-memory repository&lt;/strong&gt; for now since our first goal is merely to discuss about how’s our clean code architecture will be looked like. However, i will incrementally update the code later for another kind of storage / repository, such as database or web service.&lt;/p&gt;

&lt;p&gt;Let’s start to implement our repository interface by creating another directory (at the same level with our &lt;code&gt;domain&lt;/code&gt; directory) as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p pkg/adapter/repository/inmem/model
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And here are our models (inside the &lt;code&gt;pkg/adapter/repository/inmem/model&lt;/code&gt; directory) to be used in the context of our &lt;strong&gt;in-memory repository&lt;/strong&gt; (remember about the &lt;code&gt;interface{}&lt;/code&gt; before, when we designed our repository contract interface).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;User Model for In-Memory Repository Implementation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Cart Model for In-Memory Repository Implementation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Product Model for In-Memory Repository Implementation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;subsequently followed by our in-memory repository implementation for those 3 models respectively (inside &lt;code&gt;/adapter/repository/inmem directory&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;User In-Memory Repository Implementation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Cart In-Memory Repository Implementation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Product In-Memory Repository Implementation&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And the &lt;em&gt;use cases&lt;/em&gt; for &lt;strong&gt;cart&lt;/strong&gt; and &lt;strong&gt;user&lt;/strong&gt; as written below :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;User Usecase Port&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;User Usecase Interactor&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Cart Usecase Port&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Cart Usecase Interactor&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Product Usecase Interactor&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Last but not least, let’s create a small CLI program to test our shopping cart functionalities that we’ve created so far.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Shopping Cart CLI tester using in-memory repository&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Ok, some of you might get overwhelmed with all of the code here (while some of you might not), especially in regards with the &lt;strong&gt;use case port&lt;/strong&gt; and &lt;strong&gt;interactor&lt;/strong&gt; (what would be the difference between those 2).&lt;/p&gt;

&lt;p&gt;Test our CLI by using this following command from the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run cmd/cli/main.go
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can grab the full code from &lt;a href="https://github.com/yauritux/clean-code-architecture"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let me know for any concerns and/or questions from you guys :-).&lt;/p&gt;

</description>
      <category>go</category>
      <category>architecture</category>
      <category>cleancodearchitecture</category>
    </item>
  </channel>
</rss>
