-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.xml
1292 lines (910 loc) · 167 KB
/
feed.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.2.0">Jekyll</generator><link href="https://blogs.rajendraarora.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://blogs.rajendraarora.com/" rel="alternate" type="text/html" /><updated>2022-01-26T19:12:36+07:00</updated><id>https://blogs.rajendraarora.com/feed.xml</id><title type="html">Research blogs</title><subtitle>💻 Developer by day, 🎮 gamer by night | Software Engineer in @Taboola | ☕ sometimes coffee lover | A good player in call of duty | 📷 sometimes love photography</subtitle><author><name>Raj Arora</name></author><entry><title type="html">Channel in Golang</title><link href="https://blogs.rajendraarora.com/channel/2022/01/23/channel.html" rel="alternate" type="text/html" title="Channel in Golang" /><published>2022-01-23T00:00:00+07:00</published><updated>2022-01-23T00:00:00+07:00</updated><id>https://blogs.rajendraarora.com/channel/2022/01/23/channel</id><content type="html" xml:base="https://blogs.rajendraarora.com/channel/2022/01/23/channel.html"><h1 id="channel">Channel</h1>
<p>A channel is a communication technique which allows a Goroutine to communicate with another Goroutine.
<br /><br /></p>
<p><img src="/assets/img/channel.png" alt="Channel" /></p>
<p><br /></p>
<p>You may notice the “main routine” in above screenshot. As soon as the main routine run of code to execute. The entire program will quit even the other child routine is running.</p>
<p><br /></p>
<p><img src="/assets/img/channel2.png" alt="main routine" /></p>
<p><br /></p>
<p><strong>Declaration</strong></p>
<p>We can declare channel by writing the following syntax:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">var</span> <span class="n">channel_name</span> <span class="k">chan</span> <span class="n">Type</span></code></pre></figure>
<p>Or We can use a shorthand declaration with a builtin <code class="language-plaintext highlighter-rouge">make()</code> function:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="n">channel_name</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">string</span><span class="p">)</span></code></pre></figure>
<p><br /></p>
<p>Let’s see some basic example:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="n">channelName</span> <span class="k">chan</span> <span class="kt">int</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Value of the channel: "</span><span class="p">,</span> <span class="n">channelName</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"Type of the channel: %T"</span><span class="p">,</span> <span class="n">channelName</span><span class="p">)</span>
<span class="n">channelName1</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">int</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">Value of the channel: "</span><span class="p">,</span> <span class="n">channelName1</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"Type of the channel: %T"</span><span class="p">,</span> <span class="n">channelName1</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>The following output will be:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Value of the channel: &lt;nil&gt;</span>
<span class="c">// Type of the channel: chan int</span>
<span class="c">// Value of the channel: 0xc0000160c0</span>
<span class="c">// Type of the channel: chan int</span></code></pre></figure>
<p><br /></p>
<p><strong>How to send data with channel?</strong></p>
<p><br /></p>
<p><img src="/assets/img/channel3.png" alt="Sending data with channel" /></p>
<p><br /></p>
<ul>
<li>Here <code class="language-plaintext highlighter-rouge">5</code> is the value as input which we are sending through channel with the syntax <code class="language-plaintext highlighter-rouge">channel &lt;- 5</code></li>
<li>We can receive the value out of channel (For e.g: when you receive text message and you read it via phone) with the syntax <code class="language-plaintext highlighter-rouge">myNumber &lt;- channel</code></li>
<li>To log the channel value <code class="language-plaintext highlighter-rouge">fmt.Println(&lt;- channel)</code></li>
</ul>
<p><br /></p>
<p>Let’s have a quick at this simple channel program:</p>
<p><br /></p>
<p>This program will sum up through channel:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Start main function"</span><span class="p">)</span>
<span class="n">ch</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">int</span><span class="p">)</span>
<span class="k">go</span> <span class="n">sumValue</span><span class="p">(</span><span class="n">ch</span><span class="p">)</span>
<span class="n">ch</span> <span class="o">&lt;-</span> <span class="m">5</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"End main function"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">sumValue</span><span class="p">(</span><span class="n">num</span> <span class="k">chan</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="m">5</span> <span class="o">+</span> <span class="o">&lt;-</span><span class="n">num</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Start main function</span>
<span class="c">// 10</span>
<span class="c">// End main function</span></code></pre></figure>
<p><br /></p>
<p>Another simple channel example with greet message:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Start main function"</span><span class="p">)</span>
<span class="n">ch</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">string</span><span class="p">)</span>
<span class="k">go</span> <span class="n">greetMsg</span><span class="p">(</span><span class="n">ch</span><span class="p">)</span>
<span class="n">ch</span> <span class="o">&lt;-</span> <span class="s">"world"</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"End main function"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">greetMsg</span><span class="p">(</span><span class="n">ch</span> <span class="k">chan</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Hello"</span><span class="p">,</span> <span class="o">&lt;-</span><span class="n">ch</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Start main function</span>
<span class="c">// Hello world</span>
<span class="c">// End main function</span></code></pre></figure>
<p><br /></p>
<p>Let’s have a quick another look at this simple channel program:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="c">// syntax for channel</span>
<span class="n">ch</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">string</span><span class="p">)</span>
<span class="n">ch</span> <span class="o">&lt;-</span> <span class="s">"this is my msg 1"</span>
<span class="n">ch</span> <span class="o">&lt;-</span> <span class="s">"this is my msg 2"</span>
<span class="n">ch</span> <span class="o">&lt;-</span> <span class="s">"this is my msg 3"</span>
<span class="n">ch</span> <span class="o">&lt;-</span> <span class="s">"this is my msg 4"</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="o">&lt;-</span><span class="n">ch</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>Here our channel is unbuffered (we didn’t specify any buffer size when make()ing the channel) meaning that they will only accept sends (channel &lt;-) if there is a corresponding receive () ready to receive the sent value. So in this case ‘ch’ is an unbuffered channel and we are attempting to send onto it,
but there is no receiver of this channel.</p>
<p>We can fix it by either making the channel buffered or registering a receiver on this channel in another go routine.</p>
<p>So we might see some out error:
fatal error: all goroutines are asleep - deadlock!</p>
<p><br /></p>
<p>To avoid this error, we can specify the buffer size:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="n">ch</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">string</span><span class="p">,</span> <span class="m">4</span><span class="p">)</span></code></pre></figure>
<p>We won’t see error here anymore after specifying the buffer size.</p>
<p><br /></p>
<p><strong>Close() function</strong></p>
<p>We can close the channel with builtin <code class="language-plaintext highlighter-rouge">close()</code> function. This sets the response with <code class="language-plaintext highlighter-rouge">true/false</code> value to indicate no more value will be send to the channels.</p>
<p><br /></p>
<p>Open or close channel value can be checked with the following syntax:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="n">res</span><span class="p">,</span> <span class="n">status</span> <span class="o">:=</span> <span class="o">&lt;-</span><span class="n">ch</span></code></pre></figure>
<p><br /></p>
<p>Let’s try with one example to check the close function:</p>
<p><br /></p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"strconv"</span>
<span class="p">)</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="c">/* Creating a channel */</span>
<span class="n">ch</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="kt">string</span><span class="p">)</span>
<span class="c">/* Calling Gorouting */</span>
<span class="k">go</span> <span class="n">goRoutineFun</span><span class="p">(</span><span class="n">ch</span><span class="p">)</span>
<span class="c">/*
when the value of "status"
receives as "true" that means
channel is open and it can send and receive data.
When the value of "status" receives as "false" means
channel is closed.
*/</span>
<span class="k">for</span> <span class="p">{</span>
<span class="n">res</span><span class="p">,</span> <span class="n">status</span> <span class="o">:=</span> <span class="o">&lt;-</span><span class="n">ch</span>
<span class="k">if</span> <span class="n">status</span> <span class="o">==</span> <span class="no">false</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Channel is closed:"</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
<span class="k">break</span>
<span class="p">}</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Channel is opened with response:"</span><span class="p">,</span> <span class="n">res</span><span class="p">,</span> <span class="s">"and status:"</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">goRoutineFun</span><span class="p">(</span><span class="n">ch</span> <span class="k">chan</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="m">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span>
<span class="c">/* This is how we concat the iterated loop value with string */</span>
<span class="n">msg</span> <span class="o">:=</span> <span class="s">"Channel message "</span> <span class="o">+</span> <span class="n">strconv</span><span class="o">.</span><span class="n">Itoa</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">ch</span> <span class="o">&lt;-</span> <span class="n">msg</span>
<span class="p">}</span>
<span class="c">/* Using close() function to close the channel */</span>
<span class="nb">close</span><span class="p">(</span><span class="n">ch</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>We will see the following output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Channel is opened with response: Channel message 0 and status: true</span>
<span class="c">// Channel is opened with response: Channel message 1 and status: true</span>
<span class="c">// Channel is opened with response: Channel message 2 and status: true</span>
<span class="c">// Channel is opened with response: Channel message 3 and status: true</span>
<span class="c">// Channel is opened with response: Channel message 4 and status: true</span>
<span class="c">// Channel is closed: false</span></code></pre></figure>
<p><br /></p>
<p>Hope my contribution will help you to understand this topic. Please feel free to reach out to me on twitter <a href="https://twitter.com/rajendraarora16">@rajendraarora16</a> for any queries.</p></content><author><name>Raj Arora</name></author><category term="Channel" /><category term="channel" /><category term="go" /><summary type="html">Channel</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogs.rajendraarora.com/assets/img/go.png" /><media:content medium="image" url="https://blogs.rajendraarora.com/assets/img/go.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Goroutines</title><link href="https://blogs.rajendraarora.com/goroutines/2022/01/16/goroutines.html" rel="alternate" type="text/html" title="Goroutines" /><published>2022-01-16T00:00:00+07:00</published><updated>2022-01-16T00:00:00+07:00</updated><id>https://blogs.rajendraarora.com/goroutines/2022/01/16/goroutines</id><content type="html" xml:base="https://blogs.rajendraarora.com/goroutines/2022/01/16/goroutines.html"><h1 id="goroutines">Goroutines</h1>
<p>A Goroutine is a lightweight thread managed by the Go runtime. A Goroutine is a function or method which executes independently and simultaneously in a connection with any other Goroutine present in our program.
<br /></p>
<p>In other word, Goroutines are functions/methods that run concurrently with other functions/methods.</p>
<h1 id="how-to-create-a-goroutines">How to create a Goroutines?</h1>
<p>We can create our own Goroutine by simply using <code class="language-plaintext highlighter-rouge">go</code> keyword as prefex to the function or method.</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">go</span> <span class="n">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span></code></pre></figure>
<p><code class="language-plaintext highlighter-rouge">function(x, y, z)</code> will start new goroutine runnning.</p>
<p><br /></p>
<p>I will go over basics to a complex example to understand more on examples. So let’s start with basics..</p>
<p><br /></p>
<p>Let’s assume we have this</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="n">displayGreetMsg</span><span class="p">(</span><span class="s">"Welcome"</span><span class="p">)</span>
<span class="n">displayGreetMsg</span><span class="p">(</span><span class="s">"Raj Arora"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">displayGreetMsg</span><span class="p">(</span><span class="n">str</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="m">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>As you can see, it will return the following output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Welcome</span>
<span class="c">// Welcome</span>
<span class="c">// Welcome</span>
<span class="c">// Welcome</span>
<span class="c">// Welcome</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span></code></pre></figure>
<p><br />
Now, if you try to add <code class="language-plaintext highlighter-rouge">go keyword</code> in the following function:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">go</span> <span class="n">displayGreetMsg</span><span class="p">(</span><span class="s">"Welcome"</span><span class="p">)</span> <span class="c">/* Goroutine function */</span>
<span class="n">displayGreetMsg</span><span class="p">(</span><span class="s">"Raj Arora"</span><span class="p">)</span> <span class="c">/* normal function */</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">displayGreetMsg</span><span class="p">(</span><span class="n">str</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="m">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>You will get the following outputs:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span></code></pre></figure>
<p>As you can notice that the above program returns the continous <code class="language-plaintext highlighter-rouge">Raj Arora</code> output but it doesn’t return <code class="language-plaintext highlighter-rouge">Welcome</code> message because we passed <code class="language-plaintext highlighter-rouge">go</code> keyword here with <code class="language-plaintext highlighter-rouge">go displayGreetMsg("Welcome")</code>. The reason because:</p>
<ul>
<li>When a new Goroutine executed, the Goroutine call returns immediately. The control does not wait for the Goroutine to complete their execution just like normal function. So it moves forward to the next line after the Goroutine call and ignore the values.</li>
</ul>
<p><br /></p>
<p>You will be able to understand now why our Goroutine did not run. After the call <code class="language-plaintext highlighter-rouge">go displayGreetMsg("Welcome")</code>, the control returned immediately to the next line of code without waiting for <code class="language-plaintext highlighter-rouge">displayGreetMsg("Welcome")</code> to finish and it printed the <code class="language-plaintext highlighter-rouge">displayGreetMsg("Raj Arora")</code>. And then later the main Goroutine terminated since there is no other code to execute. That’s why we see <code class="language-plaintext highlighter-rouge">Raj Arora</code> as an output five times.</p>
<p><br /></p>
<p>Let’s run this program after some modification so you will get to know what’s happening behind:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"time"</span>
<span class="p">)</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">go</span> <span class="n">displayGreetMsg</span><span class="p">(</span><span class="s">"Welcome"</span><span class="p">)</span> <span class="c">/* Goroutine function */</span>
<span class="n">displayGreetMsg</span><span class="p">(</span><span class="s">"Raj Arora"</span><span class="p">)</span> <span class="c">/* normal function */</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">displayGreetMsg</span><span class="p">(</span><span class="n">str</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="m">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span>
<span class="n">time</span><span class="o">.</span><span class="n">Sleep</span><span class="p">(</span><span class="m">1</span> <span class="o">*</span> <span class="n">time</span><span class="o">.</span><span class="n">Second</span><span class="p">)</span> <span class="c">/* timer */</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>In above program, you may notice that I added a timer before printing. You will now get the following output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Welcome</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span>
<span class="c">// Welcome</span>
<span class="c">// Welcome</span>
<span class="c">// Raj Arora</span>
<span class="c">// Welcome</span>
<span class="c">// Raj Arora</span>
<span class="c">// Raj Arora</span></code></pre></figure>
<p>The reason because:</p>
<ul>
<li>Now the <code class="language-plaintext highlighter-rouge">go displayGreetMsg("Welcome")</code> has enough time to execute before the main Goroutine terminates. It prints <code class="language-plaintext highlighter-rouge">Welcome</code> and then wait for 1 second and print <code class="language-plaintext highlighter-rouge">Raj Arora</code> and then wait for 1 second and so on.</li>
</ul>
<h1 id="one-more-example-in-brief">One more example in brief</h1>
<p>This time I am using this example which I learnt from Udemy, here we have the following program:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"net/http"</span>
<span class="p">)</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="n">links</span> <span class="p">[]</span><span class="kt">string</span>
<span class="n">links</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span>
<span class="s">"https://google.com"</span><span class="p">,</span>
<span class="s">"https://stackoverflow.com/"</span><span class="p">,</span>
<span class="s">"https://yandex.com"</span><span class="p">,</span>
<span class="s">"https://github.com"</span><span class="p">,</span>
<span class="p">}</span>
<span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">link</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">links</span> <span class="p">{</span>
<span class="n">checkLink</span><span class="p">(</span><span class="n">link</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">checkLink</span><span class="p">(</span><span class="n">link</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
<span class="n">_</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">http</span><span class="o">.</span><span class="n">Get</span><span class="p">(</span><span class="n">link</span><span class="p">)</span>
<span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">link</span><span class="p">,</span> <span class="s">"might be down"</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">link</span><span class="p">,</span> <span class="s">"is up!"</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>It will return the following output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// https://google.com is up!</span>
<span class="c">// https://stackoverflow.com/ is up!</span>
<span class="c">// https://yandex.com is up!</span>
<span class="c">// https://github.com is up!</span></code></pre></figure>
<p>Now I am here using <code class="language-plaintext highlighter-rouge">go</code> keyword to run our Goroutine program</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"net/http"</span>
<span class="s">"time"</span>
<span class="p">)</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="n">links</span> <span class="p">[]</span><span class="kt">string</span>
<span class="n">links</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span>
<span class="s">"https://google.com"</span><span class="p">,</span>
<span class="s">"https://stackoverflow.com/"</span><span class="p">,</span>
<span class="s">"https://yandex.com"</span><span class="p">,</span>
<span class="s">"https://github.com"</span><span class="p">,</span>
<span class="p">}</span>
<span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">link</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">links</span> <span class="p">{</span>
<span class="c">/* Groutine "go" keyword
/* which creates a new thread */</span>
<span class="k">go</span> <span class="n">checkLink</span><span class="p">(</span><span class="n">link</span><span class="p">)</span>
<span class="c">/* Timer */</span>
<span class="n">time</span><span class="o">.</span><span class="n">Sleep</span><span class="p">(</span><span class="m">1</span> <span class="o">*</span> <span class="n">time</span><span class="o">.</span><span class="n">Second</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">checkLink</span><span class="p">(</span><span class="n">link</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
<span class="n">_</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">http</span><span class="o">.</span><span class="n">Get</span><span class="p">(</span><span class="n">link</span><span class="p">)</span>
<span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">link</span><span class="p">,</span> <span class="s">"might be down"</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">link</span><span class="p">,</span> <span class="s">"is up!"</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>Now that will put some delays to get your output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// https://google.com is up!</span>
<span class="c">// https://stackoverflow.com/ is up!</span>
<span class="c">// https://yandex.com is up!</span>
<span class="c">// https://github.com is up!</span></code></pre></figure>
<h1 id="some-advantages-of-goroutine-vs-threads">Some advantages of Goroutine vs Threads</h1>
<ul>
<li>We can run more Goroutines on a typical systems than threads.</li>
<li>Goroutines have a faster startup time than threads.</li>
<li>We can write massively concurrent servers without having to resort to evented programming.</li>
<li>Goroutines are built-in primitives to communicate safely between themselves or channels.</li>
<li>On Java we can run 1000’s or tens of 1000’s threads but in Go we can run hundreds of thousands or millions of Goroutines.</li>
</ul>
<p><br /></p>
<p>Hope my contribution will help you to understand this topic. Please feel free to reach out to me on twitter <a href="https://twitter.com/rajendraarora16">@rajendraarora16</a> for any queries.</p></content><author><name>Raj Arora</name></author><category term="Goroutines" /><category term="goroutines" /><category term="go" /><summary type="html">Goroutines</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogs.rajendraarora.com/assets/img/go.png" /><media:content medium="image" url="https://blogs.rajendraarora.com/assets/img/go.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Quicksort algorithm with Go</title><link href="https://blogs.rajendraarora.com/data-structure/2022/01/08/quick-sort-algorithm-go.html" rel="alternate" type="text/html" title="Quicksort algorithm with Go" /><published>2022-01-08T00:00:00+07:00</published><updated>2022-01-08T00:00:00+07:00</updated><id>https://blogs.rajendraarora.com/data-structure/2022/01/08/quick-sort-algorithm-go</id><content type="html" xml:base="https://blogs.rajendraarora.com/data-structure/2022/01/08/quick-sort-algorithm-go.html"><h1 id="quicksort-algorithm-with-go">Quicksort algorithm with Go</h1>
<p>Quicksort algorithm is a “divide and conquer” algorithm. It takes the “pivot” element from the array and partitioning the other element into sub-arrays.</p>
<p>We can select the pivot with many ways but here I am going to select the last element with my example.</p>
<p>In case you want to understand what “divide and conquer” mean here:</p>
<p><img src="/assets/img/divide_conquer.jpg" alt="Divide and conquer screenshot" /></p>
<p>As you can see in screenshot above, it is partitioning the arrays we will be using the same approach here with go to create this algorithm.</p>
<p>Let’s assume we have the following arrays:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="p">[</span><span class="m">38</span><span class="p">,</span> <span class="o">-</span><span class="m">2</span><span class="p">,</span> <span class="o">-</span><span class="m">1</span><span class="p">,</span> <span class="m">20</span><span class="p">,</span> <span class="m">50</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">8</span><span class="p">,</span> <span class="m">10</span><span class="p">]</span></code></pre></figure>
<p>Here is our approach how we are going to create our pseudo code with the following way:</p>
<p><img src="/assets/img/partition_quick_sort.jpg" alt="examplanation how it works" /></p>
<p><br />
<b>Pseudo code for our recursive algorithm:</b></p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="n">quickSort</span><span class="p">(</span><span class="n">arr</span> <span class="p">[],</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">low</span> <span class="o">&lt;</span> <span class="n">high</span> <span class="p">{</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">partition</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="n">quickSort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">pi</span><span class="o">-</span><span class="m">1</span><span class="p">)</span> <span class="c">// Before partition</span>
<span class="n">quickSort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">pi</span><span class="o">+</span><span class="m">1</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span> <span class="c">// After partition</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p><br /></p>
<p>Let’s create a quick program using golang to show how we’re implementing this Pseudo code.</p>
<p>Firstly let’s create a simple logic with arrays:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="p">(</span>
<span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span>
<span class="n">low</span> <span class="kt">int</span>
<span class="n">high</span> <span class="kt">int</span>
<span class="p">)</span>
<span class="n">arr</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">38</span><span class="p">,</span> <span class="o">-</span><span class="m">2</span><span class="p">,</span> <span class="o">-</span><span class="m">1</span><span class="p">,</span> <span class="m">20</span><span class="p">,</span> <span class="m">50</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">8</span><span class="p">,</span> <span class="m">10</span><span class="p">}</span>
<span class="n">low</span> <span class="o">=</span> <span class="m">0</span>
<span class="n">high</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">low</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">high</span><span class="p">)</span>
<span class="p">}</span>
<span class="c">// Output</span>
<span class="c">// [38 -2 -1 20 50 7 8 10]</span>
<span class="c">// 0</span>
<span class="c">// 7</span></code></pre></figure>
<p><br />
As you can see, we just created a simple logic to make sure we are good at printing, now let’s implement quick sort function with our main program:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="p">(</span>
<span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span>
<span class="n">low</span> <span class="kt">int</span>
<span class="n">high</span> <span class="kt">int</span>
<span class="p">)</span>
<span class="n">arr</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">38</span><span class="p">,</span> <span class="o">-</span><span class="m">2</span><span class="p">,</span> <span class="o">-</span><span class="m">1</span><span class="p">,</span> <span class="m">20</span><span class="p">,</span> <span class="m">50</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">8</span><span class="p">,</span> <span class="m">10</span><span class="p">}</span>
<span class="n">low</span> <span class="o">=</span> <span class="m">0</span>
<span class="n">high</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span>
<span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p><br />
Let’s have a quick look at <code class="language-plaintext highlighter-rouge">quicksort</code> function logic</p>
<p><br /></p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">func</span> <span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span><span class="p">,</span> <span class="n">low</span> <span class="kt">int</span><span class="p">,</span> <span class="n">high</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">low</span> <span class="o">&lt;</span> <span class="n">high</span> <span class="p">{</span>
<span class="c">// Partitioning..</span>
<span class="n">pi</span> <span class="o">:=</span> <span class="n">partition</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="c">// Before partitioning..</span>
<span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">pi</span><span class="o">-</span><span class="m">1</span><span class="p">)</span>
<span class="c">// After partitioning..</span>
<span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">pi</span><span class="o">+</span><span class="m">1</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p><br />
Let’s create a logic for our partitioning function</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="c">// Here, it's our return function</span>
<span class="k">func</span> <span class="n">partition</span><span class="p">(</span><span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span><span class="p">,</span> <span class="n">low</span> <span class="kt">int</span><span class="p">,</span> <span class="n">high</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span> <span class="p">{</span>
<span class="c">// Pivot</span>
<span class="n">pivot</span> <span class="o">:=</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">]</span>
<span class="c">// Index</span>
<span class="n">index</span> <span class="o">:=</span> <span class="n">low</span> <span class="o">-</span> <span class="m">1</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="n">low</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">high</span><span class="o">-</span><span class="m">1</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">pivot</span> <span class="p">{</span>
<span class="n">index</span><span class="o">++</span>
<span class="c">// Swapping</span>
<span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c">// Swapping</span>
<span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="o">+</span><span class="m">1</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">]</span> <span class="o">=</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="o">+</span><span class="m">1</span><span class="p">]</span>
<span class="k">return</span> <span class="n">index</span> <span class="o">+</span> <span class="m">1</span>
<span class="p">}</span></code></pre></figure>
<p><br /></p>
<p>Notice here two things which I made a mistake when I was implementing the logic:</p>
<ul>
<li>Inside <code class="language-plaintext highlighter-rouge">partition</code> function, our loop starts with <code class="language-plaintext highlighter-rouge">low</code> since our quicksort function is recursive. So we don’t wanna mess up with arrays index here.</li>
<li>Swapping, since go returns multiple return value so swapping is much easier with go. We don’t need to define a separate function or <code class="language-plaintext highlighter-rouge">temp</code> declaration.</li>
</ul>
<p><br />
Hopefully, we should get the following output:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="p">[</span><span class="o">-</span><span class="m">2</span> <span class="o">-</span><span class="m">1</span> <span class="m">7</span> <span class="m">8</span> <span class="m">10</span> <span class="m">20</span> <span class="m">38</span> <span class="m">50</span><span class="p">]</span></code></pre></figure>
<p><br />
In case, you want to see the complete program so here it is:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="p">(</span>
<span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span>
<span class="n">low</span> <span class="kt">int</span>
<span class="n">high</span> <span class="kt">int</span>
<span class="p">)</span>
<span class="n">arr</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">38</span><span class="p">,</span> <span class="o">-</span><span class="m">2</span><span class="p">,</span> <span class="o">-</span><span class="m">1</span><span class="p">,</span> <span class="m">20</span><span class="p">,</span> <span class="m">50</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">8</span><span class="p">,</span> <span class="m">10</span><span class="p">}</span>
<span class="n">low</span> <span class="o">=</span> <span class="m">0</span>
<span class="n">high</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span>
<span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">func</span> <span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span><span class="p">,</span> <span class="n">low</span> <span class="kt">int</span><span class="p">,</span> <span class="n">high</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">low</span> <span class="o">&lt;</span> <span class="n">high</span> <span class="p">{</span>
<span class="c">// Partitioning..</span>
<span class="n">pi</span> <span class="o">:=</span> <span class="n">partition</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="c">// Before partitioning..</span>
<span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">pi</span><span class="o">-</span><span class="m">1</span><span class="p">)</span>
<span class="c">// After partitioning..</span>
<span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">pi</span><span class="o">+</span><span class="m">1</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c">// Here, it's our return function</span>
<span class="k">func</span> <span class="n">partition</span><span class="p">(</span><span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span><span class="p">,</span> <span class="n">low</span> <span class="kt">int</span><span class="p">,</span> <span class="n">high</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span> <span class="p">{</span>
<span class="c">// Pivot</span>
<span class="n">pivot</span> <span class="o">:=</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">]</span>
<span class="c">// Index</span>
<span class="n">index</span> <span class="o">:=</span> <span class="n">low</span> <span class="o">-</span> <span class="m">1</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="n">low</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">high</span><span class="o">-</span><span class="m">1</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">pivot</span> <span class="p">{</span>
<span class="n">index</span><span class="o">++</span>
<span class="c">// Swapping</span>
<span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c">// Swapping</span>
<span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="o">+</span><span class="m">1</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">]</span> <span class="o">=</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="o">+</span><span class="m">1</span><span class="p">]</span>
<span class="k">return</span> <span class="n">index</span> <span class="o">+</span> <span class="m">1</span>
<span class="p">}</span></code></pre></figure>
<h1 id="time-complexity">Time complexity</h1>
<ul>
<li>Worst Time Complexity : <code class="language-plaintext highlighter-rouge">O(n^2)</code></li>
<li>Worst case can happen when array is already sorted</li>
<li>Best time complexity : <code class="language-plaintext highlighter-rouge">O(nlogn)</code></li>
<li>Average Time Complexity : <code class="language-plaintext highlighter-rouge">O(nlogn)</code></li>
</ul>
<p><br /></p>
<h1 id="implemeting-receiver-function">Implemeting receiver function</h1>
<p>Let’s try some way in go using receiver function, as we know that go has some receiver functional way. So, let’s modify our function and implment some receiver way.
<br /></p>
<p>We need to declare first the type here:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">type</span> <span class="n">quickSortAlgorithm</span> <span class="p">[]</span><span class="kt">int</span></code></pre></figure>
<p><br /></p>
<p>After adding the type, we can declare our function something like with receiver function:</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">type</span> <span class="n">quickSortAlgorithm</span> <span class="p">[]</span><span class="kt">int</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="p">(</span>
<span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span>
<span class="n">low</span> <span class="kt">int</span>
<span class="n">high</span> <span class="kt">int</span>
<span class="p">)</span>
<span class="n">arr</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">38</span><span class="p">,</span> <span class="o">-</span><span class="m">2</span><span class="p">,</span> <span class="o">-</span><span class="m">1</span><span class="p">,</span> <span class="m">20</span><span class="p">,</span> <span class="m">50</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">8</span><span class="p">,</span> <span class="m">10</span><span class="p">}</span>
<span class="n">low</span> <span class="o">=</span> <span class="m">0</span>
<span class="n">high</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span>
<span class="c">// Notice here we need to declare the type creation here..</span>
<span class="n">quicksortAlgo</span> <span class="o">:=</span> <span class="n">quickSortAlgorithm</span><span class="p">{}</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Before quicksort"</span><span class="p">)</span>
<span class="n">quicksortAlgo</span><span class="o">.</span><span class="n">printArray</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="c">// Using this type, we can call our function like this..</span>
<span class="n">quicksortAlgo</span><span class="o">.</span><span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"After quicksort"</span><span class="p">)</span>
<span class="n">quicksortAlgo</span><span class="o">.</span><span class="n">printArray</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p><br />
As you can see here, we have called our function using receiver <code class="language-plaintext highlighter-rouge">quicksortAlgo.quicksort(arr, low, high)</code>. Also we are printing the array value from the receiver function <code class="language-plaintext highlighter-rouge">quicksortAlgo.printArray(arr)</code></p>
<p>Alright, talk is enough. Let me show you the full code ;)</p>
<figure class="highlight"><pre><code class="language-go" data-lang="go"><span class="k">package</span> <span class="n">main</span>
<span class="k">import</span> <span class="s">"fmt"</span>
<span class="k">type</span> <span class="n">quickSortAlgorithm</span> <span class="p">[]</span><span class="kt">int</span>
<span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">var</span> <span class="p">(</span>
<span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span>
<span class="n">low</span> <span class="kt">int</span>
<span class="n">high</span> <span class="kt">int</span>
<span class="p">)</span>
<span class="n">arr</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">38</span><span class="p">,</span> <span class="o">-</span><span class="m">2</span><span class="p">,</span> <span class="o">-</span><span class="m">1</span><span class="p">,</span> <span class="m">20</span><span class="p">,</span> <span class="m">50</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">8</span><span class="p">,</span> <span class="m">10</span><span class="p">}</span>
<span class="n">low</span> <span class="o">=</span> <span class="m">0</span>
<span class="n">high</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="o">-</span> <span class="m">1</span>
<span class="n">quicksortAlgo</span> <span class="o">:=</span> <span class="n">quickSortAlgorithm</span><span class="p">{}</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Before quicksort"</span><span class="p">)</span>
<span class="n">quicksortAlgo</span><span class="o">.</span><span class="n">printArray</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="n">quicksortAlgo</span><span class="o">.</span><span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"After quicksort"</span><span class="p">)</span>
<span class="n">quicksortAlgo</span><span class="o">.</span><span class="n">printArray</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">func</span> <span class="p">(</span><span class="n">qa</span> <span class="n">quickSortAlgorithm</span><span class="p">)</span> <span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span><span class="p">,</span> <span class="n">low</span> <span class="kt">int</span><span class="p">,</span> <span class="n">high</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">low</span> <span class="o">&lt;</span> <span class="n">high</span> <span class="p">{</span>
<span class="c">// Partitioning..</span>
<span class="n">pi</span> <span class="o">:=</span> <span class="n">partition</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="c">// Before partitioning..</span>
<span class="n">qa</span><span class="o">.</span><span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">pi</span><span class="o">-</span><span class="m">1</span><span class="p">)</span>
<span class="c">// After partitioning..</span>
<span class="n">qa</span><span class="o">.</span><span class="n">quicksort</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">pi</span><span class="o">+</span><span class="m">1</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">func</span> <span class="p">(</span><span class="n">qa</span> <span class="n">quickSortAlgorithm</span><span class="p">)</span> <span class="n">printArray</span><span class="p">(</span><span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span>
<span class="p">}</span>
<span class="c">// Here, it's our return function</span>
<span class="k">func</span> <span class="n">partition</span><span class="p">(</span><span class="n">arr</span> <span class="p">[]</span><span class="kt">int</span><span class="p">,</span> <span class="n">low</span> <span class="kt">int</span><span class="p">,</span> <span class="n">high</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span> <span class="p">{</span>
<span class="c">// Pivot</span>
<span class="n">pivot</span> <span class="o">:=</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">]</span>
<span class="c">// Index</span>
<span class="n">index</span> <span class="o">:=</span> <span class="n">low</span> <span class="o">-</span> <span class="m">1</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="n">low</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">high</span><span class="o">-</span><span class="m">1</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span>
<span class="k">if</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">pivot</span> <span class="p">{</span>
<span class="n">index</span><span class="o">++</span>
<span class="c">// Swapping</span>
<span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c">// Swapping</span>
<span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="o">+</span><span class="m">1</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">]</span> <span class="o">=</span> <span class="n">arr</span><span class="p">[</span><span class="n">high</span><span class="p">],</span> <span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="o">+</span><span class="m">1</span><span class="p">]</span>
<span class="k">return</span> <span class="n">index</span> <span class="o">+</span> <span class="m">1</span>
<span class="p">}</span></code></pre></figure>
<p><br />
Your feedback is appreciated, please feel free to reach out to me on @rajendraarora16 for any suggestions.
Would be happy to say 👋</p>
<p><br />
<br /></p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
this.page.url = 'https://blogs.rajendraarora.com/data-structure/2022/01/08/quick-sort-algorithm-go.html';
// Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'data-structure/2022/01/08/quick-sort-algorithm-go.html';
};
(function() {
var d = document, s = d.createElement('script');
s.src = 'https://https-blogs-rajendraarora-com.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<script id="dsq-count-scr" src="//https-blogs-rajendraarora-com-1.disqus.com/count.js" async=""></script></content><author><name>Raj Arora</name></author><category term="data-structure" /><category term="data-structure" /><category term="go" /><summary type="html">Quicksort algorithm with Go</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogs.rajendraarora.com/assets/img/partition_quick_sort.jpg" /><media:content medium="image" url="https://blogs.rajendraarora.com/assets/img/partition_quick_sort.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Deno</title><link href="https://blogs.rajendraarora.com/deno/2021/08/23/deno.html" rel="alternate" type="text/html" title="Deno" /><published>2021-08-23T00:00:00+07:00</published><updated>2021-08-23T00:00:00+07:00</updated><id>https://blogs.rajendraarora.com/deno/2021/08/23/deno</id><content type="html" xml:base="https://blogs.rajendraarora.com/deno/2021/08/23/deno.html"><p><br /></p>
<p algin="center">
<img src="/assets/img/deno_icon.png" data-canonical-src="/assets/img/deno_icon.png" style="height: 50px;width: 50px;" />
</p>
<h1 id="what-is-deno">What is Deno?</h1>
<p>As per the <a href="https://github.com/denoland/deno">definition</a>, deno is a simple, modern and secure runtime for Javascript and Typescript which is built on V8, Rust and <a href="https://tokio.rs/">Tokio</a>.</p>
<p>It has recently been announced the <a href="https://deno.com/blog/deno-on-mdn">compatibility on MDN</a>.</p>
<h1 id="feature">Feature</h1>
<ul>
<li>Supports Typescript</li>
<li>Ships a single executable</li>
<li>Has built-in utilities like a code formatter (<code class="language-plaintext highlighter-rouge">deno fmt</code>), a linter (<code class="language-plaintext highlighter-rouge">deno lint</code>), and a test runner (<code class="language-plaintext highlighter-rouge">deno test</code>).</li>
<li>Can bundle scripts into a single JavaScript file.</li>
<li>Set of reviewed standard modules that are guaranteed to work with <a href="https://deno.land/std">Deno</a>.</li>
</ul>
<h1 id="how-to-download-deno">How to download Deno?</h1>
<p>You can download from the <a href="https://deno.land/">official website</a>.</p>
<h1 id="hello-world-with-deno">Hello World with Deno!</h1>
<p>Deno runs on Typescript, no additional config tools like webpack or bundle is required to compile. We can use both Javascript or Typescript with Deno.</p>
<p>I will be using Typescript to run a simple program <br />Command: <code class="language-plaintext highlighter-rouge">deno run hello-world.ts</code>:</p>
<figure class="highlight"><pre><code class="language-typescript" data-lang="typescript"><span class="cm">/**
* Hello-world.ts
*/</span>
<span class="kd">function</span> <span class="nx">capitalize</span><span class="p">(</span><span class="nx">word</span><span class="p">:</span> <span class="kr">string</span><span class="p">):</span> <span class="kr">string</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">word</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">).</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">word</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">hello</span><span class="p">(</span><span class="nx">name</span><span class="p">:</span> <span class="kr">string</span><span class="p">):</span> <span class="kr">string</span> <span class="p">{</span>
<span class="k">return</span> <span class="s2">`Hello </span><span class="p">${</span><span class="nx">capitalize</span><span class="p">(</span><span class="nx">name</span><span class="p">)}</span><span class="s2">`</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">hello</span><span class="p">(</span><span class="dl">'</span><span class="s1">raj</span><span class="dl">'</span><span class="p">));</span>
<span class="cm">/*
Output:
Hello Raj
*/</span></code></pre></figure>
<p><br /></p>
<p>Let’s see more good examples with <code class="language-plaintext highlighter-rouge">import</code> modules <br />Command: <code class="language-plaintext highlighter-rouge">deno run import-example.ts</code>:
<br /></p>
<figure class="highlight"><pre><code class="language-typescript" data-lang="typescript"><span class="cm">/**
* import modules
*/</span>
<span class="k">import</span> <span class="p">{</span> <span class="nx">add</span><span class="p">,</span> <span class="nx">multiply</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">https://x.nest.land/ramda@0.27.0/source/index.js</span><span class="dl">'</span><span class="p">;</span>
<span class="kd">function</span> <span class="nx">totalCost</span><span class="p">(</span><span class="nx">outbound</span><span class="p">:</span> <span class="kr">number</span><span class="p">,</span> <span class="nx">inbound</span><span class="p">:</span> <span class="kr">number</span><span class="p">,</span> <span class="nx">tax</span><span class="p">:</span> <span class="kr">number</span><span class="p">):</span> <span class="kr">number</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">multiply</span><span class="p">(</span><span class="nx">add</span><span class="p">(</span><span class="nx">outbound</span><span class="p">,</span> <span class="nx">inbound</span><span class="p">),</span> <span class="nx">tax</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">totalCost</span><span class="p">(</span><span class="mi">43</span><span class="p">,</span> <span class="mf">22.5</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">));</span>
<span class="cm">/**
Output:
98.25
*/</span></code></pre></figure>
<p><br /></p>
<h1 id="comparison-to-nodejs">Comparison to Node.js</h1>
<ul>
<li>Deno doesn’t use <code class="language-plaintext highlighter-rouge">npm</code>, it uses URLs or file paths.</li>
<li>Deno doesn’t use <code class="language-plaintext highlighter-rouge">package.json</code>.</li>
<li>All async actions in Deno return a promise, thus Deno provides different APIs than node.</li>
<li>Deno requires explicit permission for file, network or environment access which makes it more secure.</li>
<li>Deno dies in uncaught errors.</li>
<li>Deno uses “ES modules” and doesn’t support <code class="language-plaintext highlighter-rouge">require()</code> and third party modules are imported via URLs with above import module example:<br /></li>
</ul>
<figure class="highlight"><pre><code class="language-typescript" data-lang="typescript"><span class="k">import</span> <span class="p">{</span> <span class="nx">add</span><span class="p">,</span> <span class="nx">multiply</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">https://x.nest.land/ramda@0.27.0/source/index.js</span><span class="dl">'</span><span class="p">;</span></code></pre></figure>
<p><br /></p>
<p>More information can be found in <a href="https://deno.land/manual">manual doc</a>.</p>
<p><br />
<br /></p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
this.page.url = 'https://blogs.rajendraarora.com/deno/2021/08/23/deno.html';
// Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'deno/2021/08/23/deno.html';
};
(function() {
var d = document, s = d.createElement('script');
s.src = 'https://https-blogs-rajendraarora-com.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<script id="dsq-count-scr" src="//https-blogs-rajendraarora-com-1.disqus.com/count.js" async=""></script></content><author><name>Raj Arora</name></author><category term="deno" /><category term="deno" /><category term="typescript" /><category term="javascript" /><summary type="html"></summary></entry><entry><title type="html">How to deploy github repositories with Travis CI?</title><link href="https://blogs.rajendraarora.com/travis/2021/08/23/how-to-deploy-github-repo-with-travis-ci.html" rel="alternate" type="text/html" title="How to deploy github repositories with Travis CI?" /><published>2021-08-23T00:00:00+07:00</published><updated>2021-08-23T00:00:00+07:00</updated><id>https://blogs.rajendraarora.com/travis/2021/08/23/how-to-deploy-github-repo-with-travis-ci</id><content type="html" xml:base="https://blogs.rajendraarora.com/travis/2021/08/23/how-to-deploy-github-repo-with-travis-ci.html"><p>Github is a nice solution which can be integrated with Travis CI to host website, building solutions/container etc. I am using the same solution to host my blogs using jekyll to host static file via github pages.</p>
<p>I assume, you have github public repo. I will be taking my github blog example to deploy my blog repository with Travis CI.</p>
<p>So, let’s begin with the following steps:</p>
<h1 id="1-add-travisyml-file-in-your-github-repo">1. Add <code class="language-plaintext highlighter-rouge">.travis.yml</code> file in your github repo</h1>
<p>We need to add travis build configuration configuration to generate the build, so add <code class="language-plaintext highlighter-rouge">.travis.yml</code> in your root directory. Below I am adding with my configuration:</p>
<figure class="highlight"><pre><code class="language-yml" data-lang="yml"><span class="na">language</span><span class="pi">:</span> <span class="s">ruby</span>
<span class="na">cache</span><span class="pi">:</span> <span class="s">bundler</span>
<span class="na">branches</span><span class="pi">:</span>
<span class="na">only</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">main</span>
<span class="na">before_install</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">gem install bundler -v 2.0.1</span>
<span class="na">script</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">chmod +x ./script/cibuild</span>
<span class="pi">-</span> <span class="s">JEKYLL_ENV=production bundle exec jekyll build --destination site</span>
<span class="na">deploy</span><span class="pi">:</span>
<span class="na">repo</span><span class="pi">:</span> <span class="s">rajendraarora/blogs.github.io</span>
<span class="na">provider</span><span class="pi">:</span> <span class="s">pages</span>
<span class="na">local-dir</span><span class="pi">:</span> <span class="s">./site</span>
<span class="na">target-branch</span><span class="pi">:</span> <span class="s">master</span>
<span class="na">email</span><span class="pi">:</span> <span class="s">deploy@travis-ci.org</span>
<span class="na">name</span><span class="pi">:</span> <span class="s">Deployment Bot</span>
<span class="na">skip-cleanup</span><span class="pi">:</span> <span class="no">false</span>
<span class="na">github-token</span><span class="pi">:</span> <span class="s">$GITHUB_TOKEN</span>
<span class="na">keep-history</span><span class="pi">:</span> <span class="no">true</span>
<span class="na">on</span><span class="pi">:</span>
<span class="na">branch</span><span class="pi">:</span> <span class="s">master</span></code></pre></figure>
<h1 id="2-get-your-github-token">2. Get your Github Token</h1>
<p>. Go to this <a href="https://github.com/settings/tokens/new">page</a> and generate your token and keep it safe.</p>
<p>. Add your <code class="language-plaintext highlighter-rouge">GITHUB_TOKEN</code> environment below via go to setting options:</p>
<p algin="center">
<img src="/assets/img/token-setting-1.png" data-canonical-src="/assets/img/token-setting-1.png" />
</p>
<p>. Add your token below:</p>
<p algin="center">
<img src="/assets/img/token-setting-2.png" data-canonical-src="/assets/img/token-setting-2.png" />
</p>
<h1 id="3-trigger-your-build">3. Trigger your Build</h1>
<p>. Go to trigger setting options</p>
<p algin="center">
<img src="/assets/img/trigger-setting-1.png" data-canonical-src="/assets/img/trigger-setting-1.png" s="" />
</p>
<p>. Select trigger button</p>
<p algin="center">
<img src="/assets/img/trigger-setting-2.png" data-canonical-src="/assets/img/trigger-setting-2.png" height="500" />