mirror of
https://github.com/golang/go
synced 2024-10-14 03:43:28 +00:00
tutorial: delete
Instead we'll point people at the Tour and beef up code.html. Fixes #3107. R=golang-dev, bradfitz, r, adg CC=golang-dev https://golang.org/cl/5697077
This commit is contained in:
parent
d9c5626047
commit
7201b0c27c
|
@ -8,7 +8,6 @@ HTML=\
|
|||
articles/slices_usage_and_internals.html\
|
||||
effective_go.html\
|
||||
go1.html\
|
||||
go_tutorial.html\
|
||||
|
||||
all: tmpltohtml $(HTML)
|
||||
|
||||
|
|
|
@ -32,17 +32,10 @@ learned. You can <a href="http://tour.golang.org/">take the tour online</a> or
|
|||
<a href="http://code.google.com/p/go-tour/">install it locally</a>.
|
||||
</p>
|
||||
|
||||
<h3 id="orig_tutorial"><a href="go_tutorial.html">A Tutorial for the Go Programming Language</a></h3>
|
||||
<p>
|
||||
The first tutorial. An introductory text that touches upon several core
|
||||
concepts: syntax, types, allocation, constants, I/O, sorting, printing,
|
||||
goroutines, and channels.
|
||||
</p>
|
||||
|
||||
<h3 id="effective_go"><a href="effective_go.html">Effective Go</a></h3>
|
||||
<p>
|
||||
A document that gives tips for writing clear, idiomatic Go code.
|
||||
A must read for any new Go programmer. It augments the tutorial and
|
||||
A must read for any new Go programmer. It augments the tour and
|
||||
the language specification, both of which should be read first.
|
||||
</p>
|
||||
|
||||
|
@ -226,7 +219,7 @@ Go libraries.</p>
|
|||
<ul>
|
||||
<li><a href="http://go-tour-zh.appspot.com/">A Tour of Go</a></li>
|
||||
<li><a href="http://code.google.com/p/golang-china/">golang-china</a> - a broad range of Go documentation.</li>
|
||||
<li><a href="http://code.google.com/p/ac-me/downloads/detail?name=fango.pdf">Effective Go and Tutorial</a></li>
|
||||
<li><a href="http://code.google.com/p/ac-me/downloads/detail?name=fango.pdf">Effective Go and (old) Tutorial</a></li>
|
||||
</ul>
|
||||
|
||||
<h4 id="docs_cz">Czech — Čeština</h4>
|
||||
|
@ -245,7 +238,7 @@ Go libraries.</p>
|
|||
<h4 id="docs_de">German — Deutsch</h4>
|
||||
|
||||
<ul>
|
||||
<li><a href="http://bitloeffel.de/DOC/golang/go_tutorial_de.html">Eine Anleitung zum Programmieren in Go</a> - the Go Tutorial.</li>
|
||||
<li><a href="http://bitloeffel.de/DOC/golang/go_tutorial_de.html">Eine Anleitung zum Programmieren in Go</a> - the (old) Go Tutorial.</li>
|
||||
<li><a href="http://bitloeffel.de/DOC/golang/effective_go_de.html">Wirkungsvoll Go programmieren</a> - Effective Go.</li>
|
||||
<li><a href="http://bitloeffel.de/DOC/golang/code_de.html">Wie man Go-Kode schreibt</a> - How to Write Go Code.</li>
|
||||
</ul>
|
||||
|
|
|
@ -31,8 +31,10 @@ will be easy for other Go programmers to understand.
|
|||
|
||||
<p>
|
||||
This document gives tips for writing clear, idiomatic Go code.
|
||||
It augments the <a href="go_spec.html">language specification</a>
|
||||
and the <a href="go_tutorial.html">tutorial</a>, both of which you
|
||||
It augments the <a href="go_spec.html">language specification</a>,
|
||||
the <a href="http://tour.golang.org/">Tour of Go</a>,
|
||||
and <a href="/doc/code.html">How to Write Go Code</a>,
|
||||
all of which you
|
||||
should read first.
|
||||
</p>
|
||||
|
||||
|
@ -1454,7 +1456,7 @@ fmt.Println(fmt.Sprint("Hello ", 23))
|
|||
</pre>
|
||||
<p>
|
||||
As mentioned in
|
||||
the <a href="go_tutorial.html">tutorial</a>, <code>fmt.Fprint</code>
|
||||
the <a href="http://code.google.com/p/go-tour/">Tour</a>, <code>fmt.Fprint</code>
|
||||
and friends take as a first argument any object
|
||||
that implements the <code>io.Writer</code> interface; the variables <code>os.Stdout</code>
|
||||
and <code>os.Stderr</code> are familiar instances.
|
||||
|
|
|
@ -27,8 +27,10 @@ will be easy for other Go programmers to understand.
|
|||
|
||||
<p>
|
||||
This document gives tips for writing clear, idiomatic Go code.
|
||||
It augments the <a href="go_spec.html">language specification</a>
|
||||
and the <a href="go_tutorial.html">tutorial</a>, both of which you
|
||||
It augments the <a href="go_spec.html">language specification</a>,
|
||||
the <a href="http://tour.golang.org/">Tour of Go</a>,
|
||||
and <a href="/doc/code.html">How to Write Go Code</a>,
|
||||
all of which you
|
||||
should read first.
|
||||
</p>
|
||||
|
||||
|
@ -1450,7 +1452,7 @@ fmt.Println(fmt.Sprint("Hello ", 23))
|
|||
</pre>
|
||||
<p>
|
||||
As mentioned in
|
||||
the <a href="go_tutorial.html">tutorial</a>, <code>fmt.Fprint</code>
|
||||
the <a href="http://code.google.com/p/go-tour/">Tour</a>, <code>fmt.Fprint</code>
|
||||
and friends take as a first argument any object
|
||||
that implements the <code>io.Writer</code> interface; the variables <code>os.Stdout</code>
|
||||
and <code>os.Stderr</code> are familiar instances.
|
||||
|
|
|
@ -11,8 +11,9 @@ to nothing about the similarities.
|
|||
|
||||
<p>
|
||||
For a more general introduction to Go, see the
|
||||
<a href="go_tutorial.html">Go tutorial</a> and
|
||||
<a href="effective_go.html">Effective Go</a>.
|
||||
<a href="http://tour.golang.org/">Go Tour</a>,
|
||||
<a href="/doc/code.html">How to Write Go Code</a>
|
||||
and <a href="effective_go.html">Effective Go</a>.
|
||||
|
||||
<p>
|
||||
For a detailed description of the Go language, see the
|
||||
|
|
1452
doc/go_tutorial.html
1452
doc/go_tutorial.html
File diff suppressed because it is too large
Load diff
1040
doc/go_tutorial.tmpl
1040
doc/go_tutorial.tmpl
File diff suppressed because it is too large
Load diff
|
@ -225,8 +225,7 @@ If you see the "hello, world" message then Go is installed correctly.
|
|||
<h2 id="next">What's next</h2>
|
||||
|
||||
<p>
|
||||
Start by taking <a href="http://code.google.com/p/go-tour/">A Tour of Go</a>
|
||||
or reading the <a href="/doc/go_tutorial.html">Go Tutorial</a>.
|
||||
Start by taking <a href="http://tour.golang.org/">A Tour of Go</a>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
set -e
|
||||
|
||||
TMPL=${1:-go_tutorial.tmpl} # input file
|
||||
TMPL=${1:-effective_go.tmpl} # input file
|
||||
HTML=$(dirname $TMPL)/$(basename $TMPL .tmpl).html # output file
|
||||
|
||||
if ! test -w $HTML
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"./file"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func cat(f *file.File) {
|
||||
const NBUF = 512
|
||||
var buf [NBUF]byte
|
||||
for {
|
||||
switch nr, er := f.Read(buf[:]); true {
|
||||
case nr < 0:
|
||||
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", f, er)
|
||||
os.Exit(1)
|
||||
case nr == 0: // EOF
|
||||
return
|
||||
case nr > 0:
|
||||
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
|
||||
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f, ew)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse() // Scans the arg list and sets up flags
|
||||
if flag.NArg() == 0 {
|
||||
cat(file.Stdin)
|
||||
}
|
||||
for i := 0; i < flag.NArg(); i++ {
|
||||
f, err := file.Open(flag.Arg(i))
|
||||
if f == nil {
|
||||
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
|
||||
os.Exit(1)
|
||||
}
|
||||
cat(f)
|
||||
f.Close()
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"./file"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var rot13Flag = flag.Bool("rot13", false, "rot13 the input")
|
||||
|
||||
func rot13(b byte) byte {
|
||||
if 'a' <= b && b <= 'z' {
|
||||
b = 'a' + ((b-'a')+13)%26
|
||||
}
|
||||
if 'A' <= b && b <= 'Z' {
|
||||
b = 'A' + ((b-'A')+13)%26
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
type reader interface {
|
||||
Read(b []byte) (ret int, err error)
|
||||
String() string
|
||||
}
|
||||
|
||||
type rotate13 struct {
|
||||
source reader
|
||||
}
|
||||
|
||||
func newRotate13(source reader) *rotate13 {
|
||||
return &rotate13{source}
|
||||
}
|
||||
|
||||
func (r13 *rotate13) Read(b []byte) (ret int, err error) {
|
||||
r, e := r13.source.Read(b)
|
||||
for i := 0; i < r; i++ {
|
||||
b[i] = rot13(b[i])
|
||||
}
|
||||
return r, e
|
||||
}
|
||||
|
||||
func (r13 *rotate13) String() string {
|
||||
return r13.source.String()
|
||||
}
|
||||
|
||||
// end of rotate13 implementation OMIT
|
||||
|
||||
func cat(r reader) {
|
||||
const NBUF = 512
|
||||
var buf [NBUF]byte
|
||||
|
||||
if *rot13Flag {
|
||||
r = newRotate13(r)
|
||||
}
|
||||
for {
|
||||
switch nr, er := r.Read(buf[:]); {
|
||||
case nr < 0:
|
||||
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", r, er)
|
||||
os.Exit(1)
|
||||
case nr == 0: // EOF
|
||||
return
|
||||
case nr > 0:
|
||||
nw, ew := file.Stdout.Write(buf[0:nr])
|
||||
if nw != nr {
|
||||
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", r, ew)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse() // Scans the arg list and sets up flags
|
||||
if flag.NArg() == 0 {
|
||||
cat(file.Stdin)
|
||||
}
|
||||
for i := 0; i < flag.NArg(); i++ {
|
||||
f, err := file.Open(flag.Arg(i))
|
||||
if f == nil {
|
||||
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
|
||||
os.Exit(1)
|
||||
}
|
||||
cat(f)
|
||||
f.Close()
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag" // command line option parser
|
||||
"os"
|
||||
)
|
||||
|
||||
var omitNewline = flag.Bool("n", false, "don't print final newline")
|
||||
|
||||
const (
|
||||
Space = " "
|
||||
Newline = "\n"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse() // Scans the arg list and sets up flags
|
||||
var s string = ""
|
||||
for i := 0; i < flag.NArg(); i++ {
|
||||
if i > 0 {
|
||||
s += Space
|
||||
}
|
||||
s += flag.Arg(i)
|
||||
}
|
||||
if !*omitNewline {
|
||||
s += Newline
|
||||
}
|
||||
os.Stdout.WriteString(s)
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
fd int // file descriptor number
|
||||
name string // file name at Open time
|
||||
}
|
||||
|
||||
func newFile(fd int, name string) *File {
|
||||
if fd < 0 {
|
||||
return nil
|
||||
}
|
||||
return &File{fd, name}
|
||||
}
|
||||
|
||||
var (
|
||||
Stdin = newFile(syscall.Stdin, "/dev/stdin")
|
||||
Stdout = newFile(syscall.Stdout, "/dev/stdout")
|
||||
Stderr = newFile(syscall.Stderr, "/dev/stderr")
|
||||
)
|
||||
|
||||
func OpenFile(name string, mode int, perm uint32) (file *File, err error) {
|
||||
r, err := syscall.Open(name, mode, perm)
|
||||
return newFile(r, name), err
|
||||
}
|
||||
|
||||
const (
|
||||
O_RDONLY = syscall.O_RDONLY
|
||||
O_RDWR = syscall.O_RDWR
|
||||
O_CREATE = syscall.O_CREAT
|
||||
O_TRUNC = syscall.O_TRUNC
|
||||
)
|
||||
|
||||
func Open(name string) (file *File, err error) {
|
||||
return OpenFile(name, O_RDONLY, 0)
|
||||
}
|
||||
|
||||
func Create(name string) (file *File, err error) {
|
||||
return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
|
||||
}
|
||||
|
||||
func (file *File) Close() error {
|
||||
if file == nil {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
err := syscall.Close(file.fd)
|
||||
file.fd = -1 // so it can't be closed again
|
||||
return err
|
||||
}
|
||||
|
||||
func (file *File) Read(b []byte) (ret int, err error) {
|
||||
if file == nil {
|
||||
return -1, os.ErrInvalid
|
||||
}
|
||||
r, err := syscall.Read(file.fd, b)
|
||||
return int(r), err
|
||||
}
|
||||
|
||||
func (file *File) Write(b []byte) (ret int, err error) {
|
||||
if file == nil {
|
||||
return -1, os.ErrInvalid
|
||||
}
|
||||
r, err := syscall.Write(file.fd, b)
|
||||
return int(r), err
|
||||
}
|
||||
|
||||
func (file *File) String() string {
|
||||
return file.name
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
fd syscall.Handle // file descriptor number
|
||||
name string // file name at Open time
|
||||
}
|
||||
|
||||
func newFile(fd syscall.Handle, name string) *File {
|
||||
if fd == ^syscall.Handle(0) {
|
||||
return nil
|
||||
}
|
||||
return &File{fd, name}
|
||||
}
|
||||
|
||||
var (
|
||||
Stdin = newFile(syscall.Stdin, "/dev/stdin")
|
||||
Stdout = newFile(syscall.Stdout, "/dev/stdout")
|
||||
Stderr = newFile(syscall.Stderr, "/dev/stderr")
|
||||
)
|
||||
|
||||
func OpenFile(name string, mode int, perm uint32) (file *File, err error) {
|
||||
r, err := syscall.Open(name, mode, perm)
|
||||
return newFile(r, name), err
|
||||
}
|
||||
|
||||
const (
|
||||
O_RDONLY = syscall.O_RDONLY
|
||||
O_RDWR = syscall.O_RDWR
|
||||
O_CREATE = syscall.O_CREAT
|
||||
O_TRUNC = syscall.O_TRUNC
|
||||
)
|
||||
|
||||
func Open(name string) (file *File, err error) {
|
||||
return OpenFile(name, O_RDONLY, 0)
|
||||
}
|
||||
|
||||
func Create(name string) (file *File, err error) {
|
||||
return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
|
||||
}
|
||||
|
||||
func (file *File) Close() error {
|
||||
if file == nil {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
err := syscall.Close(file.fd)
|
||||
file.fd = syscall.InvalidHandle // so it can't be closed again
|
||||
return err
|
||||
}
|
||||
|
||||
func (file *File) Read(b []byte) (ret int, err error) {
|
||||
if file == nil {
|
||||
return -1, os.ErrInvalid
|
||||
}
|
||||
r, err := syscall.Read(file.fd, b)
|
||||
return int(r), err
|
||||
}
|
||||
|
||||
func (file *File) Write(b []byte) (ret int, err error) {
|
||||
if file == nil {
|
||||
return -1, os.ErrInvalid
|
||||
}
|
||||
r, err := syscall.Write(file.fd, b)
|
||||
return int(r), err
|
||||
}
|
||||
|
||||
func (file *File) String() string {
|
||||
return file.name
|
||||
}
|
|
@ -14,12 +14,6 @@ fi
|
|||
|
||||
rm -f *.$O
|
||||
|
||||
if [ "$GOOS" = "windows" ];then
|
||||
$GC -o file.$O file_windows.go
|
||||
else
|
||||
$GC file.go
|
||||
fi
|
||||
|
||||
defer_panic_recover="
|
||||
defer.go
|
||||
defer2.go
|
||||
|
@ -38,29 +32,10 @@ error_handling="
|
|||
error4.go
|
||||
"
|
||||
|
||||
go_tutorial="
|
||||
cat.go
|
||||
cat_rot13.go
|
||||
echo.go
|
||||
helloworld.go
|
||||
helloworld3.go
|
||||
print.go
|
||||
print_string.go
|
||||
server.go
|
||||
server1.go
|
||||
sieve.go
|
||||
sieve1.go
|
||||
sort.go
|
||||
sortmain.go
|
||||
strings.go
|
||||
sum.go
|
||||
"
|
||||
|
||||
for i in \
|
||||
$defer_panic_recover \
|
||||
$effective_go \
|
||||
$error_handling \
|
||||
$go_tutorial \
|
||||
slices.go \
|
||||
go1.go \
|
||||
; do
|
||||
|
@ -80,43 +55,10 @@ function testit {
|
|||
fi
|
||||
}
|
||||
|
||||
function testitpipe {
|
||||
$LD $1.$O
|
||||
./$O.out | $2 2>&1 >"$TMPFILE" || true
|
||||
x=$(echo $(cat "$TMPFILE")) # extra echo canonicalizes
|
||||
if [ "$x" != "$3" ]
|
||||
then
|
||||
echo $1 failed: '"'$x'"' is not '"'$3'"'
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
testit helloworld "" "Hello, world; or Καλημέρα κόσμε; or こんにちは 世界"
|
||||
testit helloworld3 "" "hello, world can't open file; err=no such file or directory"
|
||||
testit echo "hello, world" "hello, world"
|
||||
testit sum "" "6"
|
||||
testit strings "" ""
|
||||
testit defer "" "0 3210 2"
|
||||
testit defer2 "" "Calling g. Printing in g 0 Printing in g 1 Printing in g 2 Printing in g 3 Panicking! Defer in g 3 Defer in g 2 Defer in g 1 Defer in g 0 Recovered in f 4 Returned normally from f."
|
||||
|
||||
alphabet=abcdefghijklmnopqrstuvwxyz
|
||||
rot13=nopqrstuvwxyzabcdefghijklm
|
||||
echo $alphabet | testit cat "" $alphabet
|
||||
echo $alphabet | testit cat_rot13 "--rot13" $rot13
|
||||
echo $rot13 | testit cat_rot13 "--rot13" $alphabet
|
||||
|
||||
testit sortmain "" "Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
|
||||
|
||||
testit print "" "18446744073709551615 -1 18446744073709551615 {77 Sunset Strip} [1 2 3 4] 18446744073709551615 {77 Sunset Strip} [1 2 3 4] 18446744073709551615 {77 Sunset Strip} [1 2 3 4]"
|
||||
testit print_string "" "77 Sunset Strip"
|
||||
|
||||
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
|
||||
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
|
||||
|
||||
# server hangs; don't run it, just compile it
|
||||
$GC server.go
|
||||
testit server1 "" ""
|
||||
|
||||
testit eff_bytesize "" "1.00YB 9.09TB"
|
||||
testit eff_sequence "" "[-1 2 6 16 44]"
|
||||
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Send the sequence 2, 3, 4, ... to channel 'ch'.
|
||||
func generate(ch chan int) {
|
||||
for i := 2; ; i++ {
|
||||
ch <- i // Send 'i' to channel 'ch'.
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the values from channel 'in' to channel 'out',
|
||||
// removing those divisible by 'prime'.
|
||||
func filter(in, out chan int, prime int) {
|
||||
for {
|
||||
i := <-in // Receive value of new variable 'i' from 'in'.
|
||||
if i%prime != 0 {
|
||||
out <- i // Send 'i' to channel 'out'.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The prime sieve: Daisy-chain filter processes together.
|
||||
func main() {
|
||||
ch := make(chan int) // Create a new channel.
|
||||
go generate(ch) // Start generate() as a goroutine.
|
||||
for i := 0; i < 100; i++ { // Print the first hundred primes.
|
||||
prime := <-ch
|
||||
fmt.Println(prime)
|
||||
ch1 := make(chan int)
|
||||
go filter(ch, ch1, prime)
|
||||
ch = ch1
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Send the sequence 2, 3, 4, ... to returned channel
|
||||
func generate() chan int {
|
||||
ch := make(chan int)
|
||||
go func() {
|
||||
for i := 2; ; i++ {
|
||||
ch <- i
|
||||
}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
// Filter out input values divisible by 'prime', send rest to returned channel
|
||||
func filter(in chan int, prime int) chan int {
|
||||
out := make(chan int)
|
||||
go func() {
|
||||
for {
|
||||
if i := <-in; i%prime != 0 {
|
||||
out <- i
|
||||
}
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
func sieve() chan int {
|
||||
out := make(chan int)
|
||||
go func() {
|
||||
ch := generate()
|
||||
for {
|
||||
prime := <-ch
|
||||
out <- prime
|
||||
ch = filter(ch, prime)
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
func main() {
|
||||
primes := sieve()
|
||||
for i := 0; i < 100; i++ { // Print the first hundred primes.
|
||||
fmt.Println(<-primes)
|
||||
}
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"./sort"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ints() {
|
||||
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
|
||||
a := sort.IntSlice(data)
|
||||
sort.Sort(a)
|
||||
if !sort.IsSorted(a) {
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
|
||||
func strings() {
|
||||
data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
|
||||
a := sort.StringSlice(data)
|
||||
sort.Sort(a)
|
||||
if !sort.IsSorted(a) {
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
|
||||
type day struct {
|
||||
num int
|
||||
shortName string
|
||||
longName string
|
||||
}
|
||||
|
||||
type dayArray struct {
|
||||
data []*day
|
||||
}
|
||||
|
||||
func (p *dayArray) Len() int { return len(p.data) }
|
||||
func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
|
||||
func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] }
|
||||
|
||||
func days() {
|
||||
Sunday := day{0, "SUN", "Sunday"}
|
||||
Monday := day{1, "MON", "Monday"}
|
||||
Tuesday := day{2, "TUE", "Tuesday"}
|
||||
Wednesday := day{3, "WED", "Wednesday"}
|
||||
Thursday := day{4, "THU", "Thursday"}
|
||||
Friday := day{5, "FRI", "Friday"}
|
||||
Saturday := day{6, "SAT", "Saturday"}
|
||||
data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday}
|
||||
a := dayArray{data}
|
||||
sort.Sort(&a)
|
||||
if !sort.IsSorted(&a) {
|
||||
panic("fail")
|
||||
}
|
||||
for _, d := range data {
|
||||
fmt.Printf("%s ", d.longName)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
ints()
|
||||
strings()
|
||||
days()
|
||||
}
|
|
@ -380,7 +380,7 @@ containing other data structures as continuous blocks of memory."
|
|||
<h2>And more:</h2>
|
||||
<ul>
|
||||
<li>I haven't talked about the type system, interfaces, slices, closures, selects, ...</li>
|
||||
<li>Tutorial, documentation, mailing list, source code all online</li>
|
||||
<li>Documentation, mailing list, source code all online</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
// Web server tree:
|
||||
//
|
||||
// http://godoc/ main landing page
|
||||
// http://godoc/doc/ serve from $GOROOT/doc - spec, mem, tutorial, etc.
|
||||
// http://godoc/doc/ serve from $GOROOT/doc - spec, mem, etc.
|
||||
// http://godoc/src/ serve files from $GOROOT/src; .go gets pretty-printed
|
||||
// http://godoc/cmd/ serve documentation about commands
|
||||
// http://godoc/pkg/ serve documentation about packages
|
||||
|
|
Loading…
Reference in a new issue