You are on page 1of 9

Go

Concurrent and Systems Programming

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
)

const Space = " "

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;

import id "path"; // id as prefix


import . "path"; // no prefix
import "path"; // path's package id as prefix
import _ "path"; // only import for init side effects

import ( spec; ... ); // multiple specs

package is first, establishes scope for


unexported things.
import is next, makes exported things
accessible, usually with a prefix. Scope is file.
Upper-case initial designates export.

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

var id ,... type = value ,...;


var id ,... = value ,...;

const|type|var ( spec; ... ); // multiple specs


func id ( parameters ) results { body } // recursion ok

Top-level scope is entire package.


Parameters’ and results’ scope is body.
Other scope is innermost block.
Shadowing is ok; no multiple declarations.

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 }

No parentheses for condition; block braces


required; no dangling else problem.
if creates scope: condition, block, and else.
No line break before block.
Alternative: switch.

12/05/11 echo – 6

http://golang.org/doc/go_spec.html#If_statements
simple-­‐statement
// empty statement(!)

expression // e.g., function call


target ,... = expression ,... // tuple assignment
target ,... = expression // ...of function result
target op= expression // single operation and assignment

target ++ // increment and decrement(!)


target --

id ,... := expression ,... // shorthand declaration

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

No parentheses for condition; block braces


required; only loop statement.
for creates scope: condition, post, and
block; init and post are simple statements.
range accesses values, not element references.
No line break before block.
There are (labeled) break and continue.

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

expression [ expression ] // indexed from zero


new([ length ] type ) // construction, constant length

[ length ] type { expression ,... } // array literal, can use index:


[ ... ] type { expression ,... }
[ ] type { expression ,... }

len is built-in and has other uses.


Strings are read-only arrays of UTF-8 bytes,
not characters. for/range traverses characters.
Slices are used to avoid copy.

12/05/11 echo – 9

http://golang.org/doc/go_spec.html#Array_types
http://golang.org/pkg/builtin

You might also like