Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
go-tour: move code snippets to article-specific directories
Browse files Browse the repository at this point in the history
As the prog/tour directory grows it becomes more difficult to see
what belongs where. Also the prog/tour directory hierarchy is one
level too deep.

Here's the code used to make this change:

        for article in *.article; do
                base=$(echo $article | sed 's/.article//')
                mkdir $base
                gofiles=$(grep '^.play' $article | awk '{print $2}' | sed 's_prog/tour/__' | sort -u)
                for f in $gofiles; do
                        hg mv prog/tour/$f $base/$f
                done
                sed -i '' 's_prog/tour_'$base'_' $article
        done

        # catch dreg masked by "#appengine: " comment directive
        hg mv prog/tour/sandbox.go welcome/

LGTM=campoy
R=campoy
CC=golang-codereviews
https://codereview.appspot.com/111250043
  • Loading branch information
adg committed Jul 23, 2014
1 parent d8e3192 commit 298ae46
Show file tree
Hide file tree
Showing 77 changed files with 73 additions and 73 deletions.
28 changes: 14 additions & 14 deletions content/basics.article
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ By convention, the package name is the same as the last element of the import pa
#appengine:
#appengine: (To see a different number, seed the number generator; see [[http://golang.org/pkg/math/rand/#Seed][`rand.Seed`]].)

.play prog/tour/packages.go
.play basics/packages.go

* Imports

Expand All @@ -32,7 +32,7 @@ You can also write multiple import statements, like:

But it is good style to use the factored import statement.

.play prog/tour/imports.go
.play basics/imports.go

* Exported names

Expand All @@ -44,7 +44,7 @@ In Go, a name is exported if it begins with a capital letter.

Run the code. Then rename `math.pi` to `math.Pi` and try it again.

.play prog/tour/exported-names.go
.play basics/exported-names.go

* Functions

Expand All @@ -56,7 +56,7 @@ Notice that the type comes _after_ the variable name.

(For more about why types look the way they do, see the [[http://golang.org/doc/articles/gos_declaration_syntax.html][article on Go's declaration syntax]].)

.play prog/tour/functions.go
.play basics/functions.go

* Functions continued

Expand All @@ -70,15 +70,15 @@ to

x, y int

.play prog/tour/functions-continued.go
.play basics/functions-continued.go

* Multiple results

A function can return any number of results.

The `swap` function returns two strings.

.play prog/tour/multiple-results.go
.play basics/multiple-results.go

* Named return values

Expand All @@ -90,31 +90,31 @@ A `return` statement without arguments returns the current values of the results

Naked return statements should be used only in short function, as with the example shown here. They can harm readability in longer functions.

.play prog/tour/named-results.go
.play basics/named-results.go

* Variables

The `var` statement declares a list of variables; as in function argument lists, the type is last.

A `var` statement can be at package or function level. We see both in this example.

.play prog/tour/variables.go
.play basics/variables.go

* Variables with initializers

A var declaration can include initializers, one per variable.

If an initializer is present, the type can be omitted; the variable will take the type of the initializer.

.play prog/tour/variables-with-initializers.go
.play basics/variables-with-initializers.go

* Short variable declarations

Inside a function, the `:=` short assignment statement can be used in place of a `var` declaration with implicit type.

Outside a function, every statement begins with a keyword (`var`, `func`, and so on) and so the `:=` construct is not available.

.play prog/tour/short-variable-declarations.go
.play basics/short-variable-declarations.go

* Basic types

Expand All @@ -140,7 +140,7 @@ The example shows variables of several types,
and also that variable declarations may be "factored" into blocks,
as with import statements.

.play prog/tour/basic-types.go
.play basics/basic-types.go

* Type conversions

Expand All @@ -162,7 +162,7 @@ Unlike in C, in Go assignment between items of different type requires an
explicit conversion.
Try removing the `float64` or `int` conversions in the example and see what happens.

.play prog/tour/type-conversions.go
.play basics/type-conversions.go

* Constants

Expand All @@ -172,7 +172,7 @@ Constants can be character, string, boolean, or numeric values.

Constants cannot be declared using the `:=` syntax.

.play prog/tour/constants.go
.play basics/constants.go

* Numeric Constants

Expand All @@ -182,7 +182,7 @@ An untyped constant takes the type needed by its context.

Try printing `needInt(Big)` too.

.play prog/tour/numeric-constants.go
.play basics/numeric-constants.go

* Congratulations!

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions content/concurrency.article
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The evaluation of `f`, `x`, `y`, and `z` happens in the current goroutine and th

Goroutines run in the same address space, so access to shared memory must be synchronized. The [[http://golang.org/pkg/sync/][`sync`]] package provides useful primitives, although you won't need them much in Go as there are other primitives. (See the next slide.)

.play prog/tour/goroutines.go
.play concurrency/goroutines.go

* Channels

Expand All @@ -36,7 +36,7 @@ Like maps and slices, channels must be created before use:

By default, sends and receives block until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.

.play prog/tour/channels.go
.play concurrency/channels.go

* Buffered Channels

Expand All @@ -48,7 +48,7 @@ Sends to a buffered channel block only when the buffer is full. Receives block w

Modify the example to overfill the buffer and see what happens.

.play prog/tour/buffered-channels.go
.play concurrency/buffered-channels.go

* Range and Close

Expand All @@ -64,15 +64,15 @@ The loop `for`i`:=`range`c` receives values from the channel repeatedly until it

*Another*note*: Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a `range` loop.

.play prog/tour/range-and-close.go
.play concurrency/range-and-close.go

* Select

The `select` statement lets a goroutine wait on multiple communication operations.

A `select` blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

.play prog/tour/select.go
.play concurrency/select.go

* Default Selection

Expand All @@ -87,7 +87,7 @@ Use a `default` case to try a send or receive without blocking:
// receiving from c would block
}

.play prog/tour/default-selection.go
.play concurrency/default-selection.go

* Exercise: Equivalent Binary Trees

Expand Down Expand Up @@ -125,15 +125,15 @@ Then read and print 10 values from the channel. It should be the numbers 1, 2, 3

`Same(tree.New(1),`tree.New(1))` should return true, and `Same(tree.New(1),`tree.New(2))` should return false.

.play prog/tour/exercise-equivalent-binary-trees.go
.play concurrency/exercise-equivalent-binary-trees.go

* Exercise: Web Crawler

In this exercise you'll use Go's concurrency features to parallelize a web crawler.

Modify the `Crawl` function to fetch URLs in parallel without fetching the same URL twice.

.play prog/tour/exercise-web-crawler.go
.play concurrency/exercise-web-crawler.go

* Where to Go from here...

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 14 additions & 14 deletions content/flowcontrol.article
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ Go has only one looping construct, the `for` loop.

The basic `for` loop looks as it does in C or Java, except that the `(`)` are gone (they are not even optional) and the `{`}` are required.

.play prog/tour/for.go
.play flowcontrol/for.go

* For continued

As in C or Java, you can leave the pre and post statements empty.

.play prog/tour/for-continued.go
.play flowcontrol/for-continued.go

* For is Go's "while"

At that point you can drop the semicolons: C's `while` is spelled `for` in Go.

.play prog/tour/for-is-gos-while.go
.play flowcontrol/for-is-gos-while.go

* Forever

If you omit the loop condition it loops forever, so an infinite loop is compactly expressed.

.play prog/tour/forever.go
.play flowcontrol/forever.go

* If

The `if` statement looks as it does in C or Java, except that the `(`)` are gone and the `{`}` are required.

(Sound familiar?)

.play prog/tour/if.go
.play flowcontrol/if.go

* If with a short statement

Expand All @@ -46,13 +46,13 @@ Variables declared by the statement are only in scope until the end of the `if`.

(Try using `v` in the last `return` statement.)

.play prog/tour/if-with-a-short-statement.go
.play flowcontrol/if-with-a-short-statement.go

* If and else

Variables declared inside an `if` short statement are also available inside any of the `else` blocks.

.play prog/tour/if-and-else.go
.play flowcontrol/if-and-else.go

* Exercise: Loops and Functions

Expand All @@ -71,15 +71,15 @@ Hint: to declare and initialize a floating point value, give it floating point s
z := float64(1)
z := 1.0

.play prog/tour/exercise-loops-and-functions.go
.play flowcontrol/exercise-loops-and-functions.go

* Switch

You probably knew what `switch` was going to look like.

A case body breaks automatically, unless it ends with a `fallthrough` statement.

.play prog/tour/switch.go
.play flowcontrol/switch.go

* Switch evaluation order

Expand All @@ -98,15 +98,15 @@ does not call `f` if `i==0`.)
#appengine: 2009-11-10 23:00:00 UTC, a value whose significance is left as an
#appengine: exercise for the reader.

.play prog/tour/switch-evaluation-order.go
.play flowcontrol/switch-evaluation-order.go

* Switch with no condition

Switch without a condition is the same as `switch`true`.

This construct can be a clean way to write long if-then-else chains.

.play prog/tour/switch-with-no-condition.go
.play flowcontrol/switch-with-no-condition.go

* Defer

Expand All @@ -116,7 +116,7 @@ function returns.
The deferred call's arguments are evaluated immediately, but the function call
is not executed until the surrounding function returns.

.play prog/tour/defer.go
.play flowcontrol/defer.go

* Stacking defers

Expand All @@ -126,10 +126,10 @@ deferred calls are executed in last-in-first-out order.
To learn more about defer statements read this
[[http://blog.golang.org/defer-panic-and-recover][blog post]].

.play prog/tour/defer-multi.go
.play flowcontrol/defer-multi.go

* Congratulations!

You finished this lesson!

You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]].
You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]].
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "fmt"

func main() {
sum := 1
for ; sum < 1000; {
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 298ae46

Please sign in to comment.