Press "Enter" to skip to content

Maps – Go Lang Practical Programming Tutorial p.14


what’s going on everybody welcome to

what’s going on everybody welcome to part 14 of our to go language to real

part 14 of our to go language to real

part 14 of our to go language to real series in this tutorial what we’re going

series in this tutorial what we’re going

series in this tutorial what we’re going to be talking about is the basics of

to be talking about is the basics of

to be talking about is the basics of Maps so in the go programming language

Maps so in the go programming language

Maps so in the go programming language if you want to store something in the

if you want to store something in the

if you want to store something in the sort of you know key and values system

sort of you know key and values system

sort of you know key and values system the way that you’re gonna do that is

the way that you’re gonna do that is

the way that you’re gonna do that is with maps so let’s just go over some

with maps so let’s just go over some

with maps so let’s just go over some basic examples this should be a pretty

basic examples this should be a pretty

basic examples this should be a pretty quick tutorial and then we’ll actually

quick tutorial and then we’ll actually

quick tutorial and then we’ll actually apply it to a real use case in the next

apply it to a real use case in the next

apply it to a real use case in the next tutorial with our news aggregator web

tutorial with our news aggregator web

tutorial with our news aggregator web app so to begin let’s just start we’re

app so to begin let’s just start we’re

app so to begin let’s just start we’re just gonna have a fresh script here just

just gonna have a fresh script here just

just gonna have a fresh script here just because it should be pretty simple I’m

because it should be pretty simple I’m

because it should be pretty simple I’m gonna go ahead and import and actually

gonna go ahead and import and actually

gonna go ahead and import and actually we’re just gonna use format so I’ll just

we’re just gonna use format so I’ll just

we’re just gonna use format so I’ll just import format also yeah let’s – let’s

import format also yeah let’s – let’s

import format also yeah let’s – let’s just do func main and then we’ll just do

just do func main and then we’ll just do

just do func main and then we’ll just do everything in here so a typical map is

everything in here so a typical map is

everything in here so a typical map is gonna be defined like so so like you

gonna be defined like so so like you

gonna be defined like so so like you could say var grades or something like

could say var grades or something like

could say var grades or something like that and then map and then this will be

that and then map and then this will be

that and then map and then this will be a map containing basically strings or

a map containing basically strings or

a map containing basically strings or basically it’ll be a string key and then

basically it’ll be a string key and then

basically it’ll be a string key and then the value will be float32

the value will be float32

the value will be float32 okay for example so and actually in our

okay for example so and actually in our

okay for example so and actually in our case well we’ll go with float 32 that’s

case well we’ll go with float 32 that’s

case well we’ll go with float 32 that’s fine I’m probably gonna make floats but

fine I’m probably gonna make floats but

fine I’m probably gonna make floats but anyway so this would be a grades map

anyway so this would be a grades map

anyway so this would be a grades map right so in theory you know you might

right so in theory you know you might

right so in theory you know you might have students names to their grades in

have students names to their grades in

have students names to their grades in your class okay so now obviously we

your class okay so now obviously we

your class okay so now obviously we don’t need this since we’re inside of a

don’t need this since we’re inside of a

don’t need this since we’re inside of a function so you’d probably have

function so you’d probably have

function so you’d probably have something more along the lines of grades

something more along the lines of grades

something more along the lines of grades colon equals but also a map is just a

colon equals but also a map is just a

colon equals but also a map is just a reference type so it actually doesn’t

reference type so it actually doesn’t

reference type so it actually doesn’t have any values or anything like that if

have any values or anything like that if

have any values or anything like that if you want to have it have values you need

you want to have it have values you need

you want to have it have values you need to use goes make so you’re just going to

to use goes make so you’re just going to

to use goes make so you’re just going to encase this in make and that’s actually

encase this in make and that’s actually

encase this in make and that’s actually gonna go ahead and initialize it for you

gonna go ahead and initialize it for you

gonna go ahead and initialize it for you and all that and then what we can do is

and all that and then what we can do is

and all that and then what we can do is start to actually add values to it and

start to actually add values to it and

start to actually add values to it and get values from and all that fun stuff

get values from and all that fun stuff

get values from and all that fun stuff so let’s go ahead now that that’s done

so let’s go ahead now that that’s done

so let’s go ahead now that that’s done what we can do is we can start adding

what we can do is we can start adding

what we can do is we can start adding things and it’s just like a Python

things and it’s just like a Python

things and it’s just like a Python dictionary so

dictionary so

dictionary so basically the way that you do that is

basically the way that you do that is

basically the way that you do that is you just say grades and then don’t

you just say grades and then don’t

you just say grades and then don’t forget to do double quotes I always want

forget to do double quotes I always want

forget to do double quotes I always want to do single close Timmy and yes that’s

to do single close Timmy and yes that’s

to do single close Timmy and yes that’s his real name it’s like you know how

his real name it’s like you know how

his real name it’s like you know how hipsters are doing it these days it’s

hipsters are doing it these days it’s

hipsters are doing it these days it’s like they’re given like nicknames real

like they’re given like nicknames real

like they’re given like nicknames real names so anyways yeah Timmy I don’t know

names so anyways yeah Timmy I don’t know

names so anyways yeah Timmy I don’t know not my kid anyways he got 42 and guess

not my kid anyways he got 42 and guess

not my kid anyways he got 42 and guess that nickname didn’t help him very much

that nickname didn’t help him very much

that nickname didn’t help him very much in school and then let’s go ahead and

in school and then let’s go ahead and

in school and then let’s go ahead and give a few more we’re gonna do Jess

give a few more we’re gonna do Jess

give a few more we’re gonna do Jess again what’s going on here anyway Jess

again what’s going on here anyway Jess

again what’s going on here anyway Jess got 92 so good good for Jess and then

got 92 so good good for Jess and then

got 92 so good good for Jess and then finally let’s just add one more and then

finally let’s just add one more and then

finally let’s just add one more and then let’s say this is Sam and Sam got a 67

let’s say this is Sam and Sam got a 67

let’s say this is Sam and Sam got a 67 okay so we have that and then now what

okay so we have that and then now what

okay so we have that and then now what we can do is format dot print line we

we can do is format dot print line we

we can do is format dot print line we can just we can print all of grades so

can just we can print all of grades so

can just we can print all of grades so I’ll just go ahead and save that and

I’ll just go ahead and save that and

I’ll just go ahead and save that and then we’ll bring this up go run go touch

then we’ll bring this up go run go touch

then we’ll bring this up go run go touch go cool so as you can see here that’s

go cool so as you can see here that’s

go cool so as you can see here that’s just the full map and now generally

just the full map and now generally

just the full map and now generally you’re probably gonna like print out

you’re probably gonna like print out

you’re probably gonna like print out your whole map but you can if you want

your whole map but you can if you want

your whole map but you can if you want also what we can do is we can begin to

also what we can do is we can begin to

also what we can do is we can begin to like we can take values and assign them

like we can take values and assign them

like we can take values and assign them to specific variables so we could say

to specific variables so we could say

to specific variables so we could say like I don’t know Tim’s grade oh you

like I don’t know Tim’s grade oh you

like I don’t know Tim’s grade oh you know what we should probably do it this

know what we should probably do it this

know what we should probably do it this way Tim’s Tim’s grade people are getting

way Tim’s Tim’s grade people are getting

way Tim’s Tim’s grade people are getting angry so so we found that the style gods

angry so so we found that the style gods

angry so so we found that the style gods from my tutorials so some people were

from my tutorials so some people were

from my tutorials so some people were pointing out first of all like these

pointing out first of all like these

pointing out first of all like these this would be you know your styles for

this would be you know your styles for

this would be you know your styles for Python if you’re gonna give a variable

Python if you’re gonna give a variable

Python if you’re gonna give a variable but in go first of all you probably

but in go first of all you probably

but in go first of all you probably wanna capitalized exported most likely

wanna capitalized exported most likely

wanna capitalized exported most likely and then yeah do like basically title

and then yeah do like basically title

and then yeah do like basically title casing yeah cool also just for the

casing yeah cool also just for the

casing yeah cool also just for the record you can also do go format so go

record you can also do go format so go

record you can also do go format so go fmt and then your actual scripts and

fmt and then your actual scripts and

fmt and then your actual scripts and then basically what this will do is

then basically what this will do is

then basically what this will do is it’ll kind of like fix all your all your

it’ll kind of like fix all your all your

it’ll kind of like fix all your all your style mistakes for you so if you’re if

style mistakes for you so if you’re if

style mistakes for you so if you’re if you’re not if you’re someone like me who

you’re not if you’re someone like me who

you’re not if you’re someone like me who doesn’t really pay much attention to it

doesn’t really pay much attention to it

doesn’t really pay much attention to it maybe that’s your new best friend I

maybe that’s your new best friend I

maybe that’s your new best friend I don’t know anyway back to the tutorial

don’t know anyway back to the tutorial

don’t know anyway back to the tutorial oh sorry

oh sorry

oh sorry Tim’s grade colon equals grades

Tim’s grade colon equals grades

Tim’s grade colon equals grades Timmy and for that matter probably

Timmy and for that matter probably

Timmy and for that matter probably grades should be capitalized to anyway

grades should be capitalized to anyway

grades should be capitalized to anyway we’ll use it as it will assume that for

we’ll use it as it will assume that for

we’ll use it as it will assume that for some reason we wanted it to be internal

some reason we wanted it to be internal

some reason we wanted it to be internal so so so now we can do that and then we

so so so now we can do that and then we

so so so now we can do that and then we could say yeah format print line let’s

could say yeah format print line let’s

could say yeah format print line let’s print Tim’s braid

print Tim’s braid

print Tim’s braid we’ll say that come back up here go run

we’ll say that come back up here go run

we’ll say that come back up here go run go tight and so we got the full map

go tight and so we got the full map

go tight and so we got the full map because we were printing that out but

because we were printing that out but

because we were printing that out but also we got a 42 there

also we got a 42 there

also we got a 42 there unfortunately because Tim was doing so

unfortunately because Tim was doing so

unfortunately because Tim was doing so poorly in our class he’s been he’s been

poorly in our class he’s been he’s been

poorly in our class he’s been he’s been dropped from the class basically we’re

dropped from the class basically we’re

dropped from the class basically we’re pushing him back a grade it’s a really

pushing him back a grade it’s a really

pushing him back a grade it’s a really sad situation but anyways if you want to

sad situation but anyways if you want to

sad situation but anyways if you want to remove something you can just use the

remove something you can just use the

remove something you can just use the delete syntax so just delete and then

delete syntax so just delete and then

delete syntax so just delete and then delete from where we’re gonna delete

delete from where we’re gonna delete

delete from where we’re gonna delete from grades and then what are we gonna

from grades and then what are we gonna

from grades and then what are we gonna delete well we’re we’re deleting all

delete well we’re we’re deleting all

delete well we’re we’re deleting all little Timmy

little Timmy

little Timmy good bye Timmy and then what we can do

good bye Timmy and then what we can do

good bye Timmy and then what we can do is let’s just cut and paste down here

is let’s just cut and paste down here

is let’s just cut and paste down here we’ll save that and let’s run this one

we’ll save that and let’s run this one

we’ll save that and let’s run this one more time go run go touch go so now as

more time go run go touch go so now as

more time go run go touch go so now as you can see Tim is no longer with us

you can see Tim is no longer with us

you can see Tim is no longer with us finally the last thing that we can do is

finally the last thing that we can do is

finally the last thing that we can do is not have multiple cursors cool and what

not have multiple cursors cool and what

not have multiple cursors cool and what we’re going to do now is iterate through

we’re going to do now is iterate through

we’re going to do now is iterate through a map I almost called it a dictionary

a map I almost called it a dictionary

a map I almost called it a dictionary anyway which is probably a common task

anyway which is probably a common task

anyway which is probably a common task that you’re gonna need to do so when you

that you’re gonna need to do so when you

that you’re gonna need to do so when you iterate through this is kind of

iterate through this is kind of

iterate through this is kind of basically you’re gonna use the the range

basically you’re gonna use the the range

basically you’re gonna use the the range keyword I’m pretty sure we’ve used range

keyword I’m pretty sure we’ve used range

keyword I’m pretty sure we’ve used range already but yeah we have so before when

already but yeah we have so before when

already but yeah we have so before when we used range it returned in index and a

we used range it returned in index and a

we used range it returned in index and a value right and we just took the index

value right and we just took the index

value right and we just took the index and we just used underscore basically

and we just used underscore basically

and we just used underscore basically because if you wanted the index cool now

because if you wanted the index cool now

because if you wanted the index cool now in the case of a dictionary a dictionary

in the case of a dictionary a dictionary

in the case of a dictionary a dictionary already like if you wanted to iterate

already like if you wanted to iterate

already like if you wanted to iterate over that dictionary in theory it could

over that dictionary in theory it could

over that dictionary in theory it could are like in Python when you iterate over

are like in Python when you iterate over

are like in Python when you iterate over it I said dictionary anyway

it I said dictionary anyway

it I said dictionary anyway I was gonna do that map in Python if you

I was gonna do that map in Python if you

I was gonna do that map in Python if you were to iterate over a dictionary you

were to iterate over a dictionary you

were to iterate over a dictionary you would get back just like the key and

would get back just like the key and

would get back just like the key and then if you wanted the value do the

then if you wanted the value do the

then if you wanted the value do the dictionary key thing but if you iterate

dictionary key thing but if you iterate

dictionary key thing but if you iterate over a map and go language

over a map and go language

over a map and go language it will return you can return both the

it will return you can return both the

it will return you can return both the key and the values so for example you

key and the values so for example you

key and the values so for example you would say for K comma V so key value in

would say for K comma V so key value in

would say for K comma V so key value in range grades we can iterate over that so

range grades we can iterate over that so

range grades we can iterate over that so now we could just say format print line

now we could just say format print line

now we could just say format print line K well we could just say well let’s do

K well we could just say well let’s do

K well we could just say well let’s do [Music]

[Music]

[Music] let’s do K sorry it’s under case k and

let’s do K sorry it’s under case k and

let’s do K sorry it’s under case k and then colon comma V so it’ll be like the

then colon comma V so it’ll be like the

then colon comma V so it’ll be like the student’s name and then their grade

student’s name and then their grade

student’s name and then their grade something like that let’s go ahead and

something like that let’s go ahead and

something like that let’s go ahead and come up here go run go type duck go cool

come up here go run go type duck go cool

come up here go run go type duck go cool and then basically we’ve iterated over

and then basically we’ve iterated over

and then basically we’ve iterated over it and then now yeah you’ve you’ve got

it and then now yeah you’ve you’ve got

it and then now yeah you’ve you’ve got the the students name of the grade okay

the the students name of the grade okay

the the students name of the grade okay pretty cool but obviously super simple

pretty cool but obviously super simple

pretty cool but obviously super simple example the other thing that you’ll

example the other thing that you’ll

example the other thing that you’ll probably notice in that we’re going to

probably notice in that we’re going to

probably notice in that we’re going to kind of exemplify in the next tutorial

kind of exemplify in the next tutorial

kind of exemplify in the next tutorial is like it doesn’t appear super simple

is like it doesn’t appear super simple

is like it doesn’t appear super simple like how what if what if we wanted more

like how what if what if we wanted more

like how what if what if we wanted more than just a float 32 here like what if

than just a float 32 here like what if

than just a float 32 here like what if we wanted multiple values could we add

we wanted multiple values could we add

we wanted multiple values could we add multiple values like for example could

multiple values like for example could

multiple values like for example could we get away with

we get away with

we get away with I don’t know throwing in you know a list

I don’t know throwing in you know a list

I don’t know throwing in you know a list or a – you know like like a float 32

or a – you know like like a float 32

or a – you know like like a float 32 inch string or int and float 32 and so

inch string or int and float 32 and so

inch string or int and float 32 and so on and no so so what if you do want to

on and no so so what if you do want to

on and no so so what if you do want to have multiple values there well as

have multiple values there well as

have multiple values there well as you’ve seen so far structs are your best

you’ve seen so far structs are your best

you’ve seen so far structs are your best friend in golang so actually you would

friend in golang so actually you would

friend in golang so actually you would just create your own type that might

just create your own type that might

just create your own type that might have multiple values and that’s what you

have multiple values and that’s what you

have multiple values and that’s what you passed there if you want to do that

passed there if you want to do that

passed there if you want to do that which is something we are going to have

which is something we are going to have

which is something we are going to have to do in the next tutorial so anyways if

to do in the next tutorial so anyways if

to do in the next tutorial so anyways if you have questions comments concerns on

you have questions comments concerns on

you have questions comments concerns on maps leaving below otherwise I will see

maps leaving below otherwise I will see

maps leaving below otherwise I will see you in the next tutorial

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *