-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-structures.tex
1091 lines (819 loc) · 29.8 KB
/
data-structures.tex
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
\cleardoublepage
\phantomsection
\chapter{Data Structures}
\begin{quote}
\textit{Bad programmers worry about the code. Good programmers
worry about data structures and their relationships.} --- Linus
Torvalds
\end{quote}
In the last last few chapters we have seen some of the primitive data
data types. We also introduced few other advanced data types without
going to the details. In this chapter, we are going to look into more
data structures.
\section{Primitive Data Types}
Advanced data structures are built on top of primitive data types. This section
is going to cover the primitive data types in Go.
\subsection{Zero Value}
In the Quick Start chapter, you have learned various ways to declare a variable.
When you declare a variable using the \texttt{var}
\index{var keyword} statement without assigning a value, a default Zero value
\index{zero value} will be assigned for certain types. The Zero value is 0 for
integers and floats, empty string for strings, and false for boolean. To
demonstrate this, here is an example:
\begin{lstlisting}
package main
import "fmt"
func main() {
var name string
var age int
var tall bool
var weight float64
fmt.Printf("%#v, %#v, %#v, %#v\n", name, age, tall, weight)
}
\end{lstlisting}
This is the output:
\begin{lstlisting}[numbers=none]
"", 0, false, 0
\end{lstlisting}
\subsection{Variable}
In the quick start chapter, we have discussed about variables and its
usage. The variable declared outside the function (package level) can
access anywhere within the same package.
Here is an example:
\begin{lstlisting}
package main
import (
"fmt"
)
var name string
var country string = "India"
func main() {
name = "Jack"
fmt.Println("Name:", name)
fmt.Println("Country:", country)
}
\end{lstlisting}
In the above example, the \texttt{name} and \texttt{country} are two
package level variables. As we have seen above the \texttt{name} gets
zero value, where as value for \texttt{country} variable is explicitly
initialized.
If the variable has been defined using the \texttt{:=} syntax, and the
user wants to change the value of that variable, they need to
use \texttt{=} instead of \texttt{:=} syntax.
If you run the below program, it's going to throw an error:
\begin{lstlisting}
package main
import (
"fmt"
)
func main() {
age := 25
age := 35
fmt.Println(age)
}
\end{lstlisting}
\begin{lstlisting}[numbers=none]
$ go run update.go
# command-line-arguments
./update.go:9:6: no new variables on left side of :=
\end{lstlisting}
The above can be fixed like this:
\begin{lstlisting}
package main
import (
"fmt"
)
func main() {
age := 25
age = 35
fmt.Println(age)
}
\end{lstlisting}
Now you should see the output:
\begin{lstlisting}[numbers=none]
$ go run update.go
35
\end{lstlisting}
Using the \textit{reflect} package\index{reflect}, you can identify
the type of a variable:
\begin{lstlisting}
package main
import (
"fmt"
"reflect"
)
func main() {
var pi = 3.41
fmt.Println("type:", reflect.TypeOf(pi))
}
\end{lstlisting}
Using one or two letter variable names inside a function is common
practice. If the variable name is multi-word, use lower camelCase
(initial letter lower and subsequent words capitalized) for unexported
variables. If the variable is an exported one, use upper CamelCase
(all the words capitalized). If the variable name contains any
abbreviations like ID, use capital letters. Here are few examples:
pi, w, r, ErrorCode, nodeToDaemonPods, DB, InstanceID.
\subsubsection{Unused variables and imports}
If you declare a variable inside a function, use that variable
somewhere in the same function where it is declared. Otherwise, you
are going to get a compile error. Whereas a global variable declared
but unused is not going to throw compile time error.
Any package that is getting imported should find a place to use.
Unused import also throws compile time error.
\subsection{Boolean Type}
A boolean type represents a pair of truth values. The truth values
are denoted by the constants \textit{true} and \textit{false}. These
are the three logical operators that can be used with boolean values:
\begin{itemize}
\item \texttt{\&\&} -- Logical AND
\item \texttt{||} -- Logical OR
\item \texttt{!} -- Logical NOT
\end{itemize}
Here is an example:
\lstinputlisting{code/data-structures/logical.go}
The output of the above logical operators are like this:
\begin{lstlisting}[numbers=none]
$ go run logical.go
false
true
false
true
\end{lstlisting}
\subsection{Numeric Types}
The numeric type includes both integer types and floating-point types.
The allowed values of numeric types are same across all the CPU
architectures.
These are the unsigned integers:
\begin{itemize}
\item uint8 -- the set of all unsigned 8-bit integers (0 to 255)
\item uint16 -- the set of all unsigned 16-bit integers (0 to 65535)
\item uint32 -- the set of all unsigned 32-bit integers (0 to 4294967295)
\item uint64 -- the set of all unsigned 64-bit integers (0 to 18446744073709551615)
\end{itemize}
These are the signed integers:
\begin{itemize}
\item int8 -- the set of all signed 8-bit integers (-128 to 127)
\item int16 -- the set of all signed 16-bit integers (-32768 to 32767)
\item int32 -- the set of all signed 32-bit integers (-2147483648 to 2147483647)
\item int64 -- the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
\end{itemize}
These are the two floating-point numbers:
\begin{itemize}
\item float32 -- the set of all IEEE-754 32-bit floating-point numbers
\item float64 -- the set of all IEEE-754 64-bit floating-point numbers
\end{itemize}
These are the two complex numbers:
\begin{itemize}
\item complex64 -- the set of all complex numbers with float32 real and imaginary parts
\item complex128 -- the set of all complex numbers with float64 real and imaginary parts
\end{itemize}
These are the two commonly used used aliases:
\begin{itemize}
\item byte -- alias for uint8
\item rune -- alias for int32
\end{itemize}
\subsection{String Type}
A string type is another most import primitive data type. String type
represents string values.
\section{Constants}
A constant\index{constant} is an unchanging value. Constants are
declared like variables, but with the \textit{const} keyword.
Constants can be character, string, boolean, or numeric values.
Constants cannot be declared using the \texttt{:=} syntax.
In Go, \textit{const} is a keyword introducing a name for a scalar
value such as 2 or 3.14159 or "scrumptious". Such values, named or
otherwise, are called constants in Go. Constants can also be created
by expressions built from constants, such as 2+3 or 2+3i or math.Pi/2
or ("go"+"pher"). Constants can be declared are at package level or
function level.
This is how to declare constants:
\begin{lstlisting}[numbers=none]
package main
import (
"fmt"
)
const Freezing = true
const Pi = 3.14
const Name = "Tom"
func main() {
fmt.Println(Pi, Freezing, Name)
}
\end{lstlisting}
You can also use the factored style declaration:
\begin{lstlisting}[numbers=none]
package main
import (
"fmt"
)
const (
Freezing = true
Pi = 3.14
Name = "Tom"
)
func main() {
fmt.Println(Pi, Freezing, Name)
}
const (
Freezing = true
Pi = 3.14
Name = "Tom"
)
\end{lstlisting}
Compiler throws an error if the constant is tried to assign a new
value:
\begin{lstlisting}[numbers=none]
package main
import (
"fmt"
)
func main() {
const Pi = 3.14
Pi = 6.86
fmt.Println(Pi)
}
\end{lstlisting}
The above program throws an error like this:
\begin{lstlisting}[numbers=none]
$ go run constants.go
constants:9:5: cannot assign to Pi
\end{lstlisting}
\subsection{iota}
The \textit{iota} keyword is used to define constants of incrementing numbers.
This simplify defining many constants. The values of iota is reset to \textit{0}
whenever the reserved word const appears. The value increments by one after each
line.
Consider this example:
\begin{lstlisting}[numbers=none]
// Token represents a lexical token.
type Token int
const (
// Illegal represents an illegal/invalid character
Illegal Token = iota
// Whitespace represents a white space
// (" ", \t, \r, \n) character
Whitespace
// EOF represents end of file
EOF
// MarkerID represents '\id' or '\id1' marker
MarkerID
// MarkerIde represents '\ide' marker
MarkerIde
)
\end{lstlisting}
In the above example, the \texttt{Token} is custom type defined using
the primitive \textit{int} type. The constants are defined using the
factored syntax (many constants within parenthesis). There are
comments for each constant values. Each constant value is be
incremented starting from \texttt{0}. In the above
example, \texttt{Illegal} is \texttt{0}, Whitespace
is \texttt{1}, \texttt{EOF} is \texttt{2} and so on.
The \texttt{iota} can be used with expressions. The expression will
be repeated. Here is a good example taken from \textit{Effective
Go} (\url{https://go.dev/doc/effective_go#constants}):
\begin{lstlisting}[numbers=none]
type ByteSize float64
const (
// ignore first value (0) by assigning to blank identifier
_ = iota
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
PB
EB
ZB
YB
)
\end{lstlisting}
Using \texttt{\_} (blank identifier) you can ignore a value,
but \textit{iota} increments the value. This can be used to skip
certain values. As you can see in the above example, you can use an
expression with \textit{iota}.
Iota is reset to \texttt{0} whenever the \textit{const} keyword appears in the
source code. This means that if you have multiple \textit{const} declarations in
a single file, \textit{iota} will start at \texttt{0} for each declaration. Iota
can only be used in \textit{const} declarations. It cannot be used in other
types of declarations, such as \textit{var} declarations. The value
of \textit{iota} is only available within the const declaration in which it is
used. It cannot be used outside of that declaration.
\subsubsection{Blank Identifier}
Sometimes you may need to ignore the value returned by a function. Go
provides a special identifier called blank identifier\index{blank
identifier}\index{\_} to ignore any types of values. In Go,
underscore \texttt{\_} is the blank identifier.
Here is an example usage of blank identifier where the second value
returned by the function is discarded.
\begin{lstlisting}[numbers=none]
x, _ := someFunc()
\end{lstlisting}
Blank identifier can be used as import alias to invoke init function
without using the package.
\begin{lstlisting}[numbers=none]
import (
"database/sql"
_ "github.com/lib/pq"
)
\end{lstlisting}
In the above example, the \texttt{pq} package has some code which need
to be invoked to initialize the database driver provided by that
package. And the exported functions within the above package is
supposed to be not used.
We have already seen another example where blank identifier if used
with \textit{iota} to ignore certain constants.
\section{Arrays}
\label{sec:arrays}
An array\index{array} is an ordered container type with a fixed number
of data. In fact, the arrays are the foundation where slice is built.
We will study about slices in the next section. Most of the time, you
can use slice instead of an array.
The number of values in the array is called the length of that array.
The array type \texttt{[n]T} is an array of \texttt{n} values of
type \texttt{T}. Here are two example arrays:
\begin{lstlisting}[numbers=none]
colors := [3]string{"Red", "Green", "Blue"}
heights := [4]int{153, 146, 167, 170}
\end{lstlisting}
In the above example, the length of first array is \texttt{3} and the
array values are string data. The second array contains \texttt{int}
values. An array's length is part of its type, so arrays cannot be
re-sized. So, if the length is different for two arrays, those are
distinct incompatible types. The built-in \texttt{len} function gives
the length of array.
Array values can be accessed using the index syntax, so the expression
\texttt{s[n]} accesses the nth element, starting from zero.
An array values can be read like this:
\begin{lstlisting}[numbers=none]
colors := [3]string{"Red", "Green", "Blue"}
i := colors[1]
fmt.Println(i)
\end{lstlisting}
Similarly array values can be set using index syntax. Here is an
example:
\begin{lstlisting}[numbers=none]
colors := [3]string{"Red", "Green", "Blue"}
colors[1] = "Yellow"
\end{lstlisting}
Arrays need not be initialized explicitly. The zero value of an
array is a usable array with all elements zeroed.
\begin{lstlisting}[numbers=none]
var colors [3]string
colors[1] = "Yellow"
\end{lstlisting}
In this example, the values of colors will be empty strings (zero
value). Later we can assign values using the index syntax.
There is a way to declare array literal without specifying the length.
When using this syntax variant, the compiler will count and set the
array length.
\begin{lstlisting}[numbers=none]
colors := [...]string{"Red", "Green", "Blue"}
\end{lstlisting}
In the chapter on control structures, we have seen how to use For loop
for iterating over slices. In the same way, you can iterate over
array.
Consider this complete example:
\lstinputlisting{code/data-structures/colors.go}
If you save the above program in a file named \texttt{colors.go} and
run it, you will get output like this:
\begin{lstlisting}[numbers=none]
$ go run colors.go
Length: 3
0 Red
1 Green
2 Blue
\end{lstlisting}
In the above program, a string array is declared and initialized with
three string values. In the 7th line, the length is printed and it
gives 3. The \texttt{range} clause gives index and value, where the
index starts from zero.
\section{Slices}
Slice\index{slice} is one of most important data structure in Go.
Slice is more flexible than an array. It is possible to add and
remove values from a slice. There will be a length for slice at any
time. Though the length vary dynamically as the content value
increase or decrease.
The number of values in the slice is called the length of that slice.
The slice type \texttt{[]T} is a slice of type \texttt{T}. Here are
two example slices:
\begin{lstlisting}[numbers=none]
colors := []string{"Red", "Green", "Blue"}
heights := []int{153, 146, 167, 170}
\end{lstlisting}
The first one is a slice of strings and the second slice is a slice of
integers. The syntax is similar to array except the length of slice
is not explicitly specified. You can use built-in \texttt{len}
function to see the length of slice.
Slice values can be accessed using the index syntax, so the expression
\texttt{s[n]} accesses the nth element, starting from zero.
A slice values can be read like this:
\begin{lstlisting}[numbers=none]
colors := []string{"Red", "Green", "Blue"}
i := colors[1]
fmt.Println(i)
\end{lstlisting}
Similary slice values can be set using index syntax. Here is an
example:
\begin{lstlisting}[numbers=none]
colors := []string{"Red", "Green", "Blue"}
colors[1] = "Yellow"
\end{lstlisting}
Slices should be initialized with a length more than zero to access or
set values. In the above examples, we used slice literal syntax for
that. If you define a slice using \texttt{var} statement without
providing default values, the slice will be have a special zero value
called \texttt{nil}.
Consider this complete example:
\lstinputlisting{code/data-structures/nilslice.go}
In the above example, the value of slice \texttt{v} is \texttt{nil}.
Since the slice is nil, values cannot be accessed or set using the
index. These operations are going to raise runtime error (index out
of range).
Sometimes it may not be possible to initialize a slice with some value
using the literal slice syntax given above. Go provides a built-in
function named \texttt{make} to initialize a slice with a given length
and zero values for all items. For example, if you want a slice with
3 items, the syntax is like this:
\begin{lstlisting}[numbers=none]
colors := make([]string, 3)
\end{lstlisting}
In the above example, a slice will be initialized with 3 empty strings
as the items. Now it is possible to set and get values using the
index as given below:
\begin{lstlisting}[numbers=none]
colors[0] = "Red"
colors[1] = "Green"
colors[2] = "Blue"
i := colors[1]
fmt.Println(i)
\end{lstlisting}
If you try to set value at 3rd index (\texttt{colors[3]}), it's going
to raise runtime error with a message like this: "index out of range".
Go has a built-in function named \texttt{append} to add additional
values. The append function will increase the length of the slice.
Consider this example:
\lstinputlisting{code/data-structures/appendslice.go}
In the above example, the slice length is increased by one after
append. It is possible to add more values using \texttt{append}. See
this example:
\lstinputlisting{code/data-structures/appendslicemore.go}
The above example append two values. Though you can provide any
number of values to append.
You can use the "..." operator to expand a slice. This can be used to
append one slice to another slice. See this example:
\lstinputlisting{code/data-structures/appendsliceanother.go}
In the above example, the first slice is appended by all items in another slice.
\subsection{Slice Append Optimization}
If you append\index{slide!append} too many values to a slice using a
for loop, there is one optimization related that you need to be aware.
Consider this example:
\lstinputlisting{code/data-structures/appendfor.go}
If you run the above program, it's going to take few seconds to
execute. To explain this, some understanding of internal structure of
slice is required. Slice is implemented as a struct and an array
within. The elements in the slice will be stored in the underlying
array. As you know, the length of array is part of the array type.
So, when appending an item to a slice the a new array will be created.
To optimize, the \texttt{append} function actually created an array
with double length.
In the above example, the underlying array must be changed many times.
This is the reason why it's taking few seconds to execute. The length
of underlying array is called the capacity of the slice. Go provides
a way to initialize the underlying array with a particular length.
The \texttt{make} function has a fourth argument to specify the
capacity.
In the above example, you can specify the capacity like this:
\begin{lstlisting}[numbers=none]
v := make([]string, 0, 9000000)
\end{lstlisting}
If you make this change and run the program again, you can see that it
run much faster than the earlier code. The reason for faster code is
that the slice capacity had already set with maximum required length.
\section{Maps}
Map\index{map} is another important data structure in Go. We have
briefly discussed about maps in the Quick Start chapter. As you know,
map is an implementation of hash table. The hash table is available
in many very high level languages. The data in map is organized like
key value pairs.
A variable of map can be declared like this:
\begin{lstlisting}[numbers=none]
var fruits map[string]int
\end{lstlisting}
To make use that variable, it needs to be initialized using \textit{make}
function.
\begin{lstlisting}[numbers=none]
fruits = make(map[string]int)
\end{lstlisting}
You can also initialize using the \textit{:=} syntax:
\begin{lstlisting}[numbers=none]
fruits := map[string]int{}
\end{lstlisting}
or with \textit{var} keywod:
\begin{lstlisting}[numbers=none]
var fruits = map[string]int{}
\end{lstlisting}
You can initialize map with values like this:
\begin{lstlisting}[numbers=none]
var fruits = map[string]int{
"Apple": 45,
"Mango": 24,
"Orange": 34,
}
\end{lstlisting}
After initializing, you can add new key value pairs like this:
\begin{lstlisting}[numbers=none]
fruits["Grape"] = 15
\end{lstlisting}
If you try to add values to maps without initializing, you will get an error
like this:
\begin{lstlisting}[numbers=none]
panic: assignment to entry in nil map
\end{lstlisting}
Here is an example that's going to produce panic error:
\begin{lstlisting}[numbers=none]
package main
func main() {
var m map[string]int
m["k"] = 7
}
\end{lstlisting}
To access a value corresponding to a key, you can use this syntax:
\begin{lstlisting}[numbers=none]
mangoCount := fruits["Mango"]
\end{lstlisting}
Here is an example:
\begin{lstlisting}[numbers=none]
package main
import "fmt"
func main() {
var fruits = map[string]int{
"Apple": 45,
"Mango": 24,
"Orange": 34,
}
fmt.Println(fruits["Apple"])
}
\end{lstlisting}
If the key doesn't exist, a zero value will be returned. For example, in the
below example, value of \texttt{pineappleCount} is going be \texttt{0}.
\begin{lstlisting}[numbers=none]
package main
import "fmt"
func main() {
var fruits = map[string]int{
"Apple": 45,
"Mango": 24,
"Orange": 34,
}
pineappleCount := fruits["Pineapple"]
fmt.Println(pineappleCount)
}
\end{lstlisting}
If you need to check if the key exist, the above syntax can be modified to
return two values. The first one would be the actual value or zero value and the
second one would be a boolean indicating if the key exists.
\begin{lstlisting}[numbers=none]
package main
import "fmt"
func main() {
var fruits = map[string]int{
"Apple": 45,
"Mango": 24,
"Orange": 34,
}
_, ok := fruits["Pineapple"]
fmt.Println(ok)
}
\end{lstlisting}
In the above program, the first returned value is ignored using a blank
identifier. And the value of \texttt{ok} variable is \texttt{false}. Normally,
you can use if condition to check if the key exists like this:
\begin{lstlisting}[numbers=none]
package main
import "fmt"
func main() {
var fruits = map[string]int{
"Apple": 45,
"Mango": 24,
"Orange": 34,
}
if _, ok := fruits["Pineapple"]; ok {
fmt.Println("Key exists.")
} else {
fmt.Println("Key doesn't exist.")
}
}
\end{lstlisting}
To see the number of key/value pairs, you can use the built-in \textit{len}
function.
\begin{lstlisting}[numbers=none]
package main
import "fmt"
func main() {
var fruits = map[string]int{
"Apple": 45,
"Mango": 24,
"Orange": 34,
}
fmt.Println(len(fruits))
}
\end{lstlisting}
The above program should print \texttt{3} as the number of items in the map.
To remove an item from the map, use the built-in \textit{delete} function.
\begin{lstlisting}[numbers=none]
package main
import "fmt"
func main() {
var fruits = map[string]int{
"Apple": 45,
"Mango": 24,
"Orange": 34,
}
delete(fruits, "Orange")
fmt.Println(len(fruits))
}
\end{lstlisting}
The above program should print \texttt{2} as the number of items in the map
after deleting one item.
\section{Custom Data Types}
Apart from the built-in data types, you can create your own custom
data types\index{type!custom}. The type keyword\index{type keyword}
can be used to create custom types. Here is an example.
\begin{lstlisting}[numbers=none]
package main
import "fmt"
type age int
func main() {
a := age(2)
fmt.Println(a)
fmt.Printf("Type: %T\n", a)
}
\end{lstlisting}
If you run the above program, the output will be like this:
\begin{lstlisting}[numbers=none]
$ go run age.go
2
Type: age
\end{lstlisting}
\subsection{Structs}
Struct\index{struct} is a composite type with multiple fields of
different types within the struct. For example, if you want to
represent a person with name and age, the \texttt{struct} type will be
helpful. The \texttt{Person} struct definition will look like this:
\begin{lstlisting}[numbers=none]
type Person struct {
Name string
Age int
}
\end{lstlisting}
As you can see above, the \texttt{Person} struct is defined
using \texttt{type} and \texttt{struct} keywords. Within the curly
brace, attributes with other types are defined. If you avoid
attributes, it will become an empty struct.
Here is an example empty struct:
\begin{lstlisting}[numbers=none]
type Empty struct {
}
\end{lstlisting}
Alternatively, the curly brace can be in the same line.
\begin{lstlisting}[numbers=none]
type Empty struct {}
\end{lstlisting}
A struct can be initialized various ways. Using a \texttt{var}
statement:
\begin{lstlisting}[numbers=none]
var p1 Person
p1.Name = "Tom"
p1.Age = 10
\end{lstlisting}
You can give a literal form with all attribute values:
\begin{lstlisting}[numbers=none]
p2 := Person{"Polly", 50}
\end{lstlisting}
You can also use named attributes. In the case of named attributes,
if you miss any values, the default zero value will be initialized.
\begin{lstlisting}[numbers=none]
p3 := Person{Name: "Huck"}
p4 := Person{Age: 10}
\end{lstlisting}
In the next, we are going to learn about funtions and methods. That
chapter expands the discussion about custom types bevior changes
through funtions associated with custom types called strcuts.
It is possible to embed structs inside other
structs\index{struct!embedding}. Here is an example:
\begin{lstlisting}[numbers=none]
type Person struct {
Name string
}
type Member struct {
Person
ID int
}
\end{lstlisting}
\section{Pointers}
When you are passing a variable as an argument to a function, Go
creates a copy of the value and send it. In some situations, creating
a copy will be expensive when the size of object is large. Another
scenario where pass by value is not feasible is when you need to
modify the original object inside the function. In the case of pass
by value, you can modify it as you are getting a new object every
time. Go supports another way to pass a reference to the original
value using the memory location or address of the
object\index{pointer}.
To get address of a variable, you can use \texttt{\&} as a prefix for
the variable. Here in an example usage:
\begin{lstlisting}[numbers=none]
a := 7
fmt.Printf("%v\n", &a)
\end{lstlisting}
To get the value back from the address, you can use \texttt{*} as a
prefix for the variable. Here in an example usage:
\begin{lstlisting}[numbers=none]
a := 7
b := &a
fmt.Printf("%v\n", *b)
\end{lstlisting}
Here is a complete example:
\lstinputlisting{code/data-structures/pointervalue.go}
A typical output will be like this:
\begin{lstlisting}[numbers=none]
0xc42000a340
0xc42000a348
0xc42000a340
\end{lstlisting}
As you can see above, the second output is different from the first
and third. This is because a value is passed instead of a pointer.
And so when we are printing the address, it's printing the address of
the new variable.
In the functions chapter, the section about methods
(section~\ref{sec:methods}) explains the pointer receiver.
\subsection{new}
The built-in function \textit{new} can be used to allocate memory. It
allocates zero values and returns the address of the given data type.