Array Methods GoLang
Array Methods GoLang
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
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
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.