Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created April 12, 2014 17:20
Show Gist options
  • Save vaskoz/10546740 to your computer and use it in GitHub Desktop.
Save vaskoz/10546740 to your computer and use it in GitHub Desktop.
4 different Go For loops, but 2 don't use blocks the same was as the other 2. "for_blocks_fmt.go" is the gofmt version, but it breaks the code by its formatting because it changes the CForLoopNoPrePost example so that it doesn't compile if you add the newline as the original.
package main
// There are 4 FOR examples below, but they differ in how they require
// blocks to start.
import "fmt"
// CANNOT start block on next line
func CForLoop() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
// CAN start block on next line
func CForLoopNoPrePost() {
sum := 1
for ; sum < 1000;
{
sum += sum
}
fmt.Println(sum)
}
// CANNOT start block on next line
func GoWhileLoop() {
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
}
// CAN start block on next line
func GoInfiniteLoop() {
for
{
}
}
func main() {
CForLoop()
CForLoopNoPrePost()
GoWhileLoop()
// GoInfiniteLoop()
}
package main
// There are 4 FOR examples below, but they differ in how they require
// blocks to start.
import "fmt"
// CANNOT start block on next line
func CForLoop() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
// CAN start block on next line
func CForLoopNoPrePost() {
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
}
// CANNOT start block on next line
func GoWhileLoop() {
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
}
// CAN start block on next line
func GoInfiniteLoop() {
for {
}
}
func main() {
CForLoop()
CForLoopNoPrePost()
GoWhileLoop()
// GoInfiniteLoop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment