package main
import "fmt"
/*
* Count to ten in go.
*/
func main() {
const limit = 10
for i := 1; i <= limit; i++ {
fmt.Printf("%d ", i)
}
fmt.Printf("GO!\n")
}
This program simply prints the numbers from 1 to 10. Some notable things:
- In the const declaration, the type of the constant is
taken from the type of the value given it.
- The for loop looks a lot like C or Java, except
without parentheses.
- The curly braces on the loop body are required, even for
one statement. This is true for ifs and other control constructs
as well.
- The opening curly brace must be on the same line as the statement
header. (If you put it on the next line, the line break
acts like a semicolon.)
- The symbol = assigns a variable, as in C and Java. The
symbol := declares the variable of the same type as the
expression on the right.
Go allows the increment i++, but in Go it is classified as a
statement, not an expression. This eliminates some of the more painful
things allowed in C (and sometimes Java). There is no ++i.