You are on page 1of 77

Types

slice, map, new, make, struct


https://golang.org/ref/spec
slice
list
slice vs. slicing vs. index access
making a slice
making a slice
slicing a slice
m
ak
in
g
a
sli
ce
If we think that our slice might grow, we can set a capacity larger
than length. This gives our slice room to grow without golang
having to create a new underlying array every time our slice
grows. When the slice exceeds capacity, then a new underlying
array will be created. These arrays double in size each time they’re
created (2, 4, 8, 16) up to a certain point, and then they scale in
some smaller proportion.
in
de
x e
ou rro
to r
fr
an
ge
ap
pe
a ndi
sli ng
ce to
ap
ca pe
pa nd
cit ing
y
to bey
a on
sli d
ce

If we think that our slice might grow, we can set a capacity larger
than length. This gives our slice room to grow without golang
having to create a new underlying array every time our slice
grows. When the slice exceeds capacity, then a new underlying
array will be created. These arrays typically double in size each
time they’re created (2, 4, 8, 16).
ap
sli pe
ce nd
to in
a ga
sli
ce
notice the syntax
args...
a ap
sli p
ce en
to din
a g
sli
ce

notice the syntax


fro de
m let
a e
sli
ce
exercise
write a program that
creates a slice of ints using shorthand notation
then prints the slice
exercise
write a program that
creates a slice of strings using shorthand notation
then prints the slice
exercise
create a slice of ints using make;
set the length to 5 and capacity to 10
exercise
append a slice to a slice
exercise
delete an element from a slice
exercise
create a slice
then make your program throw an
“index out of range” error
map
key, value
maps
● basic info ● Maps are Reference Types
○ key:value ○ they behave like pointers
○ maps keys to values ○ when you pass a map variable to a
○ called “dictionaries” in some languages function
○ built into the language (not an additional ■ any changes to that mapped
library you must import) so they’re variable in the function
first-class citizens ■ change that original mapped
● Map Keys variable outside the function
○ need to be unique ● Maps are Not Thread Safe
○ the type used for a key needs to have the ○ best to avoid using maps concurrently
equality operator defined for it
■ the type must allow equals
comparisons
■ can’t use these types
● slice
● map
for sh
cre orth
ati and
ng
am
ap
ad
din
g
to an
a m en
ap try
Pri
nti
Pri ng m
nti
ng ap le
ma n
p
up
da
tin
ga
ne
ntr
y
de
let
ea
ne
ntr
y
ch
ec
co k fo
mm r e
a o xiste
k id nc
iom e
ch
ec
co k fo
mm r e
a o xiste
k id nc
iom e
ran
ge
loo
p
ma
a m ke
ap
exercise
create a program that:
● creates a map using shorthand notation
● adds an entry to the map
● changes an entry in the map
● deletes an entry in the map
● prints all of the entries in the map using range
● prints the len of the map
● uses the comma ok idiom
make vs new
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
○ initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
○ initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
○ initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
zero value
false for booleans, 0 for integers, 0.0 for floats, "" for strings
nil for pointers, functions, interfaces, slices, channels, and maps

https://golang.org/ref/spec#The_zero_value
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
○ initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
○ initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
○ initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
● initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”

● make
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
● initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
● initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
make vs new
● make ● new
○ slices, maps, channels ○ returns a pointer
○ allocates memory ■ newly allocated
● initializes ■ zeroed value
■ puts 0 or empty string into values ● “returns a pointer to a newly allocated
zeroed value of type T”
exercise
create a program that:
● declares a variable of type int using new
○ (note: this is not idiomatic go code to create an int this way)
● print out the memory address of the variable
● print out the value of the variable
● true or false:
○ new returned a pointer
exercise
create a program that:
● declares a variable of type string using new
○ (note: this is not idiomatic go code to create a string this way)
● print out the memory address of the variable
● print out the value of the variable
● true or false:
○ new returned a pointer
exercise
create a program that:
● declares a variable of type bool using new
○ (note: this is not idiomatic go code to create a bool this way)
● print out the memory address of the variable
● print out the value of the variable
● true or false:
○ new returned a pointer
exercise
create a program that:
● declares a variable of type[]int using make
● print out the value of the variable
● true or false:
○ make returned a pointer
exercise
create a program that:
● declares a variable of type map[int]string using make
● print out the value of the variable
● true or false:
○ make returned a pointer
exercise
What are the zeroed values for int, string, bool?
exercise
What are the zeroed values for slice and map
exercise
Explain the difference between make and new
struct
grouped fields
cr
an ea
d te
in a
iti st
al ru
iz ct
e
it
ac
ce
ss
do ing
t n fie
ot ld
at s
io us
n in
g
in
iti
of aliz
ty in
pe g
pe two
rs va
on ri
st abl
ru es
ct
in
i
na tial
m ize
in a
g
th var
e ia
fie bl
ld e
s
in
iti
a
om liz
itt e a
in v
g ar
fie ia
ld ble
s
vi
ew
in
g
of th
p1 e t
yp
e

We have created our own type


which is interesting to think about!
ta
ki
ng
p1 the
is ad
of d
ty res
pe s
*p of
er a
so str
n uc
t
● new
○ returns a pointer
■ newly allocated
■ zeroed value
● “returns a pointer to a newly allocated
zeroed value of type T”
question
Can we use make with a struct?
n els
n
c ha
d
an
no ps,
, ma
es
l i c
s

ma
kei
s fo r
question
Can we use make with a struct?
● make
○ slices, maps, channels
○ allocates memory
● initializes
■ puts 0 or empty string into values
exercise
create a program that:
● defines a struct type to hold customer info
● initialize two variables using that struct type
● uses dot-notation to print a field from each of the variables
● changes the value of one of the fields
● prints the changed field
exercise
Can you use new to create a variable of a struct type?
exercise
Can you use make to create a variable of a struct type?
Review
● slice vs. slicing vs. index access ● make
● slice ○ slices, maps, channels
○ list ○ allocates memory
○ mySlice := []int{1, 3, 5, 7, 9, 11} ● initializes
○ greeting := make([]string, 3, 5) ■ puts 0 or empty string into values
○ greeting = append(greeting, "Hello") ● new
○ mySlice = append(mySlice, myOtherSlice...) ○ returns a pointer
○ mySlice = append(mySlice[:2], mySlice[3:]...) ■ newly allocated
● map ■ zeroed value
○ key, value ● “returns a pointer to a newly allocated
■ key type, element type zeroed value of type T”
○ initializing ● struct
■ myMap := map[int]string{<entries>} ○ grouped fields
■ otras := make(map[string]string) ○ type person struct { name string age int }
○ adding new entry ○ p1 := person{name: "James"}
■ otras[“new key”] = “new value”
○ changing entry
■ otras[“new key”] = “newer value”
○ delete entry
■ delete(otras, “new key”)
● comma ok idiom
○ if val, ok := myGreeting[2]; ok {
fmt.Println("val: ", val)
fmt.Println("exists: ", exists)
}
Review Questions
slice
● Why would you want to use make when creating a slice?
slice vs map vs struct
● Describe the difference between the above data structures. Give an
example of data that would be stored in each type.

You might also like