2012/goforc: Slides for "Go for C programmers" talk.

R=r
CC=golang-dev
https://golang.org/cl/6848048
diff --git a/2012/goforc.slide b/2012/goforc.slide
new file mode 100644
index 0000000..b480767
--- /dev/null
+++ b/2012/goforc.slide
@@ -0,0 +1,726 @@
+Go for C programmers
+ACCU, Silicon Valley Chapter, Nov. 14, 2012
+
+# Go is a general-purpose programming language developed at Google.
+# Go's design is influenced by the C and Pascal lineage, but instead of providing
+# a conglomerate of features, Go takes a fresh look at the essential mechanisms
+# required for modern programming tasks. As a result, Go has evolved to a compact
+# language of surprising power. In this presentation we give a brief introduction
+# to Go, slightly slanted towards programmers with a C/C++ background, and then
+# talk about particularly interesting aspects of Go.
+
+Robert Griesemer
+Google Inc.
+gri@golang.org
+
+* Organization of this talk
+
+Part 1: Introduction to Go
+
+Break
+
+Part 2: Object-oriented and concurrent programming in Go
+
+# if time permits and desire exists
+
+* Motivation
+
+* Have
+
+Incomprehensible and unsafe code.
+
+Extremely slow builds.
+
+Missing concurrency support.
+
+Outdated tools.
+
+# C, C++ are > 30 years old!
+# Java is 15 years old, but not really a systems-programming language.
+# (No control over data layout, no unsigned, verbose, etc.)
+
+* Want
+
+Readable, safe, and efficient code.
+
+A build system that scales.
+
+Good concurrency support.
+
+Tools that can operate at Google-scale.
+
+# A language for the 21. century!
+
+* Meet Go
+
+Compact, yet expressive
+# spec is ~50 pages
+
+Statically typed and garbage collected
+
+Object- but not type-oriented
+
+Strong concurrency support
+
+Efficient implementation
+
+Rich standard library
+
+Fast compilers
+
+Scalable tools
+
+* Hello, World
+
+.play goforc/hello.go
+
+# the canonical first program
+# note: immediately readable for a C programmer even without prior knowledge of Go
+# (not the case with C++'s Hello, World, which is using the << operator)
+# which bring us to the first item on the "Want" list: readable code.
+
+* Syntax
+
+* Syntax
+
+_Syntax_is_not_important..._-_unless_you_are_a_programmer._
+Rob Pike.
+
+_The_readability_of_programs_is_immeasurably_more_important_than_their_writeability._
+Hints on Programming Language Design
+C. A. R. Hoare 1973
+
+# A lot of effort has gone into honing Go's syntax over more than 3 years.
+# The result is a clutter- and stutter-free, compact, yet readable notation.
+# Consider some alternatives...
+
+* Too verbose
+
+	scoped_ptr<logStats::LogStats>
+		logStats(logStats::LogStats::NewLogStats(FLAGS_logStats, logStats::LogStats::kFIFO));
+
+# real example from C++ (2012) (names changed)
+# logStats mentioned 9 times!
+# too much clutter
+
+* Too dense
+
+	(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) =>
+		if (ps(x)) ps -- (x * x to n by x) else ps))
+
+# prime sieve in Scala
+# pretty and short but incomprehensible
+
+* Just right
+
+	t := time.Now()
+	switch {
+	case t.Hour() < 12:
+	    return "morning"
+	case t.Hour() < 18:
+	    return "afternoon"
+	default:
+	    return "evening"
+	}
+
+# looks like pseudo code
+# readable even w/o knowing Go
+# lots of things are going on but not relevant for understanding
+# no clutter
+
+* Reading Go code
+
+* Packages
+
+A Go program consists of _packages_.
+
+A _package_ consists of one or more _source_files_ (`.go` files).
+
+Each _source_file_ starts with a _package_clause_ followed by declarations.
+
+.code goforc/hello.go
+
+By convention, all files belonging to a single package are located in a single directory.
+
+* Declarations, "Pascal-style" (left to right)
+
+Pattern: keyword names [type] [initializer]
+
+.code goforc/decls.go /START/,/STOP/
+
+* Why?
+
+	p, q *Point
+
+	func adder(delta int) func(x int) int
+
+# try this in C
+# composition of types straight-forward
+
+* Constants
+
+In Go, constants are _mathematically_precise_.
+
+There is no need for qualifiers (`-42LL`, `7UL`, etc.)
+
+.code goforc/consts.go /START1/,/STOP1/
+
+Only when used, constants are truncated to size:
+
+.play goforc/consts.go /START2/,/STOP2/
+
+Huge win in readability and ease of use.
+
+* Types
+
+Familiar:
+- Basic types, arrays, structs, pointers, functions.
+
+But:
+- `string` is a basic type.
+- No automatic conversion of basic types in expressions.
+- No pointer arithmetic; pointers and arrays are different.
+- A function type represents a function; context and all.
+
+New:
+- _Slices_ instead of array pointers + separate length: `[]int`
+- _Maps_ because everybody needs them: `map[string]int`
+- _Interfaces_ for polymorphism: `interface{}`
+- _Channels_ for communication between goroutines: `chan` `int`
+
+* Variables
+
+Familiar:
+
+.code goforc/vars.go /START1/,/STOP1/
+
+New: Type can be inferred from type of initializing expression.
+
+.code goforc/vars.go /START2/,/STOP2/
+
+Shortcut (inside functions):
+
+.code goforc/vars.go /i :=/
+
+It is _safe_ to take the address of _any_ variable:
+
+.code goforc/vars.go /return/
+
+* Functions
+
+Functions may have multiple return values:
+
+	func atoi(s string) (i int, err error)
+
+Functions are first-class citizens (_closures_):
+
+.code goforc/adder.go /START1/,/STOP1/
+.play goforc/adder.go /START2/,/STOP2/
+
+* Statements
+
+.code goforc/stmts.go /START/,/STOP/
+
+* Go statements for C programmers
+
+Cleanups:
+
+- No semicolons
+- Multiple assignments
+- `++` and `--` are statements
+- No parentheses around conditions; curly braces mandatory
+- Implicit `break` in `switch`; explicit `fallthrough`
+
+New:
+
+- `for` `range`
+- type `switch`
+- `go`, `select`
+- `defer`
+
+* Assignments
+
+Assignments may assign multiple values simultaneously:
+
+	a, b = x, y
+
+Equivalent to:
+
+	t1 := x
+	t2 := y
+	a = t1
+	b = t2
+
+For instance:
+
+	a, b = b, a       // swap a and b
+	i, err = atoi(s)  // assign results of atoi
+	i, _ = atoi(991)  // discard 2nd value
+
+* Switch statements
+
+Switch statements may have multiple case values, and `break` is implicit:
+
+	switch day {
+	case 1, 2, 3, 4, 5:
+		tag = "workday"
+	case 0, 6:
+		tag = "weekend"
+	default:
+		tag = "invalid"
+	}
+
+The case values don't have to be constants:
+
+	switch {
+	case day < 0 || day > 6:
+		tag = "invalid"
+	case day == 0 || day == 6:
+		tag = "weekend"
+	default:
+		tag = "workday"
+	}
+
+* For loops
+
+.code goforc/forloop.go /START1/,/STOP1/
+
+A `range` clause permits easy iteration over arrays and slices:
+
+.play goforc/forloop.go /START3/,/STOP3/
+
+Unused values are discarded by assigning to the blank (_) identifier:
+
+.code goforc/forloop.go /START2/,/STOP2/
+
+* Dependencies
+
+* Dependencies in Go
+
+An _import_declaration_ is used to express a _dependency_ on another package:
+
+	import "net/rpc"
+
+Here, the importing package depends on the Go package "rpc".
+
+The _import_path_ ("net/rpc") uniquely identifies a package; multiple packages may have the same name, but they all reside at different locations (directories).
+
+By convention, the package name matches the last element of the import path (here: "rpc").
+
+_Exported_ functionality of the `rpc` package is available via the _package_qualifier_ (`rpc`):
+
+	rpc.Call
+
+A Go import declaration takes the place of a C _include_.
+
+* Naming: An excursion
+
+How names work in a programming language is critical to readability.
+Scopes control how names work.
+Go has very simple scope hierarchy:
+
+- universe
+- package
+- file (for imports only)
+- function
+- block
+
+* Locality of names
+
+Upper case names are _exported_: `Name` _vs._ `name`.
+
+The package qualifier is always present for imported names.
+
+(First component of) every name is always declared in the current package.
+
+One of the best (and hardest!) decisions made in Go.
+
+* Locality scales
+
+No surprises when importing:
+
+- Adding an exported name to my package cannot break your package!
+
+Names do not leak across boundaries.
+
+In C, C++, Java the name `y` could refer to anything.
+In Go, `y` (or even `Y`) is always defined within the package.
+In Go, `x.Y` is clear: find `x` locally, `Y` belongs to it, and there is only one such `Y`.
+
+Immediate consequences for readability.
+
+* Back to imports
+
+Importing a package means reading a package's exported API.
+
+This export information is self-contained. For instance:
+
+- A imports B
+- B imports C
+- B exports contain references to C
+
+B's export data contains all necessary information about C. There is no need for a compiler to read the export data of C.
+
+This has huge consequences for build times!
+
+* Dependencies in C
+
+`.h` files are not self-contained.
+
+As a result, a compiler ends up reading core header files over and over again.
+
+`ifdef` still requires the preprocessor to read a lot of code.
+
+No wonder it takes a long time to compile...
+
+At Google scale: dependencies explode, are exponential, become almost non-computable.
+
+A large Google C++ build can read the same header file tens of thousands of times!
+
+# it is real work, too, teaching the compiler what a string is each time
+
+* Tools
+
+* A brief overview
+
+Two compilers: gc, gccgo
+
+Support for multiple platforms: x86 (32/64bit), ARM (32/64), Linux, BSD, OS X, ...
+
+Automatic formatting of source code: `gofmt`
+
+Automatic documentation extraction: `godoc`
+
+Automatic API adaption: `gofix`
+
+All (and more!) integrated into the `go` command.
+
+* Building a Go program
+
+A Go program can be compiled and linked without additional build information (`make` files, etc.).
+
+By convention, all files belonging to a package are found in the same directory.
+
+All depending packages are found by inspecting the import paths of the top-most (_main_) package.
+
+A single integrated tool takes care of compiling individual files or entire systems.
+
+* The go command
+
+Usage:
+
+	go command [arguments]
+
+Selected commands:
+
+	build       compile packages and dependencies
+	clean       remove object files
+	doc         run godoc on package sources
+	fix         run go tool fix on packages
+	fmt         run gofmt on package sources
+	get         download and install packages and dependencies
+	install     compile and install packages and dependencies
+	list        list packages
+	run         compile and run Go program
+	test        test packages
+	vet         run go tool vet on packages
+
+.link http://golang.org/cmd/go/
+
+* Break
+
+# After the break, we will discuss OOP and concurrency in Go.
+
+* Object-oriented programming
+
+* What is object-oriented programming?
+
+"Object-oriented programming (OOP) is a programming paradigm using _objects_ – usually instances of a class – consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. Many modern programming languages now support forms of OOP, at least as an option."
+
+(Wikipedia)
+
+# Important:
+# Notion of _objects_
+# _Objects_ have _state_ (data fields), and _methods_ (for interactions)
+
+* OOP requires very little extra programming language support
+
+We only need
+
+- the notion of an _Object_,
+- a mechanism to interact with them (_Methods_),
+- and support for polymorphism (_Interfaces_).
+
+Claim: Data abstraction, enscapsulation, and modularity are mechanisms
+in their own right, not OOP specific, and a modern language (OO or not)
+should have support for them independently.
+
+
+* Object-oriented programming in Go
+
+Methods without classes
+
+Interfaces without hierarchies
+
+Code reuse without inheritance
+
+Specifically:
+
+- Any _value_ can be an _object_
+- Any _type_ can play the role of a _class_
+- _Methods_ can be attached to any _type_
+- _Interfaces_ implement polymorphism.
+
+* Methods
+
+.play goforc/point.go
+
+* Methods can be attached to any type
+
+.play goforc/celsius.go
+
+* Interfaces
+
+	type Stringer interface {
+	     String() string
+	}
+
+	type Reader interface {
+	     Read(p []byte) (n int, err error)
+	}
+
+	type Writer interface {
+	     Write(p []byte) (n int, err error)
+	}
+
+	type Empty interface{}
+
+An interface defines a set of methods.
+A type that implements all methods of an interface is said to implement the interface.
+All types implement the empty interface interface{}.
+
+* Dynamic dispatch
+
+.play goforc/interface.go /START/,/STOP/
+
+A value (here: `corner`, `boiling`) of a type (`Point`, `Celsius`) that implements
+an interface (`Stringer`) can be assigned to a variable (`v`) of that interface type.
+
+* Composition and chaining
+
+Typically, interfaces are small (1-3 methods).
+
+Pervasive use of key interfaces in the standard library make it easy to chain APIs together.
+
+	package io
+	func Copy(dst Writer, src Reader) (int64, error)
+
+The io.Copy function copies by reading from any Reader and writing to any Writer.
+
+Interfaces are often introduced ad-hoc, and after the fact.
+
+There is no explicit hierarchy and thus no need to design one!
+
+* cat
+
+.code goforc/cat.go
+
+# An `os.File` implements the `Reader` interface;
+# `os.Stdout` implements the `Writer` interface.
+
+* Interfaces in practice
+
+Methods on any types and _ad_hoc_ interfaces make for a light-weight OO programming style.
+
+Go interfaces enable post-facto abstraction.
+# Existing code may not know about a new interface
+
+No explicit type hierarchies.
+# Coming up with the correct type hierarchy is hard
+# They are often wrong
+# They are difficult and time-consuming to change
+
+"Plug'n play" in a type-safe way.
+
+# There's much more but we won't have time for more detail.
+
+* Concurrency
+
+* What is concurrency?
+
+Concurrency is the composition of independently executing computations.
+
+Concurrency is a way to structure software, particularly as a way to write clean code that interacts well with the real world.
+
+It is not parallelism.
+
+* Concurrency is not parallelism
+
+Concurrency is not parallelism, although it enables parallelism.
+
+If you have only one processor, your program can still be concurrent  but it cannot be parallel.
+
+On the other hand, a well-written concurrent program might run efficiently in parallel on a multiprocessor. That property could be important...
+
+For more on that distinction, see the link below. Too much to discuss here.
+
+.link http://golang.org/s/concurrency-is-not-parallelism
+
+* A model for software construction
+
+Easy to understand.
+
+Easy to use.
+
+Easy to reason about.
+
+You don't need to be an expert!
+
+(Much nicer than dealing with the minutiae of parallelism (threads, semaphores, locks, barriers, etc.))
+
+There is a long history behind Go's concurrency features, going back to Hoare's CSP in 1978 and even Dijkstra's guarded commands (1975).
+
+* Basic Examples
+
+* A boring function
+
+We need an example to show the interesting properties of the concurrency primitives.
+
+To avoid distraction, we make it a boring example.
+
+.code goforc/example0.go /START1/,/STOP1/
+.play goforc/example0.go /START2/,/STOP2/
+
+* Ignoring it
+
+The `go` statement runs the function as usual, but doesn't make the caller wait.
+
+It launches a goroutine.
+
+The functionality is analogous to the & on the end of a shell command.
+
+.play goforc/example1.go /START/,/STOP/
+
+* Ignoring it a little less
+
+When main returns, the program exits and takes the function `f` down with it.
+
+We can hang around a little, and on the way show that both main and the launched goroutine are running.
+
+.play goforc/example2.go /START/,/STOP/
+
+* Goroutines
+
+What is a goroutine? It's an independently executing function, launched by a `go` statement.
+
+It has its own call stack, which grows and shrinks as required.
+
+It's very cheap. It's practical to have thousands, even hundreds of thousands of goroutines.
+
+It's not a thread.
+
+There might be only one thread in a program with thousands of goroutines.
+
+Instead, goroutines are multiplexed dynamically onto threads as needed to keep all the goroutines running.
+
+But if you think of it as a very cheap thread, you won't be far off.
+
+* Channels
+
+A channel in Go provides a connection between two goroutines, allowing them to communicate.
+
+.code goforc/channels.go /START1/,/STOP1/
+.code goforc/channels.go /START2/,/STOP2/
+.code goforc/channels.go /START3/,/STOP3/
+
+* Using channels
+
+A channel connects the `main` and `f` goroutines so they can communicate.
+
+.play goforc/communication1.go /START1/,/STOP1/
+.code goforc/communication1.go /START2/,/STOP2/
+
+* Synchronization
+
+When the main function executes `<–c`, it will wait for a value to be sent.
+
+Similarly, when the function `f` executes `c<–value`, it waits for a receiver to be ready.
+
+A sender and receiver must both be ready to play their part in the communication. Otherwise we wait until they are.
+
+Thus channels both communicate and synchronize.
+
+Channels can be unbuffered or buffered.
+
+* Using channels between many goroutines
+
+.play goforc/communication2.go /START1/,/STOP1/
+
+A single channel may be used to communicate between many (not just two) goroutines; many goroutines may communicate via one or multiple channels.
+
+This enables a rich variety of concurrency patterns.
+
+# There's enough material for several talks on this subject alone.
+
+* Elements of a work-stealing scheduler
+
+.code goforc/worker1.go /START2/,/STOP2/
+
+The `worker` uses two channels to communicate:
+- The `in` channel waits for some work order.
+- The `out` channel communicates the result.
+- As work load, a worker (very slowly) computes the list of prime factors for a given order.
+
+* A matching producer and consumer
+
+.code goforc/worker1.go /START3/,/STOP3/
+
+The `producer` produces and endless supply of work orders and sends them `out`.
+
+The `consumer` receives `n` results from the `in` channel and then terminates.
+
+* Putting it all together
+
+.play goforc/worker1.go /START1/,/STOP1/
+
+We use one worker to handle the entire work load.
+
+Because there is only one worker, we see the result coming back in order.
+
+This is running rather slow...
+
+* Using 10 workers
+
+.play goforc/worker2.go /START1/,/STOP1/
+
+A ready worker will read the next order from the `in` channel and start working on it. Another ready worker will proceed with the next order, and so forth.
+
+Because we have many workers and since different orders take different amounts of time to work on, we see the results coming back out-of-order.
+
+On a multi-core system, many workers may truly run in parallel.
+
+This is running much faster...
+
+* The Go approach
+
+Don't communicate by sharing memory, share memory by communicating.
+
+* There's much more...
+
+* Links
+
+Go Home Page:
+
+.link http://golang.org
+
+Go Tour (learn Go in your browser)
+
+.link http://tour.golang.org
+
+Package documentation:
+
+.link http://golang.org/pkg
+
+Articles galore:
+
+.link http://golang.org/doc
diff --git a/2012/goforc/adder.go b/2012/goforc/adder.go
new file mode 100644
index 0000000..2386e5e
--- /dev/null
+++ b/2012/goforc/adder.go
@@ -0,0 +1,21 @@
+package main
+
+import "fmt"
+
+// START1 OMIT
+func adder(delta int) func(x int) int {
+	f := func(x int) int { // HL
+		return x + delta // HL
+	} // HL
+	return f
+}
+
+// STOP1 OMIT
+
+func main() {
+	// START2 OMIT
+	var inc = adder(1)
+	fmt.Println(inc(0))
+	fmt.Println(adder(-1)(10))
+	// STOP2 OMIT
+}
diff --git a/2012/goforc/cat.go b/2012/goforc/cat.go
new file mode 100644
index 0000000..579fd09
--- /dev/null
+++ b/2012/goforc/cat.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+	"flag"
+	"io"
+	"os"
+)
+
+func main() {
+	flag.Parse()
+	for _, arg := range flag.Args() {
+		f, err := os.Open(arg)
+		if err != nil {
+			panic(err)
+		}
+		defer f.Close()
+		_, err = io.Copy(os.Stdout, f) // HL
+		if err != nil {
+			panic(err)
+		}
+	}
+}
diff --git a/2012/goforc/celsius.go b/2012/goforc/celsius.go
new file mode 100644
index 0000000..988ca7d
--- /dev/null
+++ b/2012/goforc/celsius.go
@@ -0,0 +1,17 @@
+package main
+
+import "fmt"
+
+type Celsius float32
+type Fahrenheit float32
+
+func (t Celsius) String() string           { return fmt.Sprintf("%g°C", t) }
+func (t Fahrenheit) String() string        { return fmt.Sprintf("%g°F", t) }
+func (t Celsius) ToFahrenheit() Fahrenheit { return Fahrenheit(t*9/5 + 32) }
+
+func main() {
+	var t Celsius = 21
+	fmt.Println(t.String())
+	fmt.Println(t)
+	fmt.Println(t.ToFahrenheit())
+}
diff --git a/2012/goforc/channels.go b/2012/goforc/channels.go
new file mode 100644
index 0000000..94d0558
--- /dev/null
+++ b/2012/goforc/channels.go
@@ -0,0 +1,26 @@
+package main
+
+func main() {
+	var value int
+
+	// START1 OMIT
+	// Declaring and initializing.
+	var c chan int
+	c = make(chan int)
+	// or
+	c := make(chan int) // HL
+	// STOP1 OMIT
+
+	// START2 OMIT
+	// Sending on a channel.
+	c <- 1 // HL
+	// STOP2 OMIT
+
+	// START3 OMIT
+	// Receiving from a channel.
+	// The "arrow" indicates the direction of data flow.
+	value = <-c // HL
+	// STOP3 OMIT
+
+	_ = value
+}
diff --git a/2012/goforc/communication1.go b/2012/goforc/communication1.go
new file mode 100644
index 0000000..b327404
--- /dev/null
+++ b/2012/goforc/communication1.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+	"fmt"
+	"time"
+)
+
+// START1 OMIT
+func main() {
+	c := make(chan string)
+	go f("three", 300*time.Millisecond, c)
+	for i := 0; i < 10; i++ {
+		fmt.Println("Received", <-c) // Receive expression is just a value. // HL
+	}
+	fmt.Println("Done.")
+}
+
+// STOP1 OMIT
+
+// START2 OMIT
+func f(msg string, delay time.Duration, c chan string) {
+	for i := 0; ; i++ {
+		c <- fmt.Sprintf("%s %d", msg, i) // Any suitable value can be sent. // HL
+		time.Sleep(delay)
+	}
+}
+
+// STOP2 OMIT
diff --git a/2012/goforc/communication2.go b/2012/goforc/communication2.go
new file mode 100644
index 0000000..10fec78
--- /dev/null
+++ b/2012/goforc/communication2.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+	"fmt"
+	"time"
+)
+
+// START1 OMIT
+func main() {
+	c := make(chan string)
+	go f("three", 300*time.Millisecond, c) // HL
+	go f("six", 600*time.Millisecond, c)   // HL
+	go f("nine", 900*time.Millisecond, c)  // HL
+	for i := 0; i < 10; i++ {
+		fmt.Println("Received", <-c)
+	}
+	fmt.Println("Done.")
+}
+
+// STOP1 OMIT
+
+// START2 OMIT
+func f(msg string, delay time.Duration, c chan string) {
+	for i := 0; ; i++ {
+		c <- fmt.Sprintf("%s %d", msg, i) // Any suitable value can be sent. // HL
+		time.Sleep(delay)
+	}
+}
+
+// STOP2 OMIT
diff --git a/2012/goforc/consts.go b/2012/goforc/consts.go
new file mode 100644
index 0000000..6404795
--- /dev/null
+++ b/2012/goforc/consts.go
@@ -0,0 +1,24 @@
+package main
+
+import "fmt"
+
+// START1 OMIT
+const (
+	MaxUInt = 1<<64 - 1
+	Pi      = 3.14159265358979323846264338327950288419716939937510582097494459
+	Pi2     = Pi * Pi
+	Delta   = 2.0
+)
+
+// STOP1 OMIT
+
+func main() {
+	// START2 OMIT
+	var x uint64 = MaxUInt
+	var pi2 float32 = Pi2
+	var delta int = Delta
+	// STOP2 OMIT
+	fmt.Println("x =", x)
+	fmt.Println("pi2 =", pi2)
+	fmt.Println("delta =", delta)
+}
diff --git a/2012/goforc/decls.go b/2012/goforc/decls.go
new file mode 100644
index 0000000..74f019b
--- /dev/null
+++ b/2012/goforc/decls.go
@@ -0,0 +1,23 @@
+package main
+
+// START OMIT
+import "fmt"
+
+const digits = "0123456789abcdef"
+
+type Point struct {
+	x, y int
+	tag  string
+}
+
+var s [32]byte
+
+var msgs = []string{"Hello, 世界", "Ciao, Mondo"}
+
+func itoa(x, base int) string
+
+// STOP OMIT
+
+func main() {
+	fmt.Println() // use fmt
+}
diff --git a/2012/goforc/example0.go b/2012/goforc/example0.go
new file mode 100644
index 0000000..a6e5829
--- /dev/null
+++ b/2012/goforc/example0.go
@@ -0,0 +1,23 @@
+package main
+
+import (
+	"fmt"
+	"time"
+)
+
+// START1 OMIT
+func main() {
+	f("Hello, World", 500*time.Millisecond)
+}
+
+// STOP1 OMIT
+
+// START2 OMIT
+func f(msg string, delay time.Duration) {
+	for i := 0; ; i++ {
+		fmt.Println(msg, i)
+		time.Sleep(delay)
+	}
+}
+
+// STOP2 OMIT
diff --git a/2012/goforc/example1.go b/2012/goforc/example1.go
new file mode 100644
index 0000000..cef6d1d
--- /dev/null
+++ b/2012/goforc/example1.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+	"fmt"
+	"time"
+)
+
+// START OMIT
+func main() {
+	go f("three", 300*time.Millisecond)
+	go f("six", 600*time.Millisecond)
+	go f("nine", 900*time.Millisecond)
+}
+
+// STOP OMIT
+
+func f(msg string, delay time.Duration) {
+	for i := 0; ; i++ {
+		fmt.Println(msg, i)
+		time.Sleep(delay)
+	}
+}
diff --git a/2012/goforc/example2.go b/2012/goforc/example2.go
new file mode 100644
index 0000000..b494f29
--- /dev/null
+++ b/2012/goforc/example2.go
@@ -0,0 +1,24 @@
+package main
+
+import (
+	"fmt"
+	"time"
+)
+
+// START OMIT
+func main() {
+	go f("three", 300*time.Millisecond)
+	go f("six", 600*time.Millisecond)
+	go f("nine", 900*time.Millisecond)
+	time.Sleep(3 * time.Second)
+	fmt.Println("Done.")
+}
+
+// STOP OMIT
+
+func f(msg string, delay time.Duration) {
+	for i := 0; ; i++ {
+		fmt.Println(msg, i)
+		time.Sleep(delay)
+	}
+}
diff --git a/2012/goforc/forloop.go b/2012/goforc/forloop.go
new file mode 100644
index 0000000..4706780
--- /dev/null
+++ b/2012/goforc/forloop.go
@@ -0,0 +1,28 @@
+package main
+
+import "fmt"
+
+var primes = [...]int{2, 3, 5, 7, 11, 13, 17, 19}
+
+func _() {
+	// START1 OMIT
+	for i := 0; i < len(primes); i++ {
+		fmt.Println(i, primes[i])
+	}
+	// STOP1 OMIT
+
+	// START2 OMIT
+	var sum int
+	for _, x := range primes {
+		sum += x
+	}
+	// STOP2 OMIT
+}
+
+func main() {
+	// START3 OMIT
+	for i, x := range primes {
+		fmt.Println(i, x)
+	}
+	// STOP3 OMIT
+}
diff --git a/2012/goforc/hello.go b/2012/goforc/hello.go
new file mode 100644
index 0000000..078ddff
--- /dev/null
+++ b/2012/goforc/hello.go
@@ -0,0 +1,7 @@
+package main
+
+import "fmt"
+
+func main() {
+	fmt.Println("Hello, 世界")
+}
diff --git a/2012/goforc/interface.go b/2012/goforc/interface.go
new file mode 100644
index 0000000..c885547
--- /dev/null
+++ b/2012/goforc/interface.go
@@ -0,0 +1,34 @@
+package main
+
+import "fmt"
+
+type Point struct{ x, y int }
+
+func (p Point) String() string { return fmt.Sprintf("Point{%d, %d}", p.x, p.y) }
+
+type Celsius float32
+type Fahrenheit float32
+
+func (t Celsius) String() string           { return fmt.Sprintf("%g°C", t) }
+func (t Fahrenheit) String() string        { return fmt.Sprintf("%g°F", t) }
+func (t Celsius) ToFahrenheit() Fahrenheit { return Fahrenheit(t*9/5 + 32) }
+
+func main() {
+	// START OMIT
+	type Stringer interface {
+		String() string
+	}
+
+	var v Stringer // HL
+	var corner = Point{1, 1}
+	var boiling = Celsius(100)
+
+	v = corner
+	fmt.Println(v.String()) // dynamic dispatch
+	fmt.Println(v)
+
+	v = boiling.ToFahrenheit()
+	fmt.Println(v.String()) // dynamic dispatch
+	fmt.Println(v)
+	// STOP OMIT
+}
diff --git a/2012/goforc/point.go b/2012/goforc/point.go
new file mode 100644
index 0000000..cfeb350
--- /dev/null
+++ b/2012/goforc/point.go
@@ -0,0 +1,20 @@
+package main
+
+import "fmt"
+
+type Point struct{ x, y int }
+
+func PointToString(p Point) string {
+	return fmt.Sprintf("Point{%d, %d}", p.x, p.y)
+}
+
+func (p Point) String() string { // HL
+	return fmt.Sprintf("Point{%d, %d}", p.x, p.y)
+}
+
+func main() {
+	p := Point{3, 5}
+	fmt.Println(PointToString(p)) // static dispatch // HL
+	fmt.Println(p.String())       // static dispatch // HL
+	fmt.Println(p)
+}
diff --git a/2012/goforc/stmts.go b/2012/goforc/stmts.go
new file mode 100644
index 0000000..7d271d3
--- /dev/null
+++ b/2012/goforc/stmts.go
@@ -0,0 +1,33 @@
+package main
+
+import "fmt"
+
+const digits = "0123456789abcdef"
+
+func itoa(x, base int) string {
+	// START OMIT
+	t := x
+	switch {
+	case x == 0:
+		return "0"
+	case x < 0:
+		t = -x
+	}
+	var s [32]byte
+	i := len(s)
+	for t != 0 { // Look, ma, no ()'s!
+		i--
+		s[i] = digits[t%base]
+		t /= base
+	}
+	if x < 0 {
+		i--
+		s[i] = '-'
+	}
+	return string(s[i:])
+	// STOP OMIT
+}
+
+func main() {
+	fmt.Println(itoa(-42, 2))
+}
diff --git a/2012/goforc/vars.go b/2012/goforc/vars.go
new file mode 100644
index 0000000..0fc6628
--- /dev/null
+++ b/2012/goforc/vars.go
@@ -0,0 +1,18 @@
+package p
+
+// START1 OMIT
+var i int
+var p, q *Point
+var threshold float64 = 0.75
+
+// STOP1 OMIT
+
+// START2 OMIT
+var i = 42       // type of i is int
+var z = 1 + 2.3i // type of z is complex128
+// STOP2 OMIT
+
+func _() int {
+	i := 42 // type of i is int
+	return &i
+}
diff --git a/2012/goforc/worker1.go b/2012/goforc/worker1.go
new file mode 100644
index 0000000..a9b12aa
--- /dev/null
+++ b/2012/goforc/worker1.go
@@ -0,0 +1,60 @@
+package main
+
+import "fmt"
+import "time"
+
+// START1 OMIT
+func main() {
+	start := time.Now()
+
+	in := make(chan int)    // Channel on which work orders are received.
+	out := make(chan []int) // Channel on which results are returned.
+	go producer(in)
+	go worker(in, out) // Launch one worker. // HL
+	consumer(out, 100)
+
+	fmt.Println(time.Since(start))
+}
+
+// STOP1 OMIT
+
+// START2 OMIT
+func worker(in chan int, out chan []int) {
+	for {
+		order := <-in           // Receive a work order. // HL
+		result := factor(order) // Do some work. // HL
+		out <- result           // Send the result back. // HL
+	}
+}
+
+// STOP2 OMIT
+
+// START3 OMIT
+func producer(out chan int) {
+	for order := 0; ; order++ {
+		out <- order // Produce a work order. // HL
+	}
+}
+
+func consumer(in chan []int, n int) {
+	for i := 0; i < n; i++ {
+		result := <-in // Consume a result. // HL
+		fmt.Println("Consumed", result)
+	}
+}
+
+// STOP3 OMIT
+
+// factor returns a list containing x and its prime factors.
+func factor(x int) (list []int) {
+	list = append(list, x)
+	for f := 2; x >= f; f++ {
+		for x%f == 0 {
+			x /= f
+			list = append(list, f)
+			// Slow down so we can see what happens.
+			time.Sleep(50 * time.Millisecond)
+		}
+	}
+	return
+}
diff --git a/2012/goforc/worker2.go b/2012/goforc/worker2.go
new file mode 100644
index 0000000..b10b8b2
--- /dev/null
+++ b/2012/goforc/worker2.go
@@ -0,0 +1,62 @@
+package main
+
+import "fmt"
+import "time"
+
+func main() {
+	start := time.Now()
+
+	// START1 OMIT
+	in := make(chan int)
+	out := make(chan []int)
+	go producer(in)
+	// Launch 10 workers. // HL
+	for i := 0; i < 10; i++ { // HL
+		go worker(in, out) // HL
+	} // HL
+	consumer(out, 100)
+	// STOP1 OMIT
+
+	fmt.Println(time.Since(start))
+}
+
+// START2 OMIT
+func worker(in chan int, out chan []int) {
+	for {
+		order := <-in           // Receive a work order. // HL
+		result := factor(order) // Do some work. // HL
+		out <- result           // Send the result back. // HL
+	}
+}
+
+// STOP2 OMIT
+
+// START3 OMIT
+func producer(out chan int) {
+	for order := 0; ; order++ {
+		out <- order // Produce a work order. // HL
+	}
+}
+
+func consumer(in chan []int, n int) {
+	for i := 0; i < n; i++ {
+		result := <-in // Consume a result. // HL
+		fmt.Println("Consumed", result)
+	}
+}
+
+// STOP3 OMIT
+
+// factor returns a list containing x and its prime factors.
+func factor(x int) (list []int) {
+	list = append(list, x)
+	for f := 2; x >= f; f++ {
+		for x%f == 0 {
+			x /= f
+			list = append(list, f)
+			// Slow down so we can see what happens.
+			time.Sleep(50 * time.Millisecond)
+		}
+	}
+	return
+}