{"id":12115,"date":"2020-03-20T07:57:08","date_gmt":"2020-03-20T07:57:08","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=12115"},"modified":"2024-06-03T10:16:21","modified_gmt":"2024-06-03T10:16:21","slug":"goroutines-channels-and-concurrency-in-a-nutshell-part-1","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/goroutines-channels-and-concurrency-in-a-nutshell-part-1","title":{"rendered":"GoRoutines, Channels and Concurrency in a Nutshell (Part 1 of 4)"},"content":{"rendered":"<p>Hey Fellow Gophers,<\/p>\n<p>After a long time, I have decided to jot something down, and this time I have taken up a bit of an ambitious topic. I am going to cover some of the significant features of Go in a series of four blogs &#8220;<strong>GoRoutines, Channels and Concurrency<\/strong>&#8220;. This is a first blog post, where I would like to get you through GoRoutines. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/03\/giphy.gif\" alt=\"Mr. Bean\" width=\"435\" height=\"245\" class=\"aligncenter size-full wp-image-12225\" \/><\/p>\n<p>GoRoutines, let\u2019s get into the details.<\/p>\n<p>Do you agree that we have a habit of pursuing multiple things at the same time? Yes, we all do because such things make human beings unique. Such things are very common when we play the radio while doing household chores, singing songs while driving or listening to songs while programming.<\/p>\n<p>GoRoutine allows us to do multiple at a time. It helps the CPU to multitask based on how we specify to it. As per the <a href=\"https:\/\/go.dev\/\" target=\"_blank\" rel=\"noopener\">Golang<\/a> website `A GoRoutine is a lightweight thread of execution` which enables us to be multithread.<\/p>\n<p>The nature of Golang execution is synchronous, but GoRoutines enables us to run it asynchronously. Or to run a few functions concurrently.<\/p>\n<p>Let&#8217;s understand this with a simple example. Assume that we have to wait 30 seconds before printing \u201cHello\u201d and before printing \u201cWorld\u201d. First, it will code it using the traditional way that is without GoRoutines then I will do it with GoRoutines. We will monitor the time it takes individually.<\/p>\n<h2> Before that let\u2019s have a quick look at the rules of GoRoutines<\/h2>\n<ul class=\"bullets\">\n<li>To run a function as a GoRoutine, we have to precede the function call with the keyword `Go`<br \/>\n E.g. If we need to call the function RunAsynchronously() as a GoRoutine we will call it as `go RunAsynchronously()`<\/li>\n<li>As soon as the synchronous function execution stops, the GoRoutine execution stops as well regardless of the completion of the function &#8211; I have mentioned in on a later stage.<\/li>\n<\/ul>\n<p> First of all, let&#8217;s create a function that will calculate the time execution for the code programmed in it.<\/p>\n<pre>\r\npackage main\r\n import (\r\n        \t\"fmt\"\r\n        \t\"time\"\r\n)\r\n \r\nfunc simple(funcToExec func()) {\r\n        \tstart := time.Now()\r\n        \tfuncToExec()\r\n        \telapsed := time.Since(start)\r\n        \tfmt.Println(\"Time Taken for function to execute:  \", elapsed)\r\n}\r\nfunc main() {\r\n}\r\n<\/pre>\n<p>So traditionally the code would look something like this.<\/p>\n<pre>\r\npackage main\r\nimport (\r\n        \t\"fmt\"\r\n        \t\"time\"\r\n)\r\nfunc simple(funcToExec func()) {\r\n        \tstart := time.Now()\r\n        \tfuncToExec()\r\n        \telapsed := time.Since(start)\u00c7\r\n        \tfmt.Println(\"Time Taken for function to execute:  \", elapsed)\r\n}\r\nfunc main() {\r\n        \tfmt.Println(\"Traditional Execution\")\r\n        \tsimple(PrintHelloWorldTraditionally)\r\n}\r\nfunc PrintWith30SecDelay(text string) {\r\n        \ttime.Sleep(30 * time.Second)\r\n        \tfmt.Println(text)\r\n}\r\nfunc PrintHelloWorldTraditionally() {\r\n        \tPrintWith30SecDelay(\"Hello\")\r\n        \tPrintWith30SecDelay(\"World\")\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>\r\nTraditional Execution\r\nHello\r\nWorld\r\nTime Taken for function to execute:   1m0s\r\n<\/pre>\n<p>Now let&#8217;s see the difference if we run one of the print as a GoRoutine<\/p>\n<pre>\r\npackage main\r\nimport (\r\n        \t\"fmt\"\r\n        \t\"time\"\r\n)\r\nfunc simple(funcToExec func()) {\r\n        \tstart := time.Now()\r\n        \tfuncToExec()\r\n        \telapsed := time.Since(start)\r\n        \tfmt.Println(\"Time Taken for function to execute:  \", elapsed)\r\n}\r\nfunc main() {\r\n        \tfmt.Println(\"Traditional Execution\")\r\n        \tsimple(PrintHelloWorldTraditionally)\r\n        \tfmt.Println(\"Execution with GoRoutines\")\r\n        \tsimple(PrintHelloWorldWithGoRoutines)\r\n}\r\nfunc PrintWith30SecDelay(text string) {\r\n        \ttime.Sleep(30 * time.Second)\r\n        \tfmt.Println(text)\r\n}\r\nfunc PrintHelloWorldTraditionally() {\r\n        \tPrintWith30SecDelay(\"Hello\")\r\n        \tPrintWith30SecDelay(\"World\")\r\n}\r\nfunc PrintHelloWorldWithGoRoutines() {\r\n        \tgo PrintWith30SecDelay(\"Hello\")\r\n        \tPrintWith30SecDelay(\"World\")\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>\r\nTraditional Execution\r\nHello\r\nWorld\r\nTime Taken for the function to execute:   1m0s\r\nExecution with GoRoutines\r\nHello\r\nWorld\r\nTime Taken for the function to execute:   30s\r\n<\/pre>\n<p>So the call of the function that prints &#8220;Hello&#8221; and of the function that prints &#8220;World&#8221; happens concurrently. Thus the delay is reduced.<\/p>\n<p>But if you switch the GoRoutine declaration to be<\/p>\n<pre>\r\nPrintWith30SecDelay(\"Hello\")\r\ngo PrintWith30SecDelay(\"World\")\r\n<\/pre>\n<p>instead of <\/p>\n<pre>\r\ngo PrintWith30SecDelay(\"Hello\")\r\nPrintWith30SecDelay(\"World\")\r\n<\/pre>\n<p>It will result in not printing of the word &#8220;World&#8221;. But why so?<\/p>\n<p>It is because the execution of <strong>`PrintWith30SecDelay(&#8220;Hello&#8221;)`<\/strong> happens a microsecond earlier to that of <strong>`PrintWith30SecDelay(&#8220;World&#8221;)`<\/strong>. Resulting into completion of <strong>`PrintWith30SecDelay(&#8220;Hello&#8221;)`<\/strong> to be a microsecond earlier.<\/p>\n<p>So how can we solve this specific problem? The answer is with the help of WaitGroups.<\/p>\n<p>Let&#8217;s look at the code on how we would do it?<\/p>\n<pre>\r\npackage main\r\nimport (\r\n        \t\"fmt\"\r\n        \t\"sync\"\r\n        \t\"time\"\r\n)\r\n \r\nvar wg sync.WaitGroup\r\nfunc simple(funcToExec func()) {\r\n        \tstart := time.Now()\r\n        \tfuncToExec()\r\n        \telapsed := time.Since(start)\r\n        \tfmt.Println(\"Time Taken for function to execute:  \", elapsed)\r\n}\r\nfunc main() {\r\n        \tfmt.Println(\"Traditional Execution\")\r\n        \tsimple(PrintHelloWorldTraditionally)\r\n        \tfmt.Println(\"Execution with GoRoutines\")\r\n        \tsimple(PrintHelloWorldWithGoRoutines)\r\n}\r\nfunc PrintWith30SecDelay(text string, wgAdded bool) {\r\n        \tif wgAdded {\r\n                  \tdefer wg.Done()\r\n        \t}\r\n         \ttime.Sleep(30 * time.Second)\r\n        \tfmt.Println(text)\r\n}\r\nfunc PrintHelloWorldTraditionally() {\r\n        \tPrintWith30SecDelay(\"Hello\", false)\r\n        \tPrintWith30SecDelay(\"World\", false)\r\n}\r\nfunc PrintHelloWorldWithGoRoutines() {\r\n        \twg.Add(1)\r\n        \tgo PrintWith30SecDelay(\"Hello\", true)\r\n        \twg.Add(1)\r\n        \tgo PrintWith30SecDelay(\"World\", true)\r\n        \twg.Wait()\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>\r\nTraditional Execution\r\nHello\r\nWorld\r\nTime Taken for the function to execute:   8s\r\nExecution with GoRoutines\r\nHello\r\nWorld\r\nTime Taken for the function to execute:   4s\r\n<\/pre>\n<p><em>Note: GoRoutines and WaitGroups are to be used carefully, or it is likely for it to end up not working correctly or being stuck in a Deadlock.<\/em><\/p>\n<p>This is something that we can achieve with channels as well that I am going to cover it in the next blog.<\/p>\n<p>In case, if you are looking for the Golang experts to build a world-class enterprise app with high speed, high security, and high modularity, then <a href=\"https:\/\/www.bacancytechnology.com\/hire-golang-developer\" rel=\"noopener\" target=\"_blank\">hire golang developer<\/a> from us. Our Golang programmers have in-depth knowledge and extensive experience in converting potential ideas into a viable business solution.<\/p>\n<p>Regarding this blog, do let me know your thoughts in the comments section below. I will soon be back with the next part `GoRoutines, Channels and Concurrency in a Nutshell: Part 2 (Channels)` coming soon.<\/p>\n<p>Ciao.<\/p>\n<p>Happy Coding Gophers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey Fellow Gophers, After a long time, I have decided to jot something down, and this time I have taken up a bit of an ambitious topic. I am going to cover some of the significant features of Go in a series of four blogs &#8220;GoRoutines, Channels and Concurrency&#8220;. This is a first blog post, [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":12125,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[1152,1265],"tags":[],"coauthors":[1585],"class_list":["post-12115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang","category-web-development"],"acf":[],"modified_by":"Chandresh Patel","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/12115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=12115"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/12115\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/12125"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=12115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=12115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=12115"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=12115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}