字幕列表 影片播放 列印英文字幕 What's going on? Everybody Welcome to Port 12 of the go language programming tutorial Siri's in his video are really talking about is a looping in Goa. So it turns out, go much like it does have classes, 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 write either loop in the other. You know, you could write four loops and wild loops, and also you could have a wild loop that acts like a four loop and so on. So anyways, we only have a four loop. But even like your most basic for loop example is really a while loop. So, for example, let's say you've got your basic four loop, which would be like four I coul equals zero. So we're initializing I and I don't really think I've stressed it even up to this point. But when you like well, when you do find a variable for the very first time, your initializing that variable so you do colon equals. But later, if you wanted to re assign a value toe, I you would just need it and equals. You wouldn't have to reassign, Just for the record anyway, for I, colon zero I less than 10. So this is basically your wow. This is like, Okay, I is currently equal to zero. Wow, I is less than 10. What do we want to do? Let's just I increment. Okay, so that could be our four loop. And then you could say something like format dot print line. What's I not io What? His eye. So let's go ahead and run that real quick. Go run. Go to dot Go and you get, you know, basically zero. So, um, so that just kind of prints out all the values that 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 four loop. So this would be a typical con elect counter loop. Um, but most of the time, you're probably not writing four loops that look like this. And that's kind of my problem with, especially with language like go on, Lee talking about the basics because, um, your private writing four loops like this. So, for example, you'll probably have a variable that's already defined. S O he would really pry 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 incremental. I You're iterating. Probably through I most of the time you're going to be iterating like, Wait, what we have is a list of not a list, a slice of you or else, right? We wantto iterated through that we don't really necessarily want to do, because again, this is more like a wild loop while I is less than 10. But it can exist. So four I less than 10 we could print. Now, if we don't do anything eyes not incriminating, I would wind up. You know, this would be an infinite loop, for example. Um, So what we'll do is I plus plus, so that's gonna go ahead and increment. I bought one by one basically for us so we can run that again. It should be the same output. So just for the record, you could do plus plus, or you could say plus equals one 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 five, for that matter. Ropes made it too big. Where did you go? There we go. And then it's 05 done because it got to 10 before. Okay, So, uh, then you have, like, you could do I I know I mentioned an infinite loop, but if you wanted an influence in it in finite loop, uh, you could just do it this way. You stuff. Okay, so there's just no conditions, right? You don't even need I equals zero. So no conditions. You could just start the four loop, and it will just go forever. So it's the equivalent of a wild true. So, um, so there you have. There you have that. So the other thing is like, let's show some more examples of loops that arm or closely matched to reality. So maybe you'll have ex colony cools, um, five. And then maybe you would have a four loop. That's gonna just like a wow 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 four loop, which is just gonna mimic our wild for us. Let's go in for matt dot print line. Uh, do stuff, Comma X. Whatever X is, let's say explicit equals three. And then let's say if X is greater than 25 trying to use it on there, um, break. And so a break is a way to get out of whatever loop you are in there so would only break you out of that loop. So if if you have, like, nested bloops, it's only gonna break you out of that specific loop that urine. So anyways, let's go ahead and pull this up. Let's run that. Do we not increment x o? We didn't save. Okay, that's like, what's going on? Okay, try again. Cool. So do some 58 11 and so on all the way to 25 then it broke. So at least in this case, X is somewhat outside of this, um, of this. So another way that you could have written this, though, obviously is let me to here, so you could have just had X colon equals five ex less than 25 x plus equals three format 0.3 line do stuff X, and that should give us the exact same result. But even though, yeah, that's gonna give us the same results. 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 all these values and checks. Don't have to really be for X. Like, for example, like, what if we had a colon equals 34 X colon equals five. You know, you could have You could ask. Okay, Well, while a is less than 25 um, X plus X plus equals three. And then, you know, for matt dot print line stuffed with X and then also a plus for something like that. So you could have, um, multiple variables that are coming into play here. What? You're doing something to something else, and then, obviously, um, implementing the thing that's basically that Wow, I'll check that you're you're doing. Anyway, we could run over a ton of four 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 into is just copy and paste the XML code that we had up to this point because again, this is going to be much more realistic, especially when you're trying to generate over a data structure that you created, which is gonna be comin. So, uh, in this case, um, 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 Earl data and populates a slice with that data. And now we want iterated over that data. So, um, immediately, you're not gonna be able to just it rate over this data. It's not really gonna work that way. Um, so instead, what we're gonna say is, let's just comment out of print. We could say instead, four underscore, cause we're not going to use it. And then capital location, um, in basically, um, no, come on range of s dot locations, format, dots, print line. Uh, and then let's just print out. Um mmm. Percent s in the new line. Or maybe it should be new line percent. That's right. Look, let's do new line percent s. And then let's just say, um look it, Okay, Now let's run that and I'll talk about it in just a second, but it won't make sure it runs a little. Okay, so let me Oh, I see. So we just printed line. We need to actually print f That'll format it for us. Uh, now, let's do one. Right. Okay, so we really haven't talked much about that. But just just for the record, um, I want to do this. Uh, we could say, Like like, for example, format dot prions. Um, here some O r. It's kind of what we did with the web development example, except just with print line. It wouldn't work as well. Uh, let's just leave this real quick save. Come over here. Run right here is invariable. So that's how you can do string formatting. Basically, uh, anyway, bringing back her for a loop. That was the mistake I was making. Not using print f anyways. A CZ. You can see we're actually now iterating over and out putting 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 It's just gonna iterated over your structure and it returns. Basically two things that was going to return the index value and then whatever that actual value is So again, that doesn't look anything like, um, like your typical four loop. Obviously it is because basically, what's happened here is like, generally, you would have, um, this is no different than four. Um, I Colon equals five. What do we want to dio, right? That's That's what we've done. It's just so happens to be that rather than five, it's It's, uh, range on s locations and range returns to values. Okay, but it's still like if that's the first time you were running over this code, at least for me. Um, 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 gonna do. It just it will help you generate over your own kind of data structures 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 four loops to our code, the next thing that we're gonna be doing is we would need to visit the those site maps, get information on the articles, and we're probably not visit. The articles were so I just grabbed headlines or something like that to keep it as simple as possible. Um, but we'll see when we get there. Anyways, if you have questions, comments, concerns, whatever feel free living below. Otherwise, as always, I will see you in the next room.
B1 中級 循環--Go Lang實用編程教程第12頁 (Looping - Go Lang Practical Programming Tutorial p.12) 8 0 林宜悉 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字