Professional Documents
Culture Documents
echo
loop, decision, command line
12/05/11 echo – 1
12/05/11 echo – 2
echo/echo.go
package main
import (
"fmt"
"os" // Unix-like system access
)
func main() {
var s string
for i := 1; i < len(os.Args); i++ {
if i > 1 {
s += Space
darwin/freebsd/linux/nacl, amd64/386/arm5.
}
main.main starts execution.
s += os.Args[i]
}
gofmt manages source formatting.
fmt.Fprintln(os.Stdout, s)
}
12/05/11 echo – 3
package
import
package id;
12/05/11 echo – 4
http://golang.org/doc/go_spec.html#Packages
const
type
var
func
const id ,... type = value ,...;
const id ,... = value ,...;
type id type; // different type
12/05/11 echo – 5
http://golang.org/doc/go_spec.html#Declarations_and_scope
if
if simple-statement; condition { block } else if-or-block
if condition { block } else if-or-block
if simple-statement; condition { block } // else optional
if condition { block }
12/05/11 echo – 6
http://golang.org/doc/go_spec.html#If_statements
simple-‐statement
// empty statement(!)
No embedded assignment.
_ (blank identifier) ignores assigned value.
Shorthand can redeclare all but one id in the
same scope, cannot have package scope.
12/05/11 echo – 7
http://golang.org/doc/go_spec.html#SimpleStmt
for
for { block } // forever
for condition { block } // while
for init; condition; post { block } // can init, increment
for target ,... = range expression { block } // usually index, value
for id ,... := range expression { block } // block scope
12/05/11 echo – 8
http://golang.org/doc/go_spec.html#ForStmt
[]
len
type id [ length ] type // constant length is part of type
type id [ length ] [ length ] type // can cascade
len( array ) // number of elements
12/05/11 echo – 9
http://golang.org/doc/go_spec.html#Array_types
http://golang.org/pkg/builtin