Arrays
Arrays
Composite Types
Gopher Level
Intermediate
⭐⭐
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
PART IV
Composite Types
★ What is an array?
★ Getting and Setting Array Elements
★ Array Literals — Easy way to create arrays
★ Comparing Arrays
★ Assigning Arrays
★ Multi-Dimensional Arrays
★ Keyed Elements
★ Named vs Unnamed Types
😀 MOODLY 😞
Random moods
DIGITAL CLOCK
Animated & Runs on CLI ⌨
a := 54
b := 143
... := ...
oneThousand := 1000
beginning
{ end
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ALLOCATION
Variables can be allocated in different locations in memory
0 0
0 0
100th 101st
memory cell memory cell
0 0
100th 101st
memory cell memory cell
0 0
0 0
0 0
0 0
fixed length
0
0 0
0 0 0
0 0 0 0 0
0 0
element type
it always have to be declared!
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S ELEMENT TYPE
It determines the type of values that an array can store
0 0
element type
you can only store "byte" values in this array
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S ELEMENT TYPE
Go automatically sets uninitialized array elements to their Zero Values
0 0
this array's element type is byte; its elements are not initialized; so, they're 0
element type
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S ELEMENT TYPE
You can use any type as an element type
"" ""
element type
you can only store "string" values in this array
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S ELEMENT TYPE
You can't store different type of values in an array
"" 0
element type
all the values in an array should have the same type
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
UNNAMED VARIABLES
Each array element is actually an unnamed variable
0 0
Unnamed Variables
Go creates unnamed variables depending on array's element type
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
GETTING ELEMENTS
You can get an array element using an index expression
0 0
type mismatch!
ages is an int array
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY LITERAL
You can use it to create an array + along with its elements
Kafka's
Kafka's
Stay Golden Everything... Revenge
Revenge
2nd Edition
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY LITERAL
Creates & Initializes a new array with the given values
Kafka's
Kafka's
Stay Golden Everything... Revenge
Revenge
2nd Edition
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY LITERAL
If you've the elements already: Use this syntax
Kafka's
Kafka's
Stay Golden Everything... Revenge
Revenge
2nd Edition
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY LITERAL
Array literal is one of the composite literals
[4] string {
"Kafka's Revenge" ,
"Stay Golden" ,
"Everythingship" ,
"Kafka's Revenge 2nd Edition" ,
}
[4] string {
"Kafka's Revenge" ,
Array's Type "Stay Golden" ,
[4]string "Everythingship" ,
"Kafka's Revenge 2nd Edition" ,
}
[4] string {
"Kafka's Revenge" ,
"Stay Golden" , Element List
"Everythingship" , { element, ... }
"Kafka's Revenge 2nd Edition" ,
}
"Kafka's
Revenge"
"Stay Golden" "" ""
"Kafka's
"Stay Golden" "" ""
Revenge"
"Kafka's
"Stay Golden"
Revenge"
3 3 6 more books
3 3 3 9 more books
3 3 more books
3 3 6 more books
6 9 3
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
[3]int{6, 9, 3} [3]int{6, 9, 3}
6 9 3 == 6 9 3
?
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
COMPARING ARRAYS
Arrays are comparable when their types are identical
[3]int{6, 9, 3} [3]int{6, 9, 3}
6 9 3 == 6 9 3
Comparable
They've identical types
[3]int
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
COMPARING ARRAYS
Arrays are equal when their elements are equal
[3]int{6, 9, 3} [3]int{6, 9, 3}
6 9 3 == 6 9 3
Equal
They have the same elements
6, 9, and 3
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
COMPARING ARRAYS
Go compares every element of the arrays one by one
6 9 3 == 6 9 3
blue[0] == red[0]
blue[1] == red[1]
blue[2] == red[2]
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
COMPARING ARRAYS
Arrays are not equal even when their elements are not equal
[3]int{6, 9, 3} [3]int{3, 9, 6}
6 9 3 ≠ 3 9 6
Not Equal
They have the same elements; but in a different order
[3]int{6, 9, 3} [2]int{6, 9}
6 9 3 ? 6 9
[3]int{6, 9, 3} [2]int{6, 9}
6 9 3 6 9
Not Comparable
They have different types
3 3 3 3
3 3 3 3 3 3
3 3
allocates
24 bytes of new memory
6 9 3 6 9 3
6 9 3 6 9 3
red := blue
blue[0] = 10
red = blue
6 9 3 3 5
[3]int{5, 6, 1} 5 6 1
[3]int{9, 8, 4} 9 8 4
[3]int{9, 8, 4}
5 6 1
9 8 4
[0] index 5 6 1
9 8 4
5 6 1
[1] index 9 8 4
[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}
[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}
[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}
[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}
[2] [3]int {
{5, 6, 1} ,
{9, 8, 4} ,
}
Go initializes
the uninitialized elements
0 0 1.5
to their zero values
0 0 0 0 0 1.5
[3]int{6, 9, 3}
unnamed type
underlying type is itself: [3]int
[ It has its own structure ]
bookcase{6, 9, 3} == [3]int{6, 9, 3}
✓
You don't need to convert these array values
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
NAMED <-> NAMED
A named type is always a different type than any other type
bookcase{6, 9, 3} == cabinet{6, 9, 3}
bookcase(
bookcase{6, 9, 3} == cabinet{6, 9, 3},
)
✓
You can only convert
if their underlying types are identical
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
summary
memory cells
15 25
0 0
"Kafka's
"Stay Golden" "" ""
Revenge"
"Kafka's
"Stay Golden"
Revenge"
[2] [3]int {
{5, 6, 1} ,
{9, 8, 4} ,
}
10 9 3 6 9 3
red := blue
blue[0] = 10
red == blue
blue = red
6 9 3 6 9
[3]int{6, 9, 3}
unnamed type
underlying type is itself: [3]int
[ It has its own structure ]
bookcase{6, 9, 3} == [3]int{6, 9, 3}
✓
You don't need to convert these array values
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
PART IV
Composite Types
✓
Arrays Slices String
Maps
Collection Collection Internals Structs
Collection
of of Byte Slices Groups
of
Elements Elements ASCII & Unicode different types of
Indexable
Indexable Indexable Encoding & Decoding variables together
Key-Value Pairs
Fixed Length Dynamic length