<?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: Saptarshi Das</title>
    <description>The latest articles on DEV Community by Saptarshi Das (@sd007cse).</description>
    <link>https://dev.to/sd007cse</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%2F1137346%2Fc36c193d-2ac6-4d89-8380-2fc79c87496d.jpeg</url>
      <title>DEV Community: Saptarshi Das</title>
      <link>https://dev.to/sd007cse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sd007cse"/>
    <language>en</language>
    <item>
      <title>Channels in Go</title>
      <dc:creator>Saptarshi Das</dc:creator>
      <pubDate>Fri, 11 Aug 2023 16:53:35 +0000</pubDate>
      <link>https://dev.to/sd007cse/channels-in-go-273h</link>
      <guid>https://dev.to/sd007cse/channels-in-go-273h</guid>
      <description>&lt;p&gt;Channels are a typed, thread-safe queue and it allows different go-routines to communicate with each other. Channels can be created by using make keyword.&lt;/p&gt;

&lt;p&gt;For Example:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Channel Name&amp;gt; := make(chan &amp;lt;Channel Type&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can send and receive data in channels in Go lang. In Go lang this &amp;lt;- operator is called channel operator. Data flows in the direction of the arrow. This operation will block until another go-routine is ready to receive the value. Example:- If we want to send data to a channel we will be using the below code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Channel name&amp;gt; &amp;lt;- &amp;lt;Input Value&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if we want to receive data from the channel we will be using the below code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Variable&amp;gt; := &amp;lt;- &amp;lt;Channel Name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reads and removes a value from the channel and saves it into the variable. This operation will block until there is a value in the channel to be read.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deadlock and Blocking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A deadlock is when a group of go-routines are all blocking so none of them can continue. This is a common bug that we need to watch out for in concurrent programming.&lt;/p&gt;

&lt;p&gt;We can block and wait until something is sent on a channel using the code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;-&amp;lt;Channel Name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will block until it pops a single item off the channel, then continue, discarding the item.&lt;/p&gt;

&lt;h2&gt;
  
  
  Buffered Channels
&lt;/h2&gt;

&lt;p&gt;Channels can optionally be buffered. We can provide a buffer length as the second argument to make() to create a buffered channel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Channel Name&amp;gt; := make(chan, &amp;lt;Buffer Length&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sending on a buffered channel only blocks when the buffer is full. Receiving blocks only when the buffer is empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Closing Channels in Go&lt;/strong&gt;&lt;br&gt;
Channels can be explicitly closed by a sender:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Channel Name&amp;gt; := make(chan &amp;lt;Channel Type&amp;gt;)

// do some stuff with the channel

close(&amp;lt;Channel Name&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can check if the channel is closed or not by using "ok" after the variable for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Variable&amp;gt;,ok := &amp;lt;-&amp;lt;Channel Name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;receivers can check the ok value when receiving from a channel to test if a channel was closed.&lt;/p&gt;

&lt;p&gt;If ok is false if the channel is empty and closed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;_Sending on a closed channel will cause a panic. A panic on the main go-routine will cause the entire program to crash, and a panic in any other go-routine will cause that go-routine to crash.&lt;/p&gt;

&lt;p&gt;Closing isn't necessary. There's nothing wrong with leaving channels open, they'll still be garbage collected if they're unused. You should close channels to indicate explicitly to a receiver that nothing else is going to come across._&lt;/p&gt;

&lt;h2&gt;
  
  
  Range in Channels
&lt;/h2&gt;

&lt;p&gt;Channels can be ranged over.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for &amp;lt;item&amp;gt; := range &amp;lt;Channel Name&amp;gt; {
    // item is the next value received from the channel
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example will receive values over the channel (blocking at each iteration if nothing new is there) and will exit only when the channel is closed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Select Statement
&lt;/h2&gt;

&lt;p&gt;Now if we have a single go-routine listening to multiple channels and want to process data in the order it comes through each channel.&lt;/p&gt;

&lt;p&gt;A select statement is used to listen to multiple channels at the same time. It is similar to a switch statement but for channels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select {
  case i, ok := &amp;lt;- &amp;lt;Channel Name&amp;gt;:
    fmt.Println(i)
  case s, ok := &amp;lt;- &amp;lt;Channel Name&amp;gt;:
    fmt.Println(s)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first channel with a value ready to be received will fire and its body will execute. If multiple channels are ready at the same time one is chosen randomly. The ok variable in the example above refers to whether or not the channel has been closed by the sender yet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default Case&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The default case in a select statement executes immediately if no other channel has a value ready. A default case stops the select statement from blocking.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select {
  case v := &amp;lt;- &amp;lt;Channel Name&amp;gt;:
    // use v
  default:
    // receiving from channel would block
    // so do something else
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;There are two type of channels are - Read-Only Channels and Write-Only Channels.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Read-Only Channel - A channel can be a read-only by casting it from a chan to a &amp;lt;-chan type. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main(){
  &amp;lt;Channel Name&amp;gt; := make(chan &amp;lt;Type&amp;gt;)
  readCh(&amp;lt;Channel Name&amp;gt;)
}

func readCh(&amp;lt;Channel Name&amp;gt; &amp;lt;-chan &amp;lt;Type&amp;gt;) {
  // ch can only be read from
  // in this function
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write-Only Channel - The write-only channels can be created by changing the position of the arrow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func writeCh(&amp;lt;Channel Name&amp;gt; chan&amp;lt;-&amp;lt;Type&amp;gt;) {
  // ch can only be written to
  // in this function
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also give a visit to the article &lt;a href="https://dave.cheney.net/2014/03/19/channel-axioms"&gt;Dave Cheney's awesome article&lt;/a&gt;, here you can learn about the extreme cases of Channels.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>go</category>
      <category>productivity</category>
      <category>development</category>
    </item>
  </channel>
</rss>
