<?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: Ayush Gupta</title>
    <description>The latest articles on DEV Community by Ayush Gupta (@ayushg).</description>
    <link>https://dev.to/ayushg</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3610633%2Fa7d520d0-0bd4-4670-8f70-2e2abb52b8a4.jpeg</url>
      <title>DEV Community: Ayush Gupta</title>
      <link>https://dev.to/ayushg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayushg"/>
    <language>en</language>
    <item>
      <title>How Linux Works - Chapter 1: A Concise Summary for Beginners</title>
      <dc:creator>Ayush Gupta</dc:creator>
      <pubDate>Fri, 23 Oct 2026 01:23:01 +0000</pubDate>
      <link>https://dev.to/ayushg/how-linux-works-chapter-1-a-concise-summary-for-beginners-2a1f</link>
      <guid>https://dev.to/ayushg/how-linux-works-chapter-1-a-concise-summary-for-beginners-2a1f</guid>
      <description>&lt;p&gt;At first glance, Linux sounds strange and unapproachable, like that one girl you know. She looks at you from the corner of her eye, but you being you, you never managed to gather enough courage to just go talk to her!&lt;/p&gt;

&lt;p&gt;I’m sorry, but I unfortunately can’t help you with that particular quagmire. I can promise to make Linux more approachable for you through a chapter-by-chapter deep dive.&lt;/p&gt;

&lt;p&gt;I will be using &lt;a href="https://nostarch.com/howlinuxworks3" rel="noopener noreferrer"&gt;How Linux Works (3rd Edition) by Brian Ward&lt;/a&gt;. This book is not only updated to teach how modern Linux works, but also gives you a deep enough dive to understand all layers of abstraction.&lt;/p&gt;

&lt;p&gt;Abstraction? Nothing fancy, to be honest. It’s just a word that means you can ignore most of the details that make up the component at hand and focus on what it does and how it works.&lt;/p&gt;

&lt;p&gt;Simple example: to drive a car, you do not need to know how the engine works, you just need to know how to drive. But if you are a car mechanic, you must know engines in order to repair them, though you still don’t have to worry about the source of the iron used to make the engine in the first place. So basically, it’s abstraction on top of abstraction.&lt;/p&gt;

&lt;p&gt;As for Linux systems, there are three main layers of abstraction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hardware layer&lt;/strong&gt; - This is where all the physical components of your system live. For example, the CPU, keyboard, memory, etc.&lt;br&gt;
&lt;strong&gt;Kernel layer&lt;/strong&gt; - The heart of the Linux system. It is an interface between the user and hardware layers. The kernel is responsible for managing processes (a process is simply a running instance of a program), drivers, the CPU, etc.&lt;br&gt;
&lt;strong&gt;User layer&lt;/strong&gt; - This is where you and I live, along with the programs that we use. Each process running here has its own share of the computer’s hardware and is managed by the kernel.&lt;br&gt;
Show Image&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main Memory and State&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we go further, a quick word on main memory (RAM), since everything else depends on it. Think of it as a giant row of tiny switches, each one either 0 or 1. These are called bits. Everything your computer is working on right now lives in there, and the CPU reads and changes those bits all day long.&lt;/p&gt;

&lt;p&gt;The exact arrangement of those bits at any given moment is called state. A running process is really just its state sitting in memory, nothing more mysterious than that. Keep this in mind, because it comes back later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvd1ajj2rmqiqebmuaxty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvd1ajj2rmqiqebmuaxty.png" alt="Linux architecture diagram showing the hardware layer at the bottom, the kernel in the middle, and user processes on top" width="800" height="649"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kernel vs User Processes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;User programs do not have unrestricted access to memory. What if someone using malicious software tried to modify important files your running program needed? To prevent this, user programs are given their own separate memory space in RAM, and the kernel sets the rules for what each process can touch.&lt;/p&gt;

&lt;p&gt;User programs run in user mode and have restrictions, while the kernel runs in unrestricted kernel mode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding the Kernel&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As previously mentioned, the kernel provides an abstraction between the user and the hardware. An OS is more than a kernel, but the kernel is one of its main components. There are many jobs the kernel must perform:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Processes&lt;/strong&gt; - The kernel keeps track of which process is allowed to use the CPU.&lt;br&gt;
&lt;strong&gt;Memory&lt;/strong&gt;- The kernel needs to keep track of all memory: what is currently in use by a particular process, what might be shared between processes, and what is free.&lt;br&gt;
&lt;strong&gt;Device drivers&lt;/strong&gt; - The kernel acts as a medium between hardware and processes. It’s the kernel’s duty to operate the hardware.&lt;br&gt;
&lt;strong&gt;System calls and support&lt;/strong&gt; - System calls are the primary way for a process to communicate with the kernel.&lt;/p&gt;

&lt;p&gt;Now we will briefly touch on each of these responsibilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Process Management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Process management means everything from the start to the termination of processes. To be precise: starting, pausing, resuming, scheduling, and terminating them.&lt;/p&gt;

&lt;p&gt;On a modern operating system, we tend to see processes running “at the same time”. In reality, each process (talking strictly about a single-core CPU) runs one at a time, but switching from one process to another is so fast that it seems as if they are running simultaneously. Each process runs for a short while, then the kernel stores its state (CPU registers, memory, etc.), pulls in the next process, loads its data, and resumes it. This is known as context switching.&lt;/p&gt;

&lt;p&gt;The kernel is responsible for context switching. To understand how it works, suppose a process has been running for its time slice and the time is now up. The kernel prepares to switch from that process to another one waiting to run:&lt;/p&gt;

&lt;p&gt;The CPU interrupts the running process based on its internal timer and switches into kernel mode. This gives the kernel total control.&lt;br&gt;
The kernel stores the current state of the process.&lt;br&gt;
The kernel takes care of any pending work, like finishing up an I/O request the process had made.&lt;br&gt;
The kernel analyzes the queue of waiting processes and picks one according to its scheduling policy.&lt;br&gt;
The kernel loads the selected process and its data.&lt;br&gt;
The kernel tells the CPU how long to run the process and switches the CPU to user mode, where the process takes control of the CPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory Management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Memory is a big deal. The kernel does the supreme job of managing memory, which is a complex task. The following conditions must hold:&lt;/p&gt;

&lt;p&gt;The kernel must have a private area in memory that no process can access.&lt;br&gt;
Each user process must also have its own private section in memory.&lt;br&gt;
One user process may not access the private memory of another process.&lt;br&gt;
User processes can share memory.&lt;br&gt;
Some memory in user processes can be read-only.&lt;br&gt;
The system can use more memory than its actual size by using disk space as an auxiliary.&lt;/p&gt;

&lt;p&gt;Modern CPU architecture includes an &lt;strong&gt;MMU (Memory Management Unit)&lt;/strong&gt; that helps with address translation. When a process tries to access memory, it does not use the real address. The requested address is a virtual address, which the MMU translates into the actual physical address. This prevents the process from accessing parts of physical memory that are not its own.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Device Drivers and Management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Device drivers are typically part of the kernel. They translate generic system calls like read(), open(), etc. into actual machine signals. They run in privileged kernel space, where they have access to raw hardware.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;System Calls and Support&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;System calls allow a user process to do tasks that it cannot do on its own. Examples of system calls include read(), write(), open(), and fork().&lt;/p&gt;

&lt;p&gt;A quick note: you might see malloc() mentioned alongside these, but it isn’t a system call itself. It’s a C library function that sits on top of system calls like brk() and mmap() to ask the kernel for memory.&lt;/p&gt;

&lt;p&gt;To start a process, we need to understand two system calls in particular:&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;fork() *&lt;/em&gt;- The process creates a 1:1 copy of itself. The copy inherits all the state of the parent process.&lt;br&gt;
*&lt;em&gt;exec() *&lt;/em&gt;- It comes in many forms, but essentially it replaces the current process with the one specified (exec(program)).&lt;/p&gt;

&lt;p&gt;Other than init (PID 1, the very first process the kernel starts), every process is started by another process. When you type “ls” in the terminal, the following happens:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdg96gbftprymtiq1c5bf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdg96gbftprymtiq1c5bf.png" alt="Diagram of the Linux process creation flow: the shell calls fork() to make a child, the child calls exec() to become ls, and the parent waits" width="799" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In plain words: your shell (the program you’re typing into) calls fork() to make a copy of itself. That copy, called the child, then calls exec(“ls”), which swaps it out for the ls program. Meanwhile, the original shell (the parent) just waits until ls is done, then hands you your prompt back.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User Space&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;User space is where all the real work happens, since each process is just a state in memory. User space refers to the entire collection of memory used by running processes.&lt;/p&gt;

&lt;p&gt;From the kernel’s point of view, all programs running in user space are technically equal. Some processes make use of others for specific functions. The processes close to the user expose many features, but as we move away from the user, we get to processes that perform one task and do it well. For example, a program like vim exposes a full text editor with advanced features, while a program like grep (which is also very powerful) helps you find text in a file blazing fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Users&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One last thing before we wrap up. Every process in Linux is owned by a user. That is how the kernel decides who is allowed to do what. A regular user can play around with their own files and processes, but not with anyone else’s.&lt;/p&gt;

&lt;p&gt;Then there is root, also known as the superuser. Root can do anything, no questions asked, think of it as the master key to the whole system. Great power and all that, so be careful: one wrong command as root and you can break your entire system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Linux has three layers of abstraction: hardware, kernel, and user.&lt;br&gt;
A running process is just state (a pattern of bits) sitting in memory.&lt;br&gt;
The kernel manages processes, memory, device drivers, and system calls.&lt;br&gt;
Processes take turns on the CPU so fast it looks simultaneous. This is context switching.&lt;br&gt;
The MMU translates virtual addresses to physical ones, keeping processes out of each other’s memory.&lt;br&gt;
New processes are born through fork() (copy yourself) and exec() (become something else).&lt;br&gt;
Every process belongs to a user, and root is the all-powerful one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoyed my concise but complete overview of chapter 1 of “How Linux Works”. Please like and follow me so I can keep making educational content, improve myself, and provide you all with greater value.&lt;/p&gt;

&lt;p&gt;Thank you very much for reading. I’ll see you in the next one!&lt;/p&gt;

</description>
      <category>linux</category>
      <category>devops</category>
      <category>tutorial</category>
      <category>cli</category>
    </item>
    <item>
      <title>Finally: GO channels explained</title>
      <dc:creator>Ayush Gupta</dc:creator>
      <pubDate>Thu, 11 Dec 2025 21:18:39 +0000</pubDate>
      <link>https://dev.to/ayushg/finally-go-channels-explained-40lf</link>
      <guid>https://dev.to/ayushg/finally-go-channels-explained-40lf</guid>
      <description>&lt;p&gt;Learning Go was going just fine until I met you—channel. You pipe-looking snitch. This guy can't hold a secret! Always ready to gossip.&lt;/p&gt;

&lt;p&gt;Go channels sit right at the center of Go multi-threading. They make data transfer between goroutines as simple as bread and butter.&lt;/p&gt;

&lt;p&gt;Now let's get serious... like really serious about Go channels and how they function. But before we start exploring them, I have to promise you something: By the end of this blog post, you will have a basic understanding—even much more than that—about channels. And on this unshakeable foundation, you will be able to build your building of further, more advanced topics. This blog is not written "bookishly"—loosely translated as not being ordered in such a way that it feels boring by going too much into theory. Let's get our hands dirty from the start!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating our first channel
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;PrintValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;channel&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;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;PrintValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"hello"&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;Sleep&lt;/span&gt;&lt;span class="p"&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="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&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;Here is our channel! It sends a string from the main thread to a goroutine. The goroutine, on receiving data from the main thread, prints the value.&lt;br&gt;&lt;br&gt;
Why did I use &lt;code&gt;time.Sleep()&lt;/code&gt; here?&lt;br&gt;&lt;br&gt;
It is just for the purpose of demonstration. In real-world scenarios, we are better off using WaitGroups. &lt;code&gt;time.Sleep()&lt;/code&gt; pauses the main thread from exiting and ending the program before the other goroutine finishes execution.&lt;br&gt;&lt;br&gt;
By default, channels are unbuffered, meaning the sender and the receiver must be ready at the same time in order to transfer data.&lt;/p&gt;

&lt;p&gt;What does "being ready at the same time" mean?&lt;br&gt;&lt;br&gt;
Unbuffered channels are blocking, which means they pause execution until the receiver end is ready to receive the data.&lt;/p&gt;

&lt;p&gt;What do you think will happen here?&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;channel&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;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;channel&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;It looks like it will work, but let me break it to you—it didn't. The reason? We are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sending data into the channel in the main thread&lt;/li&gt;
&lt;li&gt;The channel is blocked until the receiver end is ready&lt;/li&gt;
&lt;li&gt;The receiver end is also in the main thread&lt;/li&gt;
&lt;li&gt;Deadlock!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's move on to our next topic: buffered channels. These types of channels have some buffer area. They have a certain amount of storage they can fill before blocking.&lt;br&gt;&lt;br&gt;
Declaring a buffered channel is quite similar to an unbuffered channel but with a capacity:&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;channel&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;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a channel of type &lt;code&gt;string&lt;/code&gt; with a capacity of 3.&lt;br&gt;&lt;br&gt;
That means we can send up to 3 complete strings before it blocks the execution.&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;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;
&lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Hi"&lt;/span&gt;
&lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Namaste"&lt;/span&gt;
&lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Salut"&lt;/span&gt; &lt;span class="c"&gt;// this will not work until and unless the&lt;/span&gt;
                   &lt;span class="c"&gt;// receiver receives previous values and frees the channel&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let us fix our earlier deadlock using a buffered channel:&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;channel&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;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;channel&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;Now this will work! The channel has a capacity of 1, which means it can store one string before blocking the execution. But does this also mean the following will work?&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;channel&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;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No matter how much capacity you increase, there will always be a deadlock.&lt;br&gt;&lt;br&gt;
The reason is simple:the receiver end is always blocking. If some part of the program is trying to receive from a channel, it will block execution. Hence, deadlock.&lt;/p&gt;

&lt;p&gt;Moving on, now that we have a rudimentary understanding of the types of channels, we can start exploring certain use cases without introducing much complexity.&lt;/p&gt;
&lt;h2&gt;
  
  
  Sending data from main thread to worker thread
&lt;/h2&gt;

&lt;p&gt;Here we create 10 worker threads using a for-loop and send some integer data to them using an unbuffered channel.&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="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&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="p"&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;Sleep&lt;/span&gt;&lt;span class="p"&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="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Completed Task:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;channel&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="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;10&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="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;channel&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="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="p"&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;Sleep&lt;/span&gt;&lt;span class="p"&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="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;6&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;Here is how the execution happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, the main thread creates a worker thread and passes the channel to it.&lt;/li&gt;
&lt;li&gt;After creation, it sends data to the channel and waits for the other end to receive while pausing further execution.&lt;/li&gt;
&lt;li&gt;The worker thread does some work (simulated by 0.5 seconds of delay) and receives the data.&lt;/li&gt;
&lt;li&gt;The main thread continues execution.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This gives us one advantage. All goroutines are supposed to run simultaneously, but here they are pausing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix using buffered channel
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&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="p"&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;Sleep&lt;/span&gt;&lt;span class="p"&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="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Completed Task:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;channel&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;10&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;10&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="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;channel&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="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="p"&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;Sleep&lt;/span&gt;&lt;span class="p"&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="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&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;Here is how the execution happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, the main thread creates worker threads and passes the channel to them.&lt;/li&gt;
&lt;li&gt;After creation, it sends data to the channel and continues execution without pausing because the channel has capacity for more data.&lt;/li&gt;
&lt;li&gt;The worker thread does some work (simulated by 0.5 seconds of delay) and receives the data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Just by adding storage to our channel, we make our program 10x faster!&lt;br&gt;&lt;br&gt;
Minimum time (just considering sleep time) before buffered channel = 0.5 * 10 = 5 seconds&lt;br&gt;&lt;br&gt;
Minimum time after buffered channel = 0.5 seconds (every thread running simultaneously)&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; It is not guaranteed how the threads will be processed. You might see output that is not always ordered the way you expect (1 to 10).&lt;/p&gt;
&lt;h2&gt;
  
  
  Directional Channels
&lt;/h2&gt;

&lt;p&gt;Let us talk about our final topic for this blog, and that is &lt;strong&gt;Directional Channels&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Directional channels let you specify what end of the channel the function will be using. They are restricted to only sending or only receiving data. This prevents a function from accidentally performing the wrong operation on a channel.&lt;/p&gt;

&lt;p&gt;Send-only channel: This informs the function that the only thing it is allowed to do with the channel is send data.&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Receive-only channel: This informs the function that it will just be receiving on this channel and nothing more.&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;receiveData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;ch&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;Here is a very simple example to illustrate directions:&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="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Hello from sender!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;receiveData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ch&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;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;sendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;receiveData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&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;In this example, &lt;code&gt;sendData&lt;/code&gt; can only send values into the channel, and &lt;code&gt;receiveData&lt;/code&gt; can only read from it. This restriction enforces clear communication direction—one function writes, the other reads—preventing accidental misuse of the channel (like trying to read in a sender function or vice versa). It's a clean, safe way to structure concurrent communication in Go.&lt;/p&gt;

&lt;p&gt;There's a lot to cover, but I think this article explained the core problems that someone might deal with when picking up channels. Strong foundation.&lt;/p&gt;

&lt;p&gt;There are more things that I want to cover, but I will give you some time to digest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ranging over channels&lt;/li&gt;
&lt;li&gt;WaitGroups&lt;/li&gt;
&lt;li&gt;select statement&lt;/li&gt;
&lt;li&gt;and many more...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;That's it for channels Part 1! Practice these examples, and you'll never fear the pipe-looking snitch again. Next time: range, select, and real-world patterns.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Have a nice day!😊&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>concurrency</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
