Press "Enter" to skip to content

Looping – Go Lang Practical Programming Tutorial p.12


what’s going on everybody welcome to

what’s going on everybody welcome to part 12 of the go language programming

part 12 of the go language programming

part 12 of the go language programming tutorial series in this video we’re

tutorial series in this video we’re

tutorial series in this video we’re gonna be talking about is a looping in

gonna be talking about is a looping in

gonna be talking about is a looping in go so it turns out go much like it

go so it turns out go much like it

go so it turns out go much like it doesn’t have classes it actually doesn’t

doesn’t have classes it actually doesn’t

doesn’t have classes it actually doesn’t have a wow loop

have a wow loop

have a wow loop but if this isn’t your first programming

but if this isn’t your first programming

but if this isn’t your first programming language you’re probably very much aware

language you’re probably very much aware

language you’re probably very much aware that you could bright either loop in the

that you could bright either loop in the

that you could bright either loop in the other you know you could write for loops

other you know you could write for loops

other you know you could write for loops and while loops and also you could have

and while loops and also you could have

and while loops and also you could have a while loop that acts like a for loop

a while loop that acts like a for loop

a while loop that acts like a for loop and so on so anyways we only have a for

and so on so anyways we only have a for

and so on so anyways we only have a for loop but even like your most basic for

loop but even like your most basic for

loop but even like your most basic for loop example is really a while loop so

loop example is really a while loop so

loop example is really a while loop so for example let’s say you’ve got you

for example let’s say you’ve got you

for example let’s say you’ve got you know your basic for loop which would be

know your basic for loop which would be

know your basic for loop which would be like for I colon equals 0 so we’re

like for I colon equals 0 so we’re

like for I colon equals 0 so we’re initializing I and I don’t really think

initializing I and I don’t really think

initializing I and I don’t really think I’ve stressed it even up to this point

I’ve stressed it even up to this point

I’ve stressed it even up to this point but when you it like we when you define

but when you it like we when you define

but when you it like we when you define a variable for the very first time

a variable for the very first time

a variable for the very first time you’re initializing that variable so you

you’re initializing that variable so you

you’re initializing that variable so you do colon equals but later if you wanted

do colon equals but later if you wanted

do colon equals but later if you wanted to reassign a value to I you would just

to reassign a value to I you would just

to reassign a value to I you would just need it an equals you wouldn’t have to

need it an equals you wouldn’t have to

need it an equals you wouldn’t have to reassign I just for the record anyway

reassign I just for the record anyway

reassign I just for the record anyway for I colon 0 I less than 10 so this is

for I colon 0 I less than 10 so this is

for I colon 0 I less than 10 so this is basically your wow this is like okay I

basically your wow this is like okay I

basically your wow this is like okay I is currently equal to 0 Wow

is currently equal to 0 Wow

is currently equal to 0 Wow I is less than 10 what do we want to do

I is less than 10 what do we want to do

I is less than 10 what do we want to do let’s just I increment okay so that

let’s just I increment okay so that

let’s just I increment okay so that could be our for loop and then you could

could be our for loop and then you could

could be our for loop and then you could say something like format dot print line

say something like format dot print line

say something like format dot print line what’s I not I Oh what is I so let’s go

what’s I not I Oh what is I so let’s go

what’s I not I Oh what is I so let’s go ahead and run that real quick go run go

ahead and run that real quick go run go

ahead and run that real quick go run go tuck go and you get you know basically 0

tuck go and you get you know basically 0

tuck go and you get you know basically 0 to 9 so so that just kind of prints out

to 9 so so that just kind of prints out

to 9 so so that just kind of prints out all the values that’s just your you know

all the values that’s just your you know

all the values that’s just your you know your hello world for loop example but

your hello world for loop example but

your hello world for loop example but there’s quite a few things that you can

there’s quite a few things that you can

there’s quite a few things that you can do from here so for example not all

do from here so for example not all

do from here so for example not all these things need to be included in your

these things need to be included in your

these things need to be included in your for loop so this would be a typical kata

for loop so this would be a typical kata

for loop so this would be a typical kata like counter loop but most of the time

like counter loop but most of the time

like counter loop but most of the time you’re probably not writing for loops

you’re probably not writing for loops

you’re probably not writing for loops that look like this and that’s kind of

that look like this and that’s kind of

that look like this and that’s kind of my problem with especially with the

my problem with especially with the

my problem with especially with the language like go only talking about the

language like go only talking about the

language like go only talking about the basics because you’re probably not

basics because you’re probably not

basics because you’re probably not writing for loops like this so for

writing for loops like this so for

writing for loops like this so for example you’ll probably have a variable

example you’ll probably have a variable

example you’ll probably have a variable that’s already defined so you would

that’s already defined so you would

that’s already defined so you would really probably have already initialized

really probably have already initialized

really probably have already initialized I to be something or to be the result of

I to be something or to be the result of

I to be something or to be the result of some function so that would be gone

some function so that would be gone

some function so that would be gone right and then you’re gonna probably

right and then you’re gonna probably

right and then you’re gonna probably rather than incrementing I you’re

rather than incrementing I you’re

rather than incrementing I you’re iterating probably through I most of the

iterating probably through I most of the

iterating probably through I most of the time you’re going to be iterating like

time you’re going to be iterating like

time you’re going to be iterating like what we have is a list of not a list a

what we have is a list of not a list a

what we have is a list of not a list a slice of URLs right we want to iterate

slice of URLs right we want to iterate

slice of URLs right we want to iterate through that we don’t really necessarily

through that we don’t really necessarily

through that we don’t really necessarily want to do because again this is more

want to do because again this is more

want to do because again this is more like a while loop while I is less than

like a while loop while I is less than

like a while loop while I is less than 10 but it can exist so for I less than

10 but it can exist so for I less than

10 but it can exist so for I less than 10 we could print I now if we don’t do

10 we could print I now if we don’t do

10 we could print I now if we don’t do anything eyes not incrementing I would

anything eyes not incrementing I would

anything eyes not incrementing I would wind up you know this would be an

wind up you know this would be an

wind up you know this would be an infinite loop for example so what we’ll

infinite loop for example so what we’ll

infinite loop for example so what we’ll do is I plus plus so that’s going to go

do is I plus plus so that’s going to go

do is I plus plus so that’s going to go ahead and increment I bought 1 by 1

ahead and increment I bought 1 by 1

ahead and increment I bought 1 by 1 basically for us so we can run that

basically for us so we can run that

basically for us so we can run that again this should be the same output so

again this should be the same output so

again this should be the same output so just for the record you can do plus plus

just for the record you can do plus plus

just for the record you can do plus plus or you could say plus equals 1 that

or you could say plus equals 1 that

or you could say plus equals 1 that would do the exact same thing the

would do the exact same thing the

would do the exact same thing the benefit of plus equals is that you could

benefit of plus equals is that you could

benefit of plus equals is that you could do something else like you could do plus

do something else like you could do plus

do something else like you could do plus equals 5 for that matter

equals 5 for that matter

equals 5 for that matter whoops made too big where did it go

whoops made too big where did it go

whoops made too big where did it go there we go

there we go

there we go right and then it’s just 0 5 done

right and then it’s just 0 5 done

right and then it’s just 0 5 done because it got to 10 before okay

because it got to 10 before okay

because it got to 10 before okay so then you have like you could do I

so then you have like you could do I

so then you have like you could do I know I mentioned an infinite loop but if

know I mentioned an infinite loop but if

know I mentioned an infinite loop but if you wanted an implement in it in finite

you wanted an implement in it in finite

you wanted an implement in it in finite loop you could just do it this way do

loop you could just do it this way do

loop you could just do it this way do stuff ok so there’s just no conditions

stuff ok so there’s just no conditions

stuff ok so there’s just no conditions right you don’t need to need I equals 0

right you don’t need to need I equals 0

right you don’t need to need I equals 0 so no conditions you could just start

so no conditions you could just start

so no conditions you could just start the for loop and it will just go forever

the for loop and it will just go forever

the for loop and it will just go forever so it’s the equivalent of a while true

so it’s the equivalent of a while true

so it’s the equivalent of a while true so so there you have there you have that

so so there you have there you have that

so so there you have there you have that so the other thing is like let’s show

so the other thing is like let’s show

so the other thing is like let’s show some more examples of loops that are

some more examples of loops that are

some more examples of loops that are more closely matched to reality so maybe

more closely matched to reality so maybe

more closely matched to reality so maybe you’ll have X colon equals 5 and then

you’ll have X colon equals 5 and then

you’ll have X colon equals 5 and then maybe you would have a for loop that’s

maybe you would have a for loop that’s

maybe you would have a for loop that’s gonna just like a while loop that’s

gonna just like a while loop that’s

gonna just like a while loop that’s gonna like so for example like a lot of

gonna like so for example like a lot of

gonna like so for example like a lot of times you’re gonna do something while

times you’re gonna do something while

times you’re gonna do something while something is the case but that something

something is the case but that something

something is the case but that something is outside of the loop sometimes so for

is outside of the loop sometimes so for

is outside of the loop sometimes so for example let’s just do like an infinite

example let’s just do like an infinite

example let’s just do like an infinite for loop which is just gonna make our

for loop which is just gonna make our

for loop which is just gonna make our while loop for us let’s go ahead and

while loop for us let’s go ahead and

while loop for us let’s go ahead and format dot print line

do stuff comma X whatever X is let’s say

do stuff comma X whatever X is let’s say X plus equals three and then let’s say

X plus equals three and then let’s say

X plus equals three and then let’s say if X is greater than 25 trying to use

if X is greater than 25 trying to use

if X is greater than 25 trying to use Python there break and so a break is a

Python there break and so a break is a

Python there break and so a break is a way to get out of whatever loop you are

way to get out of whatever loop you are

way to get out of whatever loop you are in there so it would only break you out

in there so it would only break you out

in there so it would only break you out of that loop so if it’s if you have like

of that loop so if it’s if you have like

of that loop so if it’s if you have like nested loops it’s only gonna break you

nested loops it’s only gonna break you

nested loops it’s only gonna break you out of that specific loop that you’re in

out of that specific loop that you’re in

out of that specific loop that you’re in so anyways let’s go ahead and pull this

so anyways let’s go ahead and pull this

so anyways let’s go ahead and pull this up let’s run that do we not increment X

up let’s run that do we not increment X

up let’s run that do we not increment X oh we didn’t save okay that’s like

oh we didn’t save okay that’s like

oh we didn’t save okay that’s like what’s going on okay try get cool so do

what’s going on okay try get cool so do

what’s going on okay try get cool so do step 5 8 11 and so on all the way to 25

step 5 8 11 and so on all the way to 25

step 5 8 11 and so on all the way to 25 and then it broke so at least in this

and then it broke so at least in this

and then it broke so at least in this case X is somewhat outside of this of

case X is somewhat outside of this of

case X is somewhat outside of this of this so another way that we that you you

this so another way that we that you you

this so another way that we that you you could have written this though obviously

could have written this though obviously

could have written this though obviously is let me here so you could have just

is let me here so you could have just

is let me here so you could have just said X colon equals 5 X less than 25 X

said X colon equals 5 X less than 25 X

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

Deusto X and that should give us the exact same result but even though yeah

exact same result but even though yeah

exact same result but even though yeah that’s gonna give us the same result

that’s gonna give us the same result

that’s gonna give us the same result it’s gonna look a lot cleaner chances

it’s gonna look a lot cleaner chances

it’s gonna look a lot cleaner chances are you’re not gonna write loops that

are you’re not gonna write loops that

are you’re not gonna write loops that look like that that’s just my my

look like that that’s just my my

look like that that’s just my my estimation so another example is like

estimation so another example is like

estimation so another example is like you know you wouldn’t have like all

you know you wouldn’t have like all

you know you wouldn’t have like all these values and checks don’t have to

these values and checks don’t have to

these values and checks don’t have to really be for X like for example like

really be for X like for example like

really be for X like for example like what if we had a colon equals 3 for X

what if we had a colon equals 3 for X

what if we had a colon equals 3 for X colon equals 5 you know you could have

colon equals 5 you know you could have

colon equals 5 you know you could have you could ask okay well while a is less

you could ask okay well while a is less

you could ask okay well while a is less than 25 X plus X plus equals 3 and then

than 25 X plus X plus equals 3 and then

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

you stuff with X and then also a plus equals four something like that so you

equals four something like that so you

equals four something like that so you could have multiple variables that are

could have multiple variables that are

could have multiple variables that are coming into play here while you’re doing

coming into play here while you’re doing

coming into play here while you’re doing something to something else and then

something to something else and then

something to something else and then obviously you know incrementing the

obviously you know incrementing the

obviously you know incrementing the thing that’s basically that Wow will

thing that’s basically that Wow will

thing that’s basically that Wow will check that your that you’re doing anyway

check that your that you’re doing anyway

check that your that you’re doing anyway we could run over a ton of for loop

we could run over a ton of for loop

we could run over a ton of for loop examples but I think the best thing to

examples but I think the best thing to

examples but I think the best thing to do is to apply it to like a real a real

do is to apply it to like a real a real

do is to apply it to like a real a real problem that you’re having so what I’m

problem that you’re having so what I’m

problem that you’re having so what I’m gonna go ahead and do is just copy and

gonna go ahead and do is just copy and

gonna go ahead and do is just copy and paste the the XML code that we had up to

paste the the XML code that we had up to

paste the the XML code that we had up to this point because again this is gonna

this point because again this is gonna

this point because again this is gonna be much more realistic especially when

be much more realistic especially when

be much more realistic especially when you’re trying to iterate over a data

you’re trying to iterate over a data

you’re trying to iterate over a data structure that you created which is

structure that you created which is

structure that you created which is gonna be common so in this case we’re

gonna be common so in this case we’re

gonna be common so in this case we’re basically pulling that XML data and in

basically pulling that XML data and in

basically pulling that XML data and in fact let’s just run it really quickly

fact let’s just run it really quickly

fact let’s just run it really quickly just to see what we’re returning it just

just to see what we’re returning it just

just to see what we’re returning it just visits the XML and pulls the URL data

visits the XML and pulls the URL data

visits the XML and pulls the URL data and populates a slice with that data and

and populates a slice with that data and

and populates a slice with that data and now we want to iterate over that data so

now we want to iterate over that data so

now we want to iterate over that data so immediately you’re not going to be able

immediately you’re not going to be able

immediately you’re not going to be able to just iterate over this data it’s not

to just iterate over this data it’s not

to just iterate over this data it’s not really gonna work that way so instead

really gonna work that way so instead

really gonna work that way so instead what we’re gonna say is let’s just

what we’re gonna say is let’s just

what we’re gonna say is let’s just comment out the print line we could say

comment out the print line we could say

comment out the print line we could say instead for underscore because we’re not

instead for underscore because we’re not

instead for underscore because we’re not going to use it in the capital location

going to use it in the capital location

going to use it in the capital location in basically range of s dot locations

in basically range of s dot locations

in basically range of s dot locations format dot print line and then let’s

format dot print line and then let’s

format dot print line and then let’s just print out percent s in the new line

just print out percent s in the new line

just print out percent s in the new line or maybe it should be new line percent

or maybe it should be new line percent

or maybe it should be new line percent ass that’s pretty little thing let’s do

ass that’s pretty little thing let’s do

ass that’s pretty little thing let’s do new line percent s and then let’s just

new line percent s and then let’s just

new line percent s and then let’s just say location

say location

say location okay now let’s run that and I’ll talk

okay now let’s run that and I’ll talk

okay now let’s run that and I’ll talk about it in just a second I just wanna

about it in just a second I just wanna

about it in just a second I just wanna make sure it runs okay so let me oh I

make sure it runs okay so let me oh I

make sure it runs okay so let me oh I see it we’ve done so we’re just printing

see it we’ve done so we’re just printing

see it we’ve done so we’re just printing the line we need to actually print F

the line we need to actually print F

the line we need to actually print F that will format it for us now let’s do

that will format it for us now let’s do

that will format it for us now let’s do a new line right okay so we really

a new line right okay so we really

a new line right okay so we really haven’t talked much about that but just

haven’t talked much about that but just

haven’t talked much about that but just just for the record I don’t know how I

just for the record I don’t know how I

just for the record I don’t know how I want to do this we could say like like

want to do this we could say like like

want to do this we could say like like for example format dot prints here some

variable it’s kind of like what we did

variable it’s kind of like what we did with the the web development example

with the the web development example

with the the web development example except just with with print line it

except just with with print line it

except just with with print line it wouldn’t work as well so let’s just

wouldn’t work as well so let’s just

wouldn’t work as well so let’s just delete this real quick save come over

delete this real quick save come over

delete this real quick save come over here

here

here run right here are some variables so

run right here are some variables so

run right here are some variables so that’s how you can do string formatting

that’s how you can do string formatting

that’s how you can do string formatting basically anyway bringing back our for

basically anyway bringing back our for

basically anyway bringing back our for loop that was the mistake I was making

loop that was the mistake I was making

loop that was the mistake I was making not using printf anyways as you can see

not using printf anyways as you can see

not using printf anyways as you can see we’re actually now iterating over and

we’re actually now iterating over and

we’re actually now iterating over and outputting them these are string values

outputting them these are string values

outputting them these are string values and all that so that’s good so now let’s

and all that so that’s good so now let’s

and all that so that’s good so now let’s talk about the range function basically

talk about the range function basically

talk about the range function basically what it’s gonna do is it’s just gonna

what it’s gonna do is it’s just gonna

what it’s gonna do is it’s just gonna iterate over your structure and it

iterate over your structure and it

iterate over your structure and it returns basically two things it’s going

returns basically two things it’s going

returns basically two things it’s going to return the index value and then

to return the index value and then

to return the index value and then whatever that actual value is so again

whatever that actual value is so again

whatever that actual value is so again that doesn’t look anything like like

that doesn’t look anything like like

that doesn’t look anything like like your typical for loop obviously it it is

your typical for loop obviously it it is

your typical for loop obviously it it is because basically what’s happened here

because basically what’s happened here

because basically what’s happened here is like generally you’ll you would have

is like generally you’ll you would have

is like generally you’ll you would have this is no different than for I colon

this is no different than for I colon

this is no different than for I colon equals five what do we want to do right

equals five what do we want to do right

equals five what do we want to do right that’s that that’s what we’ve done it

that’s that that’s what we’ve done it

that’s that that’s what we’ve done it just so happens to be that rather than

just so happens to be that rather than

just so happens to be that rather than five its

five its

five its it’s range on s locations and range

it’s range on s locations and range

it’s range on s locations and range returns to values okay

returns to values okay

returns to values okay but it’s still like if that’s the first

but it’s still like if that’s the first

but it’s still like if that’s the first time you’re running over this code at

time you’re running over this code at

time you’re running over this code at least for me it just wasn’t that obvious

least for me it just wasn’t that obvious

least for me it just wasn’t that obvious I don’t know it just didn’t look it

I don’t know it just didn’t look it

I don’t know it just didn’t look it still looked pretty foreign to me so

still looked pretty foreign to me so

still looked pretty foreign to me so anyways that’s what range is going to do

anyways that’s what range is going to do

anyways that’s what range is going to do it just it will help you iterate over

it just it will help you iterate over

it just it will help you iterate over your own kind of data structures in and

your own kind of data structures in and

your own kind of data structures in and also built-in data structures as well

also built-in data structures as well

also built-in data structures as well and just know that just remember that

and just know that just remember that

and just know that just remember that it’s returning both index and the value

it’s returning both index and the value

it’s returning both index and the value in our case that we’re just calling it

in our case that we’re just calling it

in our case that we’re just calling it location and then we can print out the

location and then we can print out the

location and then we can print out the location okay so now that we’ve applied

location okay so now that we’ve applied

location okay so now that we’ve applied for loops to our code the next thing

for loops to our code the next thing

for loops to our code the next thing that we’re going to be doing is we would

that we’re going to be doing is we would

that we’re going to be doing is we would need to visit though those site Maps get

need to visit though those site Maps get

need to visit though those site Maps get information on the articles and we’ll

information on the articles and we’ll

information on the articles and we’ll probably not visit the articles we’ll

probably not visit the articles we’ll

probably not visit the articles we’ll probably scrab like the headlines or

probably scrab like the headlines or

probably scrab like the headlines or something like that to keep it as simple

something like that to keep it as simple

something like that to keep it as simple as possible but we’ll see when we get

as possible but we’ll see when we get

as possible but we’ll see when we get there anyways if you have questions

there anyways if you have questions

there anyways if you have questions comments concerns whatever feel free to

comments concerns whatever feel free to

comments concerns whatever feel free to leave them below otherwise as always I

leave them below otherwise as always I

leave them below otherwise as always 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 *