what's going on everybody welcome to
part 12 of the go language programming
tutorial series in this video we're
gonna be talking about is a looping in
go so it turns out go much like it
doesn't have classes it actually doesn't
have a wow loop
but if this isn't your first programming
language you're probably very much aware
that you could bright either loop in the
other you know you could write for loops
and while loops and also you could have
a while loop that acts like a for loop
and so on so anyways we only have a for
loop but even like your most basic for
loop example is really a while loop so
for example let's say you've got you
know your basic for loop which would be
like for I colon equals 0 so we're
initializing I and I don't really think
I've stressed it even up to this point
but when you it like we when you define
a variable for the very first time
you're initializing that variable so you
do colon equals but later if you wanted
to reassign a value to I you would just
need it an equals you wouldn't have to
reassign I just for the record anyway
for I colon 0 I less than 10 so this is
basically your wow this is like okay I
is currently equal to 0 Wow
I is less than 10 what do we want to do
let's just I increment okay so that
could be our for loop and then you could
say something like format dot print line
what's I not I Oh what is I so let's go
ahead and run that real quick go run go
tuck go and you get you know basically 0
to 9 so so that just kind of prints out
all the values that's just your you know
your hello world for loop example but
there's quite a few things that you can
do from here so for example not all
these things need to be included in your
for loop so this would be a typical kata
like counter loop but most of the time
you're probably not writing for loops
that look like this and that's kind of
my problem with especially with the
language like go only talking about the
basics because you're probably not
writing for loops like this so for
example you'll probably have a variable
that's already defined so you would
really probably have already initialized
I to be something or to be the result of
some function so that would be gone
right and then you're gonna probably
rather than incrementing I you're
iterating probably through I most of the
time you're going to be iterating like
what we have is a list of not a list a
slice of URLs right we want to iterate
through that we don't really necessarily
want to do because again this is more
like a while loop while I is less than
10 but it can exist so for I less than
10 we could print I now if we don't do
anything eyes not incrementing I would
wind up you know this would be an
infinite loop for example so what we'll
do is I plus plus so that's going to go
ahead and increment I bought 1 by 1
basically for us so we can run that
again this should be the same output so
just for the record you can do plus plus
or you could say plus equals 1 that
would do the exact same thing the
benefit of plus equals is that you could
do something else like you could do plus
equals 5 for that matter
whoops made too big where did it go
there we go
right and then it's just 0 5 done
because it got to 10 before okay
so then you have like you could do I
know I mentioned an infinite loop but if
you wanted an implement in it in finite
loop you could just do it this way do
stuff ok so there's just no conditions
right you don't need to need I equals 0
so no conditions you could just start
the for loop and it will just go forever
so it's the equivalent of a while true
so so there you have there you have that
so the other thing is like let's show
some more examples of loops that are
more closely matched to reality so maybe
you'll have X colon equals 5 and then
maybe you would have a for loop that's
gonna just like a while loop that's
gonna like so for example like a lot of
times you're gonna do something while
something is the case but that something
is outside of the loop sometimes so for
example let's just do like an infinite
for loop which is just gonna make our
while loop for us let's go ahead and
format dot print line
do stuff comma X whatever X is let's say
X plus equals three and then let's say
if X is greater than 25 trying to use
Python there break and so a break is a
way to get out of whatever loop you are
in there so it would only break you out
of that loop so if it's if you have like
nested loops it's only gonna break you
out of that specific loop that you're in
so anyways let's go ahead and pull this
up let's run that do we not increment X
oh we didn't save okay that's like
what's going on okay try get cool so do
step 5 8 11 and so on all the way to 25
and then it broke so at least in this
case X is somewhat outside of this of
this so another way that we that you you
could have written this though obviously
is let me here so you could have just
said X colon equals 5 X less than 25 X
plus equals 3 format dot print line
Deusto X and that should give us the
exact same result but even though yeah
that's gonna give us the same result
it's gonna look a lot cleaner chances
are you're not gonna write loops that
look like that that's just my my
estimation so another example is like
you know you wouldn't have like all
these values and checks don't have to
really be for X like for example like
what if we had a colon equals 3 for X
colon equals 5 you know you could have
you could ask okay well while a is less
than 25 X plus X plus equals 3 and then
you know format dot print line
you stuff with X and then also a plus
equals four something like that so you
could have multiple variables that are
coming into play here while you're doing
something to something else and then
obviously you know incrementing the
thing that's basically that Wow will
check that your that you're doing anyway
we could run over a ton of for loop
examples but I think the best thing to
do is to apply it to like a real a real
problem that you're having so what I'm
gonna go ahead and do is just copy and
paste the the XML code that we had up to
this point because again this is gonna
be much more realistic especially when
you're trying to iterate over a data
structure that you created which is
gonna be common so in this case we're
basically pulling that XML data and in
fact let's just run it really quickly
just to see what we're returning it just
visits the XML and pulls the URL data
and populates a slice with that data and
now we want to iterate over that data so
immediately you're not going to be able
to just iterate over this data it's not
really gonna work that way so instead
what we're gonna say is let's just
comment out the print line we could say
instead for underscore because we're not
going to use it in the capital location
in basically range of s dot locations
format dot print line and then let's
just print out percent s in the new line
or maybe it should be new line percent
ass that's pretty little thing let's do
new line percent s and then let's just
say location
okay now let's run that and I'll talk
about it in just a second I just wanna
make sure it runs okay so let me oh I
see it we've done so we're just printing
the line we need to actually print F
that will format it for us now let's do
a new line right okay so we really
haven't talked much about that but just
just for the record I don't know how I
want to do this we could say like like
for example format dot prints here some
[Music]
variable it's kind of like what we did
with the the web development example
except just with with print line it
wouldn't work as well so let's just
delete this real quick save come over
here
run right here are some variables so
that's how you can do string formatting
basically anyway bringing back our for
loop that was the mistake I was making
not using printf anyways as you can see
we're actually now iterating over and
outputting them these are string values
and all that so that's good so now let's
talk about the range function basically
what it's gonna do is it's just gonna
iterate over your structure and it
returns basically two things it's going
to return the index value and then
whatever that actual value is so again
that doesn't look anything like like
your typical for loop obviously it it is
because basically what's happened here
is like generally you'll you would have
this is no different than for I colon
equals five what do we want to do right
that's that that's what we've done it
just so happens to be that rather than
five its
it's range on s locations and range
returns to values okay
but it's still like if that's the first
time you're running over this code at
least for me it just wasn't that obvious
I don't know it just didn't look it
still looked pretty foreign to me so
anyways that's what range is going to do
it just it will help you iterate over
your own kind of data structures in and
also built-in data structures as well
and just know that just remember that
it's returning both index and the value
in our case that we're just calling it
location and then we can print out the
location okay so now that we've applied
for loops to our code the next thing
that we're going to be doing is we would
need to visit though those site Maps get
information on the articles and we'll
probably not visit the articles we'll
probably scrab like the headlines or
something like that to keep it as simple
as possible but we'll see when we get
there anyways if you have questions
comments concerns whatever feel free to
leave them below otherwise as always I
will see you in the next tutorial