{"id":1332,"date":"2021-02-26T08:36:20","date_gmt":"2021-02-26T08:36:20","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1332"},"modified":"2021-02-27T15:49:09","modified_gmt":"2021-02-27T15:49:09","slug":"fun-with-goroutines","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/02\/26\/fun-with-goroutines\/","title":{"rendered":"Fun with `goroutines`"},"content":{"rendered":"<p>Channels are way of communication between multiple <code>goroutines<\/code><\/p>\n\n\n<p>You can declare a channel and the type of content the channel will receive like below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var myChannel chan int<\/code><\/pre>\n\n\n\n<p>The <code>int<\/code> above is the type of data it can receive and the data can be send using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Send 10 through channel\nmyChannel &lt;- 10<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Receive the channel content\nval := &lt;- myChannel<\/code><\/pre>\n\n\n\n<p>In this blog I will just play out some sample scenarios of sending valid information and waiting for the <code>goroutine<\/code> to sed\/receive the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Send<\/h3>\n\n\n\n<p><code>func send<\/code> will send data <code>[1..10]<\/code> to <code>ch<\/code> and sleep ever time it is send<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func send(ch chan int) {\n\tfor i := 0; i &lt; 10; i++ {\n\t\ttime.Sleep(1 * time.Second)\n\t\tch &lt;- i\n\t}\n\tfmt.Println(\" Sender done...\")\n\treturn\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Receive<\/h3>\n\n\n\n<p><code>func receive<\/code> will receive all the information sent to the channel till the last info.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func receive(ch chan int) {\n\tvar val int\n\n\tfor {\n\t\tval = &lt;-ch\n\t\tfmt.Printf(\" &gt; %d\\n\", val)\n\t\tif val &gt;= 10 {\n\t\t\tclose(ch)\n\t\t\tdone &lt;- true\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Term<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>func term() {\n\tdone &lt;- true\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Main<\/h3>\n\n\n\n<p>Playing out the entry point to call the send and receive<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func main() {\n\tfmt.Println(\" --- Main ---\")\n\tch := make(chan int)\n\n\tgo send(ch)\n\tgo receive(ch)\n\n\t\/\/ wait on done\n\t&lt;-done\n\tfmt.Println(\" Done.. \")\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code> &gt; 0\n &gt; 1\n &gt; 2\n &gt; 3\n &gt; 4\n &gt; 5\n &gt; 6\n &gt; 7\n &gt; 8\n Sender done...\n Closing reciever...\n &gt; 9\n Receiver done...\n Done.. \n<\/code><\/pre>\n\n\n\n<p>Source repository is available <a href=\"https:\/\/github.com\/samarthya\/gofun\">here<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What If?<\/h3>\n\n\n\n<p>If you try sending a different type to the channel type defined &#8211; what shall happen?<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Will it throw runtime error?<\/li><li>Will it ignore?<\/li><li>Will it throw compile time error?<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Easier to try<\/p><\/blockquote><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>var done chan bool = make(chan bool)\ndone &lt;- 'a'<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"205\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-26-at-2.11.16-PM-1024x205.png\" alt=\"\" class=\"wp-image-1338\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-26-at-2.11.16-PM-1024x205.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-26-at-2.11.16-PM-300x60.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-26-at-2.11.16-PM-768x154.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-26-at-2.11.16-PM.png 1470w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>It just does throws compile time error<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># command-line-arguments\nsrc\/main.go:57:7: cannot use 'a' (type untyped rune) as type bool in send<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Channels are way of communication between multiple goroutines You can declare a channel and the type of content the channel will receive like below. The int above is the type of data it can receive and the data can be send using In this blog I will just play out some sample scenarios of sending [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1334,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[137],"tags":[165,166,167],"class_list":["post-1332","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","tag-channels","tag-concurrency","tag-fun","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1332"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1332\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1334"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}