Press "Enter" to skip to content

Mapping news data – Go Lang Practical Programming Tutorial p.15


hello hello and welcome to part 15 of
our go language tutorial series in this
tutorial what we’re gonna be doing is
using the mapping system that we’ve just
learned in the previous tutorial and
applying that to our news aggregator
application so at this point I’m going
back to the code for that news
aggregator app that we left off on
basically we’re to the point where we we
visited the the site map that contains a
bunch of site maps we’ve pulled those
locations to all the other site maps and
now we’re parsing all those site maps
basically and well at least we’re just
grabbing that information basically and
throwing it into end basically and
parsing out the titles keywords and
locations but at that point what we
probably like to do is store that in
some sort of map that we can then
iterate through and basically pass so so
that we can just pass just that map over
and iterate over that now it could be
the case that actually you could just
pass the news struct and then try to
iterate over that as well I just think
probably putting it into a map makes a
little bit more sense also if you were
gonna probably convert it to a JSON or
something like that it would make more
sense that way as well but in theory you
you could leave the news struct and then
iterate over that as well but that’s not
what we’re gonna do we’re gonna throw it
into map of keys and values and all that
I think it’ll be a little cleaner and
easier for us I think to iterate over
when we do pass it to our web app so
first of all what we’re going to go
ahead and do this since we talked about
the previous tutorial you’re not going
to be able to make a map to multiple
values you could only map you know to a
single type value so if you wanted to
have multiple values what you can do is
create a struct create your own type so
that’s what we’re gonna do now so we’re
going to just do it type and we’re going
to call this news map it’s going to be a
struct and this type will just contain
keyword which will be a string and then
it’s going to contain location which
will also be a string keyword should be
plural because it’s going to be but
we’ve got enough keywords I mean we
could do it I don’t think you’d wind up
in trouble
I’m just gonna leave it that way for now
anyway you might be thinking oh but
Harrison titles well what I’m gonna do
is I’m gonna make the key of the map the
title and then keyword and location will
be our values so that’s gonna be our
news map now we’re gonna go ahead and do
is come down into our main loop here and
then we’re just gonna add a new variable
here which will be the the news map
itself so we’re just going to say news
map colon equals and then that’s gonna
be make make what we’re gonna make a map
and that’ll be a map where the key is a
string and the values are news map
values so then what we’re gonna do is we
can
we’vewe’ve unmarshal this data to to our
news type here so we know we have all
that information and we we can iterate
over that information so and then as we
iterate over that information we can
store it into our map so that’s what
we’re gonna do now so basically we’re
just gonna start a new for loop so for
and then it’ll be the value basically
and then or for the index rather and
then or actually you probably want to do
sorry probably one of the incentives for
the index and then we don’t actually
care about the value here we’re gonna
say colon equals range and then we’re
just gonna do end keywords and so this
is gonna be kind of a hacky way to do
this there’s there’s probably a better
way to do it maybe some sort of like
range and a length of something
something like that but this should work
as well so because we’re just gonna grab
the the values and keywords so maybe it
would be even better to grab like rather
than keywords do like n dot titles maybe
like that might be more efficient and
then entering over them I don’t know I
actually don’t know if that’ll change or
anything
anyways all we really want to get is an
index value like that’s what I’m after
so then what we’re gonna say is oops
training that like a Python anyway I was
like why didn’t an indent
for me so now we’re going to say is news
underscore map because for this value
variable that we just defined up here so
news map and then the title will just be
n whoops
n dot titles which is that that slice of
titles so then to get a specific element
we can grab it with the index of that
element so we can just say I DX so news
map whatever title equals the news map
type that we just kind of that we just
created basically and in there what
we’re gonna say is n dot and then we’ll
do keywords keywords let me make sure
yet so keyword came first and then
location so n dot keywords ID X and then
n dot capital locations ID x cool and
then now what we could do is iterate
over iterate over the the the things
inside of our map like so basically once
we’ve gone through all the locations
like basically up to this point we
should have now news map which contains
all the data we’re interested in this
should contain the the titles right the
keywords to articles and the locations
of all the articles so if we wanted what
we’re more interested in doing is
displaying this on our web app but if we
wanted to iterate over this we could do
something like for ID X data : eat
whoops :
why is that keep happening : equals that
must be hitting the backspace key at the
same time range news map let’s go ahead
and format dot print line and then let’s
just add some new lines here this is do
three and then the index and then let’s
go ahead and format die
it’s a print line and then I’m just
gonna do two more so index should be the
title right and then we want to see the
keywords so let’s just do keyword or I’m
sorry what we need to do is data dot
keyword and then data dot location okay
let’s save that and let’s run that make
sure we didn’t screw anything up because
we probably did yeah index out of range
boy would I like a better error than
that it doesn’t even give me a line
number oh it does here 38 okay so here
keywords index let’s try
and titles let’s try keywords here you
rerun that real quick see if we run on
that same issue
somewhere yes so it’s definitely
something to do with our keywords like
it’s not getting populated so for range
and dot keyword n is our news okay so
our issue here is likely this capital K
that’s gonna be my guess
so I’m gonna switch that to lowercase K
because I don’t think it was a capital K
hopefully that’s our only issue let’s
see what happens
okay yeah so that was our issue so the
reason why first of all it was like so
when we passed n dot so we should be
able to change this back to titles and
that should work
basically titles was getting populated
but keywords wasn’t because it was
looking for a capital K tag and I wasn’t
finding it so it was populating titles
and locations but not keywords but then
what we were trying to do was reference
that specific index for a capital K
keywords tag not the variable that we’re
using here and it was like oh we don’t
have that so that’s why that was
throwing that error that was relatively
unhelpful anyway um I changed back to
title titles let’s try that one more
time
and then if that works we’ll be off –
okay cool okay so now that we have all
this information what we’re going to be
doing now is going back to our web
application information basically in
part of this tutorial because now we’re
getting close to being able to like hey
let’s actually put this up on our on our
web app so we’re going to be focusing
back on learning a little bit more about
web applications and go so if you have
any more questions or if you’ve got
comments tutorials tutorials if you have
any tutorials let us know any way
questions comments leaving below
otherwise I will see you in the next
tutorial
Please follow and like us:

Be First to Comment

Leave a Reply