Press "Enter" to skip to content

Hands-On Golang Exercise – Map Key Index

this hands-on exercise I want you to
create a map with a key of type string
which is a person’s last underscore
first name and a value of type slice of
string make that big two which stores
their favorite things store three
records in your map and I have a
possible data set right here which I’m
gonna copy for you print out all of the
values along with their index position
in the slice so pause the video right
now go do that a hands-on exercise in
the goin playground when you’ve finished
come back and restart this video or
unpause this video so continues playing
because now right now I’m gonna show you
the solution so let’s go to the golang
playground and first thing i’m going to
do is create a math using a composite
literal so short declaration operator
map it’s gonna have a key of string and
then a slice of string as the value and
then composite literal so there is the
type we have the type this is the type
and then we have the curly braces and we
put the values there so that’s a
composite literal the type curly braces
put the value right there so now I’m
going to come down here and put in the
values and those values are gonna be
like this
but I’ll do it like yeah that’s fine
I’ll do it like that command V and then
this will be a colon and then we need a
slice of string and that to make a new
composite literal and then it ends right
here and a comma and now we do the next
one and we have the key colon and then
the value and it’s gonna be a slice of
string and Miss Moneypenny likes James
Bond literature computer science because
she is awesome and then we’re gonna come
here and we’re gonna do doctor no and
dr. noha likes being evil ice cream and
sunsets it’s actually a fascinating guy
aside from that being evil part it’s got
some eclectic interests alright so there
we go there’s our map and let’s print it
out just make sure it all works sweet
there’s our map so now the next thing we
want to do is we want to capture our
all the values so I’m just gonna comment
that out so we keep our output a little
bit clean and we’re gonna do a four key
value : equals range over our mount and
we’re gonna get that all that stuff out
so here we could format print line and
we could say you know this is and the
key is going to be their name this is
the record for and then we’ll do key
right and then what we’ll do is four
will do another four range so for index
value to call on equals range and we’re
gonna range over this value which is
gonna be that slice of string right
because the key the key is associated
with a slice of string cool you know put
our curly braces in and they will bumped
print line and we’ll do a tab and then
index and value format at all and run it
be to declare do not use there we go
this is the record for doctor no being
evil ice cream sunsets this is the
record for bond James shaken not stirred
martinis in women yeah he is a
misogynist I agree this is the right and
that’s not a good thing we are all equal
this is a record for Moneypenny mess but
I grew up reading Ian Fleming novels
still fan of the movies this is a record
for Miss Moneypenny James Bond
literature in computer science
alright sweet so that’s an example for
hands-on exercise number seven number
eight hands-on exercise number eight
Please follow and like us:

Be First to Comment

Leave a Reply