Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
a5e10edc
Commit
a5e10edc
authored
Nov 26, 2012
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc/play: don't use println in examples
R=golang-dev, rsc CC=golang-dev
https://golang.org/cl/6849105
parent
2e73453a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
15 additions
and
11 deletions
+15
-11
fib.go
doc/play/fib.go
+3
-1
sieve.go
doc/play/sieve.go
+3
-1
solitaire.go
doc/play/solitaire.go
+7
-7
tree.go
doc/play/tree.go
+2
-2
No files found.
doc/play/fib.go
View file @
a5e10edc
package
main
package
main
import
"fmt"
// fib returns a function that returns
// fib returns a function that returns
// successive Fibonacci numbers.
// successive Fibonacci numbers.
func
fib
()
func
()
int
{
func
fib
()
func
()
int
{
...
@@ -13,5 +15,5 @@ func fib() func() int {
...
@@ -13,5 +15,5 @@ func fib() func() int {
func
main
()
{
func
main
()
{
f
:=
fib
()
f
:=
fib
()
// Function calls are evaluated left-to-right.
// Function calls are evaluated left-to-right.
p
rintln
(
f
(),
f
(),
f
(),
f
(),
f
())
fmt
.
P
rintln
(
f
(),
f
(),
f
(),
f
(),
f
())
}
}
doc/play/sieve.go
View file @
a5e10edc
...
@@ -2,6 +2,8 @@
...
@@ -2,6 +2,8 @@
package
main
package
main
import
"fmt"
// Send the sequence 2, 3, 4, ... to channel 'ch'.
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func
Generate
(
ch
chan
<-
int
)
{
func
Generate
(
ch
chan
<-
int
)
{
for
i
:=
2
;
;
i
++
{
for
i
:=
2
;
;
i
++
{
...
@@ -26,7 +28,7 @@ func main() {
...
@@ -26,7 +28,7 @@ func main() {
go
Generate
(
ch
)
// Launch Generate goroutine.
go
Generate
(
ch
)
// Launch Generate goroutine.
for
i
:=
0
;
i
<
10
;
i
++
{
for
i
:=
0
;
i
<
10
;
i
++
{
prime
:=
<-
ch
prime
:=
<-
ch
print
(
prime
,
"
\n
"
)
fmt
.
Println
(
prime
)
ch1
:=
make
(
chan
int
)
ch1
:=
make
(
chan
int
)
go
Filter
(
ch
,
ch1
,
prime
)
go
Filter
(
ch
,
ch1
,
prime
)
ch
=
ch1
ch
=
ch1
...
...
doc/play/solitaire.go
View file @
a5e10edc
...
@@ -28,7 +28,7 @@ var board = []rune(
...
@@ -28,7 +28,7 @@ var board = []rune(
...........
...........
`
)
`
)
// center is the position of the center hole if
// center is the position of the center hole if
// there is a single one; otherwise it is -1.
// there is a single one; otherwise it is -1.
var
center
int
var
center
int
...
@@ -47,7 +47,7 @@ func init() {
...
@@ -47,7 +47,7 @@ func init() {
var
moves
int
// number of times move is called
var
moves
int
// number of times move is called
// move tests if there is a peg at position pos that
// move tests if there is a peg at position pos that
// can jump over another peg in direction dir. If the
// can jump over another peg in direction dir. If the
// move is valid, it is executed and move returns true.
// move is valid, it is executed and move returns true.
// Otherwise, move returns false.
// Otherwise, move returns false.
...
@@ -69,11 +69,11 @@ func unmove(pos, dir int) {
...
@@ -69,11 +69,11 @@ func unmove(pos, dir int) {
board
[
pos
+
2
*
dir
]
=
'○'
board
[
pos
+
2
*
dir
]
=
'○'
}
}
// solve tries to find a sequence of moves such that
// solve tries to find a sequence of moves such that
// there is only one peg left at the end; if center is
// there is only one peg left at the end; if center is
// >= 0, that last peg must be in the center position.
// >= 0, that last peg must be in the center position.
// If a solution is found, solve prints the board after
// If a solution is found, solve prints the board after
// each move in a backward fashion (i.e., the last
// each move in a backward fashion (i.e., the last
// board position is printed first, all the way back to
// board position is printed first, all the way back to
// the starting board position).
// the starting board position).
func
solve
()
bool
{
func
solve
()
bool
{
...
@@ -89,7 +89,7 @@ func solve() bool {
...
@@ -89,7 +89,7 @@ func solve() bool {
// see if this new board has a solution
// see if this new board has a solution
if
solve
()
{
if
solve
()
{
unmove
(
pos
,
dir
)
unmove
(
pos
,
dir
)
p
rintln
(
string
(
board
))
fmt
.
P
rintln
(
string
(
board
))
return
true
return
true
}
}
unmove
(
pos
,
dir
)
unmove
(
pos
,
dir
)
...
@@ -102,7 +102,7 @@ func solve() bool {
...
@@ -102,7 +102,7 @@ func solve() bool {
// tried each possible move
// tried each possible move
if
n
==
1
&&
(
center
<
0
||
last
==
center
)
{
if
n
==
1
&&
(
center
<
0
||
last
==
center
)
{
// there's only one peg left
// there's only one peg left
p
rintln
(
string
(
board
))
fmt
.
P
rintln
(
string
(
board
))
return
true
return
true
}
}
// no solution found for this board
// no solution found for this board
...
...
doc/play/tree.go
View file @
a5e10edc
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
// express concurrent concepts, such as
// express concurrent concepts, such as
// this binary tree comparison.
// this binary tree comparison.
//
//
// Trees may be of different shapes,
// Trees may be of different shapes,
// but have the same contents. For example:
// but have the same contents. For example:
//
//
// 4 6
// 4 6
...
@@ -29,7 +29,7 @@ type Tree struct {
...
@@ -29,7 +29,7 @@ type Tree struct {
Right
*
Tree
Right
*
Tree
}
}
// Walk traverses a tree depth-first,
// Walk traverses a tree depth-first,
// sending each Value on a channel.
// sending each Value on a channel.
func
Walk
(
t
*
Tree
,
ch
chan
int
)
{
func
Walk
(
t
*
Tree
,
ch
chan
int
)
{
if
t
==
nil
{
if
t
==
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment