-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1389 lines (918 loc) · 32.7 KB
/
index.html
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
<!doctype html><html lang="en">
<head>
<meta name="generator" content="Hugo 0.30.2" />
<meta charset="utf-8">
<title>A motivation to Go(lang)</title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.6.0/css/reveal.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.6.0/css/theme/moon.css" id="theme">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/reveal.js\/3.6.0/css/print/pdf.css' : 'https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/reveal.js\/3.6.0/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2 id="a-motivation-to-go-lang">A motivation to Go(lang)</h2>
<h3 id="daniel-mejía-r">Daniel Mejía R.</h3>
<h3 id="http-daniel-m-github-io-http-daniel-m-github-io"><a href="http://daniel-m.github.io"><code>http://daniel-m.github.io</code></a></h3>
<h6 id="august-2018">August 2018</h6>
</section>
<section>
<h3 id="what-is-go-lang">What is Go(lang)?</h3>
<p>As <a href="https://golang.org">https://golang.org</a> states,</p>
<blockquote>
<p><em><a href="https://golang.org/">Go</a> is an open source programming language that makes
it easy to build simple, reliable, and efficient software.</em></p>
</blockquote>
<ul>
<li>Developed at Google as early as 2009 by Robert Griesemer, Rob Pike,
and Ken Thompson.<br /></li>
<li>Version 1.0 Released at 2012.<br /></li>
</ul>
</section>
<section>
<section><h4 id="notable-pros">Notable Pros</h4>
<ul>
<li>Exploit multicore and networked machines through concurrency.<br /></li>
<li>Compiles way too fast.</li>
<li>Garbage collected at compilation time.</li>
<li>Strongly typed.</li>
<li>Easy management of development environment (Workspace).</li>
<li>git oriented development (packages automatically downloaded from remote repos).</li>
</ul>
</section>
<section>
<ul>
<li>Native code formatting tool (linter).<br /></li>
<li>Package documentation generated at ease with native tools and self.
hosted (<a href="http://godoc.org">godoc.org</a>).<br /></li>
<li>Package testing with native tools.<br /></li>
<li>Race condition analyzer.<br /></li>
<li>Multi-platform (Operating systems).<br /></li>
<li>Multi-architecture (Hardware compatibility).</li>
<li>Cross compiling as easy as changing an environment variable.</li>
</ul>
</section>
<section>
<ul>
<li>Monolithic executables.<br /></li>
<li>Runtime reflection (mutable executables at runtime).<br /></li>
<li>Intensively documented, standardized.<br /></li>
<li>Easily shareable code snippets that execute on the cloud (<a href="http://play.golang.org">http://play.golang.org</a>).</li>
<li>Great community.</li>
</ul>
<p><strong>And lots more…</strong></p>
</section>
</section>
<section>
<section><h4 id="cons-of-some-sort">Cons of some sort</h4>
<ul>
<li>Non Object Oriented Programming (though it has structures and interfaces).<br /></li>
<li>Non functional programming (though it can be emulated through functions
or closures).<br /></li>
<li>Monolithic executables (lacks the concept of shared library to link against it).<br /></li>
</ul>
</section>
<section>
<ul>
<li>Non centralized package server.<br /></li>
<li>Depends on external services to provide packages (git forges -> <em>expose security vulnerabilities!)</em>.<br /></li>
<li>Dependency management still in development (early implementations of <code>vgo</code>
to be official within <code>go1.12</code>).<br /></li>
</ul>
</section>
</section>
<section>
<section><h3 id="why-go-personal-perspective">Why Go?: personal perspective</h3>
<p>Go helped me with,</p>
<ul>
<li>Adopt proper style: hard to implement <em>brute-force</em> programming.<br /></li>
<li>First steps with web-dev.<br /></li>
<li>Lots of API libraries to experiment with.<br /></li>
<li>Brag about programming with the Google’s language.<br /></li>
</ul>
</section>
</section>
<section>
<section><h3 id="first-steps">First steps</h3>
<h4 id="installing">Installing</h4>
<p>Go binary install is recommended to have the latest go version
from <a href="https://golang.org/dl/">https://golang.org/dl/</a> following instructions.</p>
</section>
<section>
<h4 id="the-workspace">The Workspace</h4>
<p>Go relies in the concept of <em>workspace</em>. The environment variable <code>GOPATH</code> must
be set to an existing directory which will hold all the go packages,
binaries and precompiled packages</p>
</section>
<section>
<h4 id="sample-workspace">Sample workspace</h4>
<pre><code>bin/
hello # command executable
outyet # command executable
pkg/
linux_amd64/
github.com/golang/example/
stringutil.a # package object
src/
github.com/golang/example/
.git/ # Git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
stringutil/
reverse.go # package source
reverse_test.go # test source
golang.org/x/image/
.git/ # Git repository metadata
bmp/
reader.go # package source
writer.go # package source
... (many more repositories and packages omitted) ...
</code></pre>
</section>
<section>
<h4 id="making-the-binaries-available">Making the binaries available</h4>
<p>Put compiled go packages as console commands is as easy as adding <code>GOPATH</code> to
the <code>PATH</code>,</p>
<pre><code>$ export PATH=$GOPATH/bin:$PATH
</code></pre>
</section>
<section>
<h4 id="installing-go-packages">Installing go packages</h4>
<p>To include a package within a workspace,</p>
<pre><code>$ go get github.com/user/hello
</code></pre>
<p>Installing a go package goes as,</p>
<pre><code>$ go install github.com/user/hello
</code></pre>
<p>Which first <<code>go get</code>> and then compiles the binaries into <code>$GOPATH/bin</code></p>
</section>
</section>
<section>
<h3 id="anatomy-of-a-go-source-file">Anatomy of a go source file</h3>
<p>Open <a href="https://play.golang.org/">https://play.golang.org/</a> in your browsers.
Throughout the presentations, links to the playground will be provided to
see live performance</p>
<pre><code>package main // Every go package starts with the word "package"
// Here come the package imports
import (
"fmt"
)
// Here comes the main function
func main() { // <- Opening braces must be here
// We are using the method "Println"
// included within the package "fmt"
fmt.Println("Hello, playground")
}
</code></pre>
</section>
<section>
<section><h3 id="variable-declaration">Variable declaration</h3>
<p><a href="https://play.golang.org/p/HYUpJDXWte0">https://play.golang.org/p/HYUpJDXWte0</a></p>
<pre><code>package main
import (
"fmt"
"math/rand"
)
func main() {
// Variable declaration follows the rule
// var name type [= initial_value]
var greeting string = "Hello, 世界"
fmt.Println(greeting)
fmt.Println("My favorite number is", rand.Intn(10))
}
</code></pre>
</section>
<section>
<h4 id="short-variable-declarations">Short Variable Declarations</h4>
<pre><code>package main
import (
"fmt"
"math/rand"
)
func main() {
// Variables can be assigned by
// the ":=" operator which infers the
// adequate types
greeting := "Hello, 世界"
fmt.Println(greeting)
fmt.Println("My favorite number is", rand.Intn(10))
}
</code></pre>
</section>
</section>
<section>
<h3 id="go-basic-types">Go Basic types</h3>
<pre><code>bool // true, false
string // "Hello World!"
int int8 int16 int32 int64 // Several flavors of integers
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8. Size guaranteed
rune // alias for int32
// represents a Unicode code point. Size guaranteed
float32 float64 // single and double precision
complex64 complex128 // Yes!, complex numbers natively supported
</code></pre>
</section>
<section>
<section><h3 id="functions">Functions</h3>
<p><a href="https://play.golang.org/p/Fy3IxY-LLdI">https://play.golang.org/p/Fy3IxY-LLdI</a></p>
<pre><code>package main
import (
"fmt"
"math/rand"
)
// Greeter returns a greeting string
func Greeter() string {
return "Hello, 世界"
}
// Number returns a random int seeded with seed
func Number(seed int) int {
return rand.Intn(seed)
}
func main() {
fmt.Println(Greeter())
fmt.Println("My favorite number is", Number(10))
}
</code></pre>
</section>
<section>
<h4 id="functions-with-multiple-return">Functions with multiple return</h4>
<p><a href="https://play.golang.org/p/yUPdKi-zeHL">https://play.golang.org/p/yUPdKi-zeHL</a></p>
<pre><code>package main
import (
"fmt"
"math/rand"
)
// GreeterNumber returns a greeting string
// and a number.
// Yes, go supports multiple return functions
func GreeterNumber(seed int) (string, int) {
return "Hello, 世界", rand.Intn(seed)
}
func main() {
// Variables can be assigned by
// the ":=" operator which infers the
// adequate types
greet, num := GreeterNumber(10)
fmt.Println(greet)
fmt.Println("My favorite number is", num)
}
</code></pre>
</section>
<section>
<h3 id="defer">Defer</h3>
<p>“Defers” function call</p>
<p><a href="https://play.golang.org/p/TD6SZBXwn_r">https://play.golang.org/p/TD6SZBXwn_r</a></p>
<pre><code>package main
import "fmt"
func main() {
defer fmt.Println("print last...")
defer fmt.Println("You didn't expected me to")
defer fmt.Println("I bet,")
fmt.Println("Hey there!")
fmt.Println("Ready to see something amazing?")
}
</code></pre>
</section>
</section>
<section>
<section><h3 id="go-conditionals">Go conditionals</h3>
<h4 id="if-statement">If statement</h4>
<p><a href="https://play.golang.org/p/g5TY4wb916e">https://play.golang.org/p/g5TY4wb916e</a></p>
<pre><code>package main
import (
"fmt"
)
func abs(x float64) float64 {
if x < 0 {
return abs(-x)
}
return x
}
func main() {
fmt.Println(abs(2), abs(-4))
}
</code></pre>
</section>
<section>
<h4 id="if-else">If else</h4>
<p><a href="https://play.golang.org/p/zqAHNoFiUZO">https://play.golang.org/p/zqAHNoFiUZO</a></p>
<pre><code>package main
import (
"fmt"
)
func abs(x float64) float64 {
if x < 0 {
return abs(-x)
} else {
return x
}
}
func main() {
fmt.Println(abs(2), abs(-4))
}
</code></pre>
</section>
<section>
<h4 id="if-else-if-else">If else-if else</h4>
<pre><code>
func abs(x float64) float64 {
if x < 0 {
return abs(-x)
} else if x == 0 {
return 0
} else {
return x
}
}
</code></pre>
</section>
</section>
<section>
<section><h3 id="go-loops">Go Loops</h3>
<p>A single loop structure rules them all,</p>
<p><a href="https://play.golang.org/p/HwJ5nXetGDV">https://play.golang.org/p/HwJ5nXetGDV</a></p>
<pre><code>// single condition for looping
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}
</code></pre>
</section>
<section>
<h3 id="go-loops-1">Go Loops</h3>
<p>A single loop structure rules them all,</p>
<p><a href="https://play.golang.org/p/HwJ5nXetGDV">https://play.golang.org/p/HwJ5nXetGDV</a></p>
<pre><code>// A classic C-like initial/condition/after `for` loop.
for j := 7; j <= 9; j++ {
fmt.Println(j)
}
</code></pre>
</section>
<section>
<h3 id="go-loops-2">Go Loops</h3>
<p>A single loop structure rules them all,</p>
<p><a href="https://play.golang.org/p/HwJ5nXetGDV">https://play.golang.org/p/HwJ5nXetGDV</a></p>
<pre><code>// `for` without a condition will loop repeatedly
// until you `break` out of the loop or `return` from
// the enclosing function.
for {
fmt.Println("loop")
break
}
</code></pre>
</section>
<section>
<h3 id="go-loops-3">Go Loops</h3>
<p>A single loop structure rules them all,</p>
<p><a href="https://play.golang.org/p/HwJ5nXetGDV">https://play.golang.org/p/HwJ5nXetGDV</a></p>
<pre><code>// You can also `continue` to the next iteration of
// the loop.
for n := 0; n <= 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
</code></pre>
</section>
</section>
<section>
<h3 id="arrays">Arrays</h3>
<p><a href="https://play.golang.org/p/A1oG39orUpZ">https://play.golang.org/p/A1oG39orUpZ</a></p>
<ul>
<li>Arrays are of fixed size.<br /></li>
<li>The type <code>[n]T</code> is an array of n values of type <code>T</code>.<br /></li>
</ul>
<pre><code>func main() {
var a [2]string
a[0] = "Hello"
a[1] = "World"
fmt.Println(a[0], a[1])
fmt.Println(a)
// Or the short declaration
// also known as "array literal"
primes := [6]int{2, 3, 5, 7, 11, 13}
fmt.Println(primes)
}
</code></pre>
</section>
<section>
<section><h3 id="slices">Slices</h3>
<ul>
<li>A slice is a dynamically-sized.<br /></li>
<li>The type <code>[]T</code> is a slice with elements of type <code>T</code>.<br /></li>
<li>A slice is formed by specifying two indices, a low and high bound, separated by a colon: <code>a[low : high]</code>
high is not included.<br /></li>
</ul>
<pre><code>primes := [6]int{2, 3, 5, 7, 11, 13}
var s []int = primes[1:4]
fmt.Println(s) // Outputs: [3 5 7]
</code></pre>
</section>
<section>
<h3 id="slices-and-make-operator">Slices and <code>make</code> operator</h3>
<p><a href="https://play.golang.org/p/ZwE_3e063GG">https://play.golang.org/p/ZwE_3e063GG</a></p>
<ul>
<li>The make function allocates a zeroed array and returns a slice that refers to that array:</li>
</ul>
<pre><code>a := make([]int, 5) // len(a)=5
fmt.Printf("len=%d cap=%d %v\n", len(a), cap(a), a)
// Outputs:
// len=5 cap=5 [0 0 0 0 0]
b := make([]int, 0, 5)
fmt.Printf("len=%d cap=%d %v\n", len(b), cap(b), b)
// Outputs:
// b len=0 cap=5 []
</code></pre>
</section>
</section>
<section>
<h3 id="maps">Maps</h3>
<p><a href="https://play.golang.org/p/-axSPJOLI1A">https://play.golang.org/p/-axSPJOLI1A</a></p>
<ul>
<li>The <code>make</code> function returns a map of the given type, initialized and ready for use.</li>
</ul>
<pre><code>// Create an array of float64
udea := [2]float64{6.2554189, -75.5920199}
// Allocate a map from string to [2]float64
sitios := make(map[string][2]float64)
// assign element to map
sitios["udea"] = udea
fmt.Println(sitios)
// Outputs :[6.2554189 -75.5920199]]
</code></pre>
</section>
<section>
<section><h3 id="range-operator-for-slices">Range operator for slices</h3>
<p><a href="https://play.golang.org/p/NtNGEbZokIX">https://play.golang.org/p/NtNGEbZokIX</a></p>
<ul>
<li>The range form of the for loop iterates over a slice or map.</li>
<li>When ranging over a slice, two values are returned: the first is the index, and a copy of the element at that index.</li>
</ul>
<pre><code>var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
for i, v := range pow {
fmt.Printf("2**%d = %d\n", i, v)
}
</code></pre>
</section>
<section>
<h3 id="range-operator-for-maps">Range operator for maps</h3>
<p><a href="https://play.golang.org/p/WmxXd4xsftV">https://play.golang.org/p/WmxXd4xsftV</a></p>
<ul>
<li>When ranging over a map, two values are returned for each iteration.
The first is the key, and the second is a copy of the element at that key.</li>
</ul>
<pre><code>var pow = map[int]int{0:1, 1:2, 2:4, 3:8}
for k, v := range pow {
fmt.Printf("2**%d = %d\n", k, v)
}
</code></pre>
</section>
</section>
<section>
<h3 id="anonymous-functions-lambdas">Anonymous functions (lambdas)</h3>
<ul>
<li>Functions can be passed around just like other values, and may be used as function arguments and return values.</li>
</ul>
<pre><code>func compute(fn func(float64, float64) float64) float64 {
return fn(3, 4)
}
func main() {
hypot := func(x, y float64) float64 {
return math.Sqrt(x*x + y*y)
}
fmt.Println(hypot(5, 12))
fmt.Println(compute(hypot))
fmt.Println(compute(math.Pow))
}
</code></pre>
</section>
<section>
<section><h3 id="pointers">Pointers</h3>
<p>Go has pointers, but lack pointer arithmetic
<a href="https://play.golang.org/p/O0qc2lYGuAK">https://play.golang.org/p/O0qc2lYGuAK</a></p>
<pre><code>package main
import "fmt"
func main() {
i, j := 42, 2701
p := &i // point to i
fmt.Println(p) // who is p?
fmt.Println(*p) // read i through the pointer
*p = 21 // set i through the pointer
fmt.Println(i) // see the new value of i
p = &j // point to j
fmt.Println(p) // who is p now?
*p = *p / 37 // divide j through the pointer
fmt.Println(j) // see the new value of j
}
</code></pre>
</section>
<section>
<h3 id="pointer-syntax">Pointer Syntax</h3>
<p>The type <code>*T</code> is a pointer to a <code>T</code> value. Its zero value is <code>nil</code>.</p>
<pre><code>var p *int // Will store a pointer (nil)
</code></pre>
<p>The <code>&</code> operator generates a pointer to its operand.</p>
<pre><code>i := 42 // Short int declaration
p = &i // p now points to i
</code></pre>
<p>The <code>*</code> operator denotes the pointer’s underlying value.</p>
<pre><code>fmt.Println(*p) // read i through the pointer p
+p = 21 // set i through the pointer p
</code></pre>
<p>This is known as “dereferencing” or “indirecting”.</p>
</section>
</section>
<section>
<section><h3 id="structures">Structures</h3>
<p><a href="https://play.golang.org/p/zQWVtBPH3-w">https://play.golang.org/p/zQWVtBPH3-w</a></p>
<pre><code>// We declare the Person structure
type Person struct {
Name string
LastN string
Email string
}
func main() {
john := Person{"John","Doe", "[email protected]"}
fmt.Println("The person is", john)
}
</code></pre>
</section>
<section>
<h3 id="structure-methods">Structure Methods</h3>
<p><a href="https://play.golang.org/p/OdmLz3h-AbI">https://play.golang.org/p/OdmLz3h-AbI</a></p>
<pre><code>// Mumble returns a greeting string
func (p *Person) Mumble() string {
return "Hey, my name is " + p.Name + " " + p.LastN
}
// Speak Mumbles to standard output
func (p *Person) Speak() {
fmt.Println(p.Mumble())
}
</code></pre>
</section>
<section>
<h3 id="structure-methods-1">Structure Methods</h3>
<p><a href="https://play.golang.org/p/OdmLz3h-AbI">https://play.golang.org/p/OdmLz3h-AbI</a></p>
<pre><code>func main() {
// Struct literal for John
john := Person{"John", "Doe", "[email protected]"}
// We can see that the standard output handles
// Data streaming easily (Not overload required)
fmt.Println("The person is", john)
// The struct is edowed with two methods
fmt.Println(john.Mumble())
// Tell'em John!
john.Speak()
}
</code></pre>
</section>
<section>
<h3 id="good-practices-with-structures">Good practices with structures</h3>
<p>Create Structure instances with <code>New...</code> functions,</p>
<pre><code class="language-go">type Something {
...
}
...
func NewSomething(...) *Something {
...
}
</code></pre>
</section>
<section>
<h3 id="good-practices-with-structures-1">Good practices with structures</h3>
<p>Create Structure instances with <code>New...</code> functions,</p>
<pre><code class="language-go">type Something {
...
}
...
func NewSomething(...) *Something {
...
}
</code></pre>
</section>
</section>
<section>
<section><h3 id="interfaces">Interfaces</h3>
<p><a href="https://play.golang.org/p/a_DtRz6gvub">https://play.golang.org/p/a_DtRz6gvub</a></p>
<ul>
<li>An interface type is defined as a set of method signatures.</li>
</ul>
<pre><code>// Cow structure
type Cow struct {
Color string
}
// Cow makes "moo"
func (c *Cow) MakeSound() {
fmt.Println("Moo")
}
</code></pre>
</section>
<section>
<h3 id="interfaces-1">Interfaces</h3>
<p><a href="https://play.golang.org/p/a_DtRz6gvub">https://play.golang.org/p/a_DtRz6gvub</a></p>
<pre><code>// Pigeon structure
type Pigeon struct {
Age int
}
// Pidgeon makes "Coo"
func (p *Pigeon) MakeSound() {
fmt.Println("Coo")
}
</code></pre>
</section>
<section>
<h3 id="interfaces-2">Interfaces</h3>
<p><a href="https://play.golang.org/p/a_DtRz6gvub">https://play.golang.org/p/a_DtRz6gvub</a></p>
<pre><code>// Dog structure
type Dog struct {
Age int
Color string
Name string
}
// Dogs bark
func (d *Dog) MakeSound() {
fmt.Println("Bark!")