golang_infinite_for

Golang infinite for Statement

Golang infinite for - Golang Infinite for Statement

The Infinite for Statement

The third for statement format does away with the condition, too. Go has a version of a for loop that loops forever. If you learned to program in the 1980s, your first program was probably an infinite loop in BASIC that printed HELLO to the screen forever:

10 PRINT “HELLO” 20 GOTO 10

Example 4-10 shows the Go version of this program. You can run it locally or try it out on The Go Playground.

Example 4-10. Infinite looping nostalgia

package main import “fmt” func main() { for { fmt.Println(“Hello”) } }

Running this program gives you the same output that filled the screens of millions of Commodore 64s and Apple ][s:

Hello Hello Hello Hello Hello Hello Hello …

Press Ctrl-C when you are tired of walking down memory lane.

Note

If you run this on The Go Playground, you’ll find that it will stop execution after a few seconds. As a shared resource, the playground doesn’t allow any one program to run for too long.

golang_infinite_for.txt · Last modified: 2025/02/01 06:54 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki