Press "Enter" to skip to content

Maps – Go Lang Practical Programming Tutorial p.14


what’s going on everybody welcome to
part 14 of our to go language to real
series in this tutorial what we’re going
to be talking about is the basics of
Maps so in the go programming language
if you want to store something in the
sort of you know key and values system
the way that you’re gonna do that is
with maps so let’s just go over some
basic examples this should be a pretty
quick tutorial and then we’ll actually
apply it to a real use case in the next
tutorial with our news aggregator web
app so to begin let’s just start we’re
just gonna have a fresh script here just
because it should be pretty simple I’m
gonna go ahead and import and actually
we’re just gonna use format so I’ll just
import format also yeah let’s – let’s
just do func main and then we’ll just do
everything in here so a typical map is
gonna be defined like so so like you
could say var grades or something like
that and then map and then this will be
a map containing basically strings or
basically it’ll be a string key and then
the value will be float32
okay for example so and actually in our
case well we’ll go with float 32 that’s
fine I’m probably gonna make floats but
anyway so this would be a grades map
right so in theory you know you might
have students names to their grades in
your class okay so now obviously we
don’t need this since we’re inside of a
function so you’d probably have
something more along the lines of grades
colon equals but also a map is just a
reference type so it actually doesn’t
have any values or anything like that if
you want to have it have values you need
to use goes make so you’re just going to
encase this in make and that’s actually
gonna go ahead and initialize it for you
and all that and then what we can do is
start to actually add values to it and
get values from and all that fun stuff
so let’s go ahead now that that’s done
what we can do is we can start adding
things and it’s just like a Python
dictionary so
basically the way that you do that is
you just say grades and then don’t
forget to do double quotes I always want
to do single close Timmy and yes that’s
his real name it’s like you know how
hipsters are doing it these days it’s
like they’re given like nicknames real
names so anyways yeah Timmy I don’t know
not my kid anyways he got 42 and guess
that nickname didn’t help him very much
in school and then let’s go ahead and
give a few more we’re gonna do Jess
again what’s going on here anyway Jess
got 92 so good good for Jess and then
finally let’s just add one more and then
let’s say this is Sam and Sam got a 67
okay so we have that and then now what
we can do is format dot print line we
can just we can print all of grades so
I’ll just go ahead and save that and
then we’ll bring this up go run go touch
go cool so as you can see here that’s
just the full map and now generally
you’re probably gonna like print out
your whole map but you can if you want
also what we can do is we can begin to
like we can take values and assign them
to specific variables so we could say
like I don’t know Tim’s grade oh you
know what we should probably do it this
way Tim’s Tim’s grade people are getting
angry so so we found that the style gods
from my tutorials so some people were
pointing out first of all like these
this would be you know your styles for
Python if you’re gonna give a variable
but in go first of all you probably
wanna capitalized exported most likely
and then yeah do like basically title
casing yeah cool also just for the
record you can also do go format so go
fmt and then your actual scripts and
then basically what this will do is
it’ll kind of like fix all your all your
style mistakes for you so if you’re if
you’re not if you’re someone like me who
doesn’t really pay much attention to it
maybe that’s your new best friend I
don’t know anyway back to the tutorial
oh sorry
Tim’s grade colon equals grades
Timmy and for that matter probably
grades should be capitalized to anyway
we’ll use it as it will assume that for
some reason we wanted it to be internal
so so so now we can do that and then we
could say yeah format print line let’s
print Tim’s braid
we’ll say that come back up here go run
go tight and so we got the full map
because we were printing that out but
also we got a 42 there
unfortunately because Tim was doing so
poorly in our class he’s been he’s been
dropped from the class basically we’re
pushing him back a grade it’s a really
sad situation but anyways if you want to
remove something you can just use the
delete syntax so just delete and then
delete from where we’re gonna delete
from grades and then what are we gonna
delete well we’re we’re deleting all
little Timmy
good bye Timmy and then what we can do
is let’s just cut and paste down here
we’ll save that and let’s run this one
more time go run go touch go so now as
you can see Tim is no longer with us
finally the last thing that we can do is
not have multiple cursors cool and what
we’re going to do now is iterate through
a map I almost called it a dictionary
anyway which is probably a common task
that you’re gonna need to do so when you
iterate through this is kind of
basically you’re gonna use the the range
keyword I’m pretty sure we’ve used range
already but yeah we have so before when
we used range it returned in index and a
value right and we just took the index
and we just used underscore basically
because if you wanted the index cool now
in the case of a dictionary a dictionary
already like if you wanted to iterate
over that dictionary in theory it could
are like in Python when you iterate over
it I said dictionary anyway
I was gonna do that map in Python if you
were to iterate over a dictionary you
would get back just like the key and
then if you wanted the value do the
dictionary key thing but if you iterate
over a map and go language
it will return you can return both the
key and the values so for example you
would say for K comma V so key value in
range grades we can iterate over that so
now we could just say format print line
K well we could just say well let’s do
[Music]
let’s do K sorry it’s under case k and
then colon comma V so it’ll be like the
student’s name and then their grade
something like that let’s go ahead and
come up here go run go type duck go cool
and then basically we’ve iterated over
it and then now yeah you’ve you’ve got
the the students name of the grade okay
pretty cool but obviously super simple
example the other thing that you’ll
probably notice in that we’re going to
kind of exemplify in the next tutorial
is like it doesn’t appear super simple
like how what if what if we wanted more
than just a float 32 here like what if
we wanted multiple values could we add
multiple values like for example could
we get away with
I don’t know throwing in you know a list
or a – you know like like a float 32
inch string or int and float 32 and so
on and no so so what if you do want to
have multiple values there well as
you’ve seen so far structs are your best
friend in golang so actually you would
just create your own type that might
have multiple values and that’s what you
passed there if you want to do that
which is something we are going to have
to do in the next tutorial so anyways if
you have questions comments concerns on
maps leaving below otherwise I will see
you in the next tutorial
Please follow and like us:

Be First to Comment

Leave a Reply