0% found this document useful (0 votes)
12 views100 pages

Arrays

Uploaded by

sulfan aidid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views100 pages

Arrays

Uploaded by

sulfan aidid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

PART IV

Composite Types

Gopher Level
Intermediate
⭐⭐
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

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

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAYS
What you're going to learn in this section?

★ 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

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAYS
By the end of the section, you're going to build awesome projects again!

😀 MOODLY 😞
Random moods

$ go run main.go inanc positive


inanc feels awesome 😎

$ go run main.go inanc negative


inanc feels sad 😞

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAYS
By the end of the section, you're going to build awesome projects again!

DIGITAL CLOCK
Animated & Runs on CLI ⌨

███ ███ ███ █ █ █ █ ███


█ █ █ ░ █ █ █ ░ █ █ █ █
█ █ ███ ███ ███ ███ ███
█ █ █ █ ░ █ █ ░ █ █
███ ███ ███ █ █ ███

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Why Arrays?
Most of the time
we need to work
with
multiple values

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


It's hard to do this manually!

a := 54
b := 143
... := ...
oneThousand := 1000

sum := a + b + ... + oneThousand

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


It's easy to do it with an array or a slice!

nums := [...]int{54, 143, ... , 1000}

var sum int


for _, num := range nums {
sum += num
}

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


This is only some of the benefits of arrays

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


WHAT'S AN ARRAY?
Array is a fixed length container for the same type of values

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


COMPUTER MEMORY
Computer memory stores values in memory cells

Think of this grid like the computer memory

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


MEMORY CELLS
Each memory cell occupies 1 byte

a single memory cell


1 byte wide

beginning
{ end
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ALLOCATION
Variables can be allocated in different locations in memory

100th memory cell 105th memory cell

0 0

var myAge byte var herAge byte

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAY
An array stores its elements in contiguous memory cells

a single array value

0 0

100th 101st
memory cell memory cell

var ages [2]byte

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAY
Efficiency: CPU Cache-Lines, Fast Access, 1-o-1 representation of memory...

a single array value

0 0

100th 101st
memory cell memory cell

var ages [2]byte

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAY
The size of an array is equal to the total size of its elements

var ages [2]byte

0 0

1 byte + 1 byte = 2 bytes

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAY
The size of an array is equal to the total size of its elements

var ages [2]int16

100th memory cell 102th memory cell

0 0

2 bytes + 2 bytes = 4 bytes

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAY'S LENGTH
It determines the number of elements that an array can store

0 0

var ages [2]byte

length of this array


length should always be declared!
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S LENGTH
It can only be a constant value — Array's type belongs to compile-time

0 0

var ages [1 + 1] byte

length of this array


You can use constant values and constant expressions!
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S LENGTH
Array's length determines its maximum capacity

fixed length
0
0 0

var ages [2]byte

length of this array


You cannot store more than 2 elements in this array....
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S LENGTH
If you need more capacity you can declare a new array

0 0 0

var ages [3]byte

length of this array


You can only store 3 elements in this array....
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S LENGTH
It determines the number of elements that an array can store

0 0 0 0 0

var ages [5]byte

length of this array


You can only store 5 elements in this array....
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S LENGTH
It can't be a negative number

var ages [-1] byte

length of this array


negative length is meaningless
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S ELEMENT TYPE
It determines the type of values that an array can store

0 0

var ages [2]byte

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

var ages [2]byte

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

var ages [2]byte

element type
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY'S ELEMENT TYPE
You can use any type as an element type

"" ""

var tags [2]string

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

var tags [2]string

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

var ages [2]byte

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

var ages [2]byte

0 0

ages[-1] ages[0] ages[1] ages[2]

index >= 0 len(ages) 2 index < len(array)


Copyright 2019 Inanc Gumus — Twitter: @inancgumus
SETTING ELEMENTS
You can set an element using the assignment operators

var ages [2]int


ages[0] = 6
0 0
ages[1] -= 3

ages[0] = "Can I?"

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

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAY LITERAL
Isn't it cumbersome to set elements of an array like this?...

var books [4] string


books[0] = "Kafka's Revenge"
books[1] = "Stay Golden"
books[2] = "Everythingship"
books[3] = "Kafka's Revenge 2nd Edition"

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

var books = [4] string {


"Kafka's Revenge" ,
"Stay Golden" ,
"Everythingship" ,
"Kafka's Revenge 2nd Edition" ,
}

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

books := [4] string {


"Kafka's Revenge" ,
books's type: "Stay Golden" ,
[4]string "Everythingship" ,
"Kafka's Revenge 2nd Edition" ,
}

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" ,
}

Composite Literals are used to


create new composite values (like arrays)
along with their elements on the fly
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY LITERAL
First: You need to provide the type of the composite literal

[4] string {
"Kafka's Revenge" ,
Array's Type "Stay Golden" ,
[4]string "Everythingship" ,
"Kafka's Revenge 2nd Edition" ,
}

Composite Literals are used to


create new composite values (like arrays)
along with their elements on the fly
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY LITERAL
Third: You need to separate the elements using commas

[4] string {
"Kafka's Revenge" ,
"Stay Golden" , Element List
"Everythingship" , { element, ... }
"Kafka's Revenge 2nd Edition" ,
}

The Last Comma is only needed


when you want to type the ending curly brace
in a new line
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ARRAY LITERAL
You don't need to type the last comma here

[4] string { "Kafka's Revenge" , "Stay Golden" }

The last element, and,


the ending curly brace
are on the same line

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAY LITERAL
What happens when you don't initialize some of the elements?

[4] string { "Kafka's Revenge" , "Stay Golden" }

The length is 4 but there are 2 elements here?!

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ZERO VALUES
Go initializes the uninitialized elements to zero values automatically

[4] string { "Kafka's Revenge" , "Stay Golden" }

"Kafka's
Revenge"
"Stay Golden" "" ""

Initialized to Zero Values


Copyright 2019 Inanc Gumus — Twitter: @inancgumus
ZERO VALUES
Go initializes the uninitialized elements to zero values automatically

[4] string { "Kafka's Revenge" , "Stay Golden" }

Array's length is still 4 not 2

"Kafka's
"Stay Golden" "" ""
Revenge"

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ELLIPSIS...
Go finds out the length of the array automatically

[...] string { "Kafka's Revenge" , "Stay Golden" }

The Length is 2 elements

"Kafka's
"Stay Golden"
Revenge"

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


COMPARING ARRAYS
You can only compare arrays that have identical types

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


📚 Hipster's Love Book Collection 📚

3 3 6 more books

3 3 3 9 more books

3 3 more books

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


📚 Hipster's Love Book Collection 📚

3 3 6 more books

bookcase 3 3 3 9 more books


data buffer
database records 3 more books
... 3
[3]int{6, 9, 3}

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

blue := [3]int{6, 9, 3} red := [3]int{6, 9, 3}

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

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


COMPARING ARRAYS
Can you compare these arrays?

[3]int{6, 9, 3} [2]int{6, 9}

6 9 3 ? 6 9

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


COMPARING ARRAYS
Arrays are not comparable when their types are different

[3]int{6, 9, 3} [2]int{6, 9}

6 9 3 6 9

Not Comparable
They have different types

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ASSIGNING ARRAYS
Remember, if you can compare, then you can assign

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


📚 Hipster's Love Book Collection 📚

Blue Bookcase Red Bookcase

3 3 3 3
3 3 3 3 3 3
3 3

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAYS ARE COPIED
Assigned array's elements will be copied into a new array

allocates
24 bytes of new memory

6 9 3 6 9 3

blue := [3]int{6, 9, 3} red := blue

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ARRAYS ARE COPIED
Copied array and the Original array are not connected

"red" array stays the same


blue := [3]int{6, 9, 3}
it's a new array

6 9 3 6 9 3

red := blue
blue[0] = 10

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


ASSIGNING ARRAYS
Different types of arrays are not assignable

red = blue

6 9 3 3 5

blue := [3]int{6, 9, 3} red := [2]int{3, 5}

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


MULTI-DIMENSIONAL ARRAYS
Arrays of Arrays

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


1st Student's Grades

[3]int{5, 6, 1} 5 6 1

2nd Student's Grades

[3]int{9, 8, 4} 9 8 4

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


[3] int{5, 6, 1}

[3]int{9, 8, 4}

5 6 1

9 8 4

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}

1st inner array


5 6 1
outer array [3]int
[2] [3]int
9 8 4 2nd inner array
contains
the inner arrays
[3]int
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}

[0] index 5 6 1

9 8 4

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}

5 6 1

[1] index 9 8 4

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


The length of the array
It can be any length

[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}

So: "for this array", there can be only 2 inner arrays

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


The element type of the array
It can be any type

[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}

Element type determines


the type of the inner arrays
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
Element types of the inner arrays

[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}

Inner arrays should have the same types

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Go already knows about the type of the elements

[2] [3]int {
[3] int{5, 6, 1} ,
[3]int{9, 8, 4} ,
}

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Go already knows about the type of the elements

[2] [3]int {
{5, 6, 1} ,
{9, 8, 4} ,
}

So, you can omit the types


Go implicitly adds them for you
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
KEYED ELEMENTS
Describe the indexes manually

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Cryptocurrency exchange ratios

What is 1 Bitcoin in Ethereum?

1 Ethereum is 0.5 Bitcoin

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


rates := [3] float64{ indexes
0.5 , 0
2.5 , 1
1.5, 2
}

0.5 2.5 1.5

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


KEYED ELEMENTS
Keyed elements describe the index positions

rates := [3] float64{


0: 0.5 ,
1: 2.5 ,
2: 1.5,
}

0.5 2.5 1.5

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


KEYED ELEMENTS
Each key corresponds to an index of the array

rates := [3] float64{ indexes


0: 0.5 , 0
1: 2.5 , 1
2: 1.5, 2
}

0.5 2.5 1.5

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


KEYED ELEMENTS
The keyed elements can be in any order

rates := [3] float64{ indexes


1: 2.5, 1
0: 0.5, 0
2: 1.5, 2
}

0.5 2.5 1.5

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


KEYED ELEMENTS
Uninitialized elements will be initialized to their zero values

rates := [3] float64{


2: 1.5,
}

Go initializes
the uninitialized elements
0 0 1.5
to their zero values

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


KEYED ELEMENTS
Uninitialized elements will be initialized to their zero values

rates := [...] float64{


5: 1.5,
}

0 0 0 0 0 1.5

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


KEYED ELEMENTS
Keyed elements can determine the index position of the unkeyed elements

rates := [...] float64{


5: 1.5,
2.5,
0: 0.5,
}

0.5 0 0 0 0 1.5 2.5

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


XRATIO
Cryptocurrency exchange ratios

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


NAMED vs UNNAMED TYPES
Composite Types are Unnamed Types

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


UNNAMED TYPES
An unnamed composite type's underlying type is itself!

[3]int{6, 9, 3}

unnamed type
underlying type is itself: [3]int
[ It has its own structure ]

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


NAMED TYPE
Type definition creates a new type with a new name

underlying type is [3]int

type bookcase [3]int

the name of the type

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


UNNAMED <-> NAMED
Unnamed and Named Typed Arrays comparable
If their underlying types are identical

type bookcase [3]int

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

type bookcase [3]int type cabinet [3]int

bookcase{6, 9, 3} == cabinet{6, 9, 3}

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


NAMED <-> NAMED
They're comparable when converted ✓

type bookcase [3]int type cabinet [3]int

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

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Array is a collection of elements
It stores the same type and the same number of elements
in contiguous memory locations

memory cells

15 25

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


You can access array elements using index expressions

var ages [2]byte

0 0

ages[-1] ages[0] ages[1] ages[2]

index >= 0 len(ages) 2 index < len(array)


Copyright 2019 Inanc Gumus — Twitter: @inancgumus
How many elements
that the array will store?

var name [length]elementType

Declares a variable What type of elements


that will store that the array will store?
the array value

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


var name [length]elementType

This is the type of the array


Its length and its element type
determine the type of the array as a whole
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
Array Literals

[4] string { "Kafka's Revenge" , "Stay Golden" }

Array's length is still 4 not 2

"Kafka's
"Stay Golden" "" ""
Revenge"

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Ellipsis...

[...] string { "Kafka's Revenge" , "Stay Golden" }

The Length is 2 elements

"Kafka's
"Stay Golden"
Revenge"

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Keyed Elements

rates := [...] float64{


5: 1.5,
2.5,
0: 0.5,
}

0.5 0 0 0 0 1.5 2.5

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Multi-Dimensional Arrays

[2] [3]int {
{5, 6, 1} ,
{9, 8, 4} ,
}

An array with two inner [3]int arrays


Element type: [3]int
Length: 2
Copyright 2019 Inanc Gumus — Twitter: @inancgumus
Copied array and the Original array are not connected

"red" array stays the same


blue := [3]int{6, 9, 3}
it's a copy of the blue array

10 9 3 6 9 3

red := blue
blue[0] = 10

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Different types of arrays are neither comparable nor assignable

red == blue

blue = red

6 9 3 6 9

blue := [3]int{6, 9, 3} red := [2]int{6, 9}

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


An unnamed composite type's underlying type is itself!

[3]int{6, 9, 3}

unnamed type
underlying type is itself: [3]int
[ It has its own structure ]

Copyright 2019 Inanc Gumus — Twitter: @inancgumus


Unnamed and Named typed values are comparable
If their underlying types are identical

type bookcase [3]int

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

Copyright 2019 Inanc Gumus — Twitter: @inancgumus

You might also like