Join David Gassner for an in-depth discussion in this video Creating loops with for statements, part of Learning Go.
- You can loop through your code in Go…using the for statement.…Unlike in C and Java, there is no while statement…but you can do the same sort of iterating of your code…with extended versions of for.…The most common for statement is just like in other…C-style languages with a three-part declaration…but you drop the parentheses around those three parts.…I'll demonstrate that down here by reusing the sum variable…that's at line 7 of this file.…I'll reset the value of sum to 0,…then I'll create a for loop.…
I'll start with a counter variable that I'll set to 0.…Then I'll set a condition of…i < 10,…and finally I'll increment it with i++.…Just like in C and Java, the variable you declare here…is local to the for loop.…Its scope will expire when the for loop is completed.…Within the for loop, I'll add the value of i to the sum…with sum += i.…And then at the end of the for loop,…I'll output the result with this code…which I'll copy from line 8.…
I'll run that code, and there's the result.…I get a sum of 45.…You can also loop through a slice or an array…
- Installing Go tools
- Creating and compiling a Go workspace
- Exploring variables, constants, and types
- Storing ordered and unordered values
- Grouping related values in structs
- Programming conditional logic and loops
- Defining and calling functions
- Handling errors
- Working with files
- Creating a simple HTTP server
Skill Level Beginner
1. Getting Started
2. Essential Go Skills
3. Managing Simple Values
4. Managing Complex Types and Collections
5. Managing Program Flow
6. Structuring Go Code
7. Working with Files and the Web
Next steps2m 26s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.