diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
commit | 505c19580e0f43fe5224431459cacb7c21edd93d (patch) | |
tree | 79e2634c253d60afc0cc0b2f510dc7dcbb48497b /doc/codewalk | |
parent | 1336a7c91e596c423a49d1194ea42d98bca0d958 (diff) | |
download | golang-505c19580e0f43fe5224431459cacb7c21edd93d.tar.gz |
Imported Upstream version 1upstream/1
Diffstat (limited to 'doc/codewalk')
-rw-r--r-- | doc/codewalk/functions.xml | 12 | ||||
-rw-r--r-- | doc/codewalk/markov.go | 6 | ||||
-rw-r--r-- | doc/codewalk/markov.xml | 19 | ||||
-rw-r--r-- | doc/codewalk/pig.go | 5 | ||||
-rw-r--r-- | doc/codewalk/sharemem.xml | 8 | ||||
-rw-r--r-- | doc/codewalk/urlpoll.go | 29 |
6 files changed, 32 insertions, 47 deletions
diff --git a/doc/codewalk/functions.xml b/doc/codewalk/functions.xml index 986a017e1..db518dcc0 100644 --- a/doc/codewalk/functions.xml +++ b/doc/codewalk/functions.xml @@ -45,7 +45,7 @@ turn. </step> -<step title="Multiple return values" src="doc/codewalk/pig.go:/\/\/ roll returns/,/stay.*true\n}/"> +<step title="Multiple return values" src="doc/codewalk/pig.go:/\/\/ roll returns/,/true\n}/"> Go functions can return multiple values. <br/><br/> @@ -82,16 +82,6 @@ associated with the current player. </step> -<step title="Comparing functions" src="doc/codewalk/pig.go:/if action/,/currentPlayer\)\)\n\t\t}/"> - Functions can be compared for equality in Go. From the - <a href="http://golang.org/doc/go_spec.html#Comparison_operators">language specification</a>: - Function values are equal if they refer to the same function or if both are <code>nil</code>. - <br/><br/> - - We enforce that a <code>strategy</code> function can only return a legal - <code>action</code>: either <code>roll</code> or <code>stay</code>. -</step> - <step title="Simulating a tournament" src="doc/codewalk/pig.go:/\/\/ roundRobin/,/gamesPerStrategy\n}/"> The <code>roundRobin</code> function simulates a tournament and tallies wins. Each strategy plays each other strategy <code>gamesPerSeries</code> times. diff --git a/doc/codewalk/markov.go b/doc/codewalk/markov.go index 959c2b158..a8f322eb6 100644 --- a/doc/codewalk/markov.go +++ b/doc/codewalk/markov.go @@ -50,8 +50,8 @@ import ( "flag" "fmt" "io" + "math/rand" "os" - "rand" "strings" "time" ) @@ -120,8 +120,8 @@ func main() { numWords := flag.Int("words", 100, "maximum number of words to print") prefixLen := flag.Int("prefix", 2, "prefix length in words") - flag.Parse() // Parse command-line flags. - rand.Seed(time.Nanoseconds()) // Seed the random number generator. + flag.Parse() // Parse command-line flags. + rand.Seed(time.Now().UnixNano()) // Seed the random number generator. c := NewChain(*prefixLen) // Initialize a new Chain. c.Build(os.Stdin) // Build chains from standard input. diff --git a/doc/codewalk/markov.xml b/doc/codewalk/markov.xml index a89b4d0ce..085ead7bc 100644 --- a/doc/codewalk/markov.xml +++ b/doc/codewalk/markov.xml @@ -105,7 +105,7 @@ Prefix Map key reads space-separated values from an <code>io.Reader</code>. <br/><br/> The <code>Build</code> method returns once the <code>Reader</code>'s - <code>Read</code> method returns <code>os.EOF</code> (end of file) + <code>Read</code> method returns <code>io.EOF</code> (end of file) or some other read error occurs. </step> @@ -133,7 +133,7 @@ Prefix Map key (including punctuation), which is exactly what we need. <br/><br/> <code>Fscan</code> returns an error if it encounters a read error - (<code>os.EOF</code>, for example) or if it can't scan the requested + (<code>io.EOF</code>, for example) or if it can't scan the requested value (in our case, a single string). In either case we just want to stop scanning, so we <code>break</code> out of the loop. </step> @@ -155,7 +155,7 @@ Prefix Map key <br/><br/> For more information about the <code>append</code> function and slices in general see the - <a href="http://blog.golang.org/2011/01/go-slices-usage-and-internals.html">Slices: usage and internals</a> article. + <a href="/doc/articles/slices_usage_and_internals.html">Slices: usage and internals</a> article. </step> <step title="Pushing the suffix onto the prefix" src="doc/codewalk/markov.go:/p\.Shift/"> @@ -275,16 +275,15 @@ p[len(p)-1] = suffix </step> <step title="Using this program" src="doc/codewalk/markov.go"> - To use this program, first compile and link it. - If you are using <code>6g</code> as your compiler, the command - would look something like this: + To use this program, first build it with the + <a href="/cmd/go/">go</a> command: <pre> -$ 6g markov.go && 6l -o markov markov.6</pre> +$ go build markov.go</pre> And then execute it while piping in some input text: <pre> -$ echo "a man a plan a canal panama" | ./markov -prefix=1 -a plan a man a plan a canal panama - </pre> +$ echo "a man a plan a canal panama" \ + | ./markov -prefix=1 +a plan a man a plan a canal panama</pre> Here's a transcript of generating some text using the Go distribution's README file as source material: <pre> diff --git a/doc/codewalk/pig.go b/doc/codewalk/pig.go index 9e415f589..10963b4e4 100644 --- a/doc/codewalk/pig.go +++ b/doc/codewalk/pig.go @@ -6,7 +6,7 @@ package main import ( "fmt" - "rand" + "math/rand" ) const ( @@ -61,9 +61,6 @@ func play(strategy0, strategy1 strategy) int { currentPlayer := rand.Intn(2) // Randomly decide who plays first for s.player+s.thisTurn < win { action := strategies[currentPlayer](s) - if action != roll && action != stay { - panic(fmt.Sprintf("Player %d is cheating", currentPlayer)) - } s, turnIsOver = action(s) if turnIsOver { currentPlayer = (currentPlayer + 1) % 2 diff --git a/doc/codewalk/sharemem.xml b/doc/codewalk/sharemem.xml index 1a669f7b5..d443e176e 100644 --- a/doc/codewalk/sharemem.xml +++ b/doc/codewalk/sharemem.xml @@ -65,7 +65,7 @@ and then loops passing completed Resources back to the pending channel after appropriate delays. </step> -<step title="Creating channels" src="doc/codewalk/urlpoll.go:/create our/,/complete/"> +<step title="Creating channels" src="doc/codewalk/urlpoll.go:/Create our/,/complete/"> First, main makes two channels of *Resource, pending and complete. <br/><br/> Inside main, a new goroutine sends one Resource per URL to pending @@ -75,7 +75,7 @@ The pending and complete channels are passed to each of the Poller goroutines, within which they are known as in and out. </step> -<step title="Initializing StateMonitor" src="doc/codewalk/urlpoll.go:/launch the StateMonitor/,/statusInterval/"> +<step title="Initializing StateMonitor" src="doc/codewalk/urlpoll.go:/Launch the StateMonitor/,/statusInterval/"> StateMonitor will initialize and launch a goroutine that stores the state of each Resource. We will look at this function in detail later. <br/><br/> @@ -83,14 +83,14 @@ For now, the important thing to note is that it returns a channel of State, which is saved as status and passed to the Poller goroutines. </step> -<step title="Launching Poller goroutines" src="doc/codewalk/urlpoll.go:/launch some Poller/,/}/"> +<step title="Launching Poller goroutines" src="doc/codewalk/urlpoll.go:/Launch some Poller/,/}/"> Now that it has the necessary channels, main launches a number of Poller goroutines, passing the channels as arguments. The channels provide the means of communication between the main, Poller, and StateMonitor goroutines. </step> -<step title="Send Resources to pending" src="doc/codewalk/urlpoll.go:/send some Resources/,/}\(\)/"> +<step title="Send Resources to pending" src="doc/codewalk/urlpoll.go:/Send some Resources/,/}\(\)/"> To add the initial work to the system, main starts a new goroutine that allocates and sends one Resource per URL to pending. <br/><br/> diff --git a/doc/codewalk/urlpoll.go b/doc/codewalk/urlpoll.go index b51be9502..e716c7e6c 100644 --- a/doc/codewalk/urlpoll.go +++ b/doc/codewalk/urlpoll.go @@ -5,17 +5,16 @@ package main import ( - "http" "log" + "net/http" "time" ) const ( - numPollers = 2 // number of Poller goroutines to launch - second = 1e9 // one second is 1e9 nanoseconds - pollInterval = 60 * second // how often to poll each URL - statusInterval = 10 * second // how often to log status to stdout - errTimeout = 10 * second // back-off timeout on error + numPollers = 2 // number of Poller goroutines to launch + pollInterval = 60 * time.Second // how often to poll each URL + statusInterval = 10 * time.Second // how often to log status to stdout + errTimeout = 10 * time.Second // back-off timeout on error ) var urls = []string{ @@ -33,7 +32,7 @@ type State struct { // StateMonitor maintains a map that stores the state of the URLs being // polled, and prints the current state every updateInterval nanoseconds. // It returns a chan State to which resource state should be sent. -func StateMonitor(updateInterval int64) chan<- State { +func StateMonitor(updateInterval time.Duration) chan<- State { updates := make(chan State) urlStatus := make(map[string]string) ticker := time.NewTicker(updateInterval) @@ -61,7 +60,7 @@ func logState(s map[string]string) { // Resource represents an HTTP URL to be polled by this program. type Resource struct { url string - errCount int64 + errCount int } // Poll executes an HTTP HEAD request for url @@ -71,7 +70,7 @@ func (r *Resource) Poll() string { if err != nil { log.Println("Error", r.url, err) r.errCount++ - return err.String() + return err.Error() } r.errCount = 0 return resp.Status @@ -79,8 +78,8 @@ func (r *Resource) Poll() string { // Sleep sleeps for an appropriate interval (dependant on error state) // before sending the Resource to done. -func (r *Resource) Sleep(done chan *Resource) { - time.Sleep(pollInterval + errTimeout*r.errCount) +func (r *Resource) Sleep(done chan<- *Resource) { + time.Sleep(pollInterval + errTimeout*time.Duration(r.errCount)) done <- r } @@ -93,18 +92,18 @@ func Poller(in <-chan *Resource, out chan<- *Resource, status chan<- State) { } func main() { - // create our input and output channels + // Create our input and output channels. pending, complete := make(chan *Resource), make(chan *Resource) - // launch the StateMonitor + // Launch the StateMonitor. status := StateMonitor(statusInterval) - // launch some Poller goroutines + // Launch some Poller goroutines. for i := 0; i < numPollers; i++ { go Poller(pending, complete, status) } - // send some Resources to the pending queue + // Send some Resources to the pending queue. go func() { for _, url := range urls { pending <- &Resource{url: url} |