Commit 531ded92 authored by Rob Pike's avatar Rob Pike

doc/go1: flag, runtime, testing

R=golang-dev, dsymonds, gri
CC=golang-dev
https://golang.org/cl/5557076
parent 14d7e869
...@@ -564,15 +564,15 @@ Several packages have moved under <code>exp</code> at the time of Go 1's release ...@@ -564,15 +564,15 @@ Several packages have moved under <code>exp</code> at the time of Go 1's release
</ul> </ul>
<p> <p>
Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>. All these packages are available under the same names, with the prefix <code>exp/</code>: <code>exp/ebnf</code> etc.
</p> </p>
<p> <p>
All these packages are available under the same names, with <code>exp/</code> prefixed: <code>exp/ebnf</code> etc. Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>.
</p> </p>
<p> <p>
Also, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while Finally, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while
<code>ebnflint</code> is now in <code>exp/ebnflint</code> <code>ebnflint</code> is now in <code>exp/ebnflint</code>
</p> </p>
...@@ -850,6 +850,32 @@ to be implemented in the future. ...@@ -850,6 +850,32 @@ to be implemented in the future.
No changes will be needed. No changes will be needed.
</p> </p>
<h3 id="flag">The flag package</h3>
<p>
In Go 1, the interface <a href="/pkg/flag/#Value"><code>flag.Value</code></a> has changed slightly.
The <code>Set</code> method now returns an <code>error</code> instead of
a <code>bool</code> to indicate success or failure.
</p>
<p>
There is also a new kind of flag, <code>Duration</code>, to support argument
values specifying time intervals.
Values for such flags must be given units, just as <code>time.Duration</code>
formats them: <code>10s</code>, <code>1h30m</code>, etc.
</p>
<pre><!--{{code "progs/go1.go" `/timeout/`}}
-->var timeout = flag.Duration(&#34;timeout&#34;, 30*time.Second, &#34;how long to wait for completion&#34;)</pre>
<p>
<em>Updating</em>:
Programs that implement their own flags will need minor manual fixes to update their
<code>Set</code> methods.
The <code>Duration</code> flag is new and affects no existing code.
</p>
<h3 id="go">The go/* packages</h3> <h3 id="go">The go/* packages</h3>
<p> <p>
...@@ -914,7 +940,6 @@ compiler will reject incorrect uses. Templates used in conjuction with any of th ...@@ -914,7 +940,6 @@ compiler will reject incorrect uses. Templates used in conjuction with any of th
to run-time errors. to run-time errors.
</p> </p>
<h3 id="hash">The hash package</h3> <h3 id="hash">The hash package</h3>
<p> <p>
...@@ -1064,6 +1089,20 @@ and <code>os.FileMode</code> API. ...@@ -1064,6 +1089,20 @@ and <code>os.FileMode</code> API.
Code that needs system-specific file details will need to be updated by hand. Code that needs system-specific file details will need to be updated by hand.
</p> </p>
<h3 id="runtime">The runtime package</h3>
<p>
The <code>runtime</code> package in Go 1 includes a new niladic function,
<a href="/pkg/runtime/#NumCPU"><code>runtime.NumCPU</code></a>, that returns the number of CPUs available
for parallel execution, as reported by the operating system kernel.
Its value can inform the setting of <code>GOMAXPROCS</code>.
</p>
<p>
<em>Updating</em>:
No existing code is affected.
</p>
<h3 id="strconv">The strconv package</h3> <h3 id="strconv">The strconv package</h3>
<p> <p>
...@@ -1159,6 +1198,35 @@ a cast that must be added by hand; gofix will warn about it. ...@@ -1159,6 +1198,35 @@ a cast that must be added by hand; gofix will warn about it.
</p> </p>
<h3 id="testing">The testing package</h3>
<p>
The testing package has a type, <code>B</code>, passed as an argument to benchmark functions.
In Go 1, <code>B</code> has new methods, analogous to those of <code>T</code>, enabling
logging and failure reporting.
</p>
<pre><!--{{code "progs/go1.go" `/func.*Benchmark/` `/^}/`}}
-->func BenchmarkSprintf(b *testing.B) {
// Verify correctness before running benchmark.
b.StopTimer()
got := fmt.Sprintf(&#34;%x&#34;, 23)
const expect = &#34;17&#34;
if expect != got {
b.Fatalf(&#34;expected %q; got %q&#34;, expect, got)
}
b.StartTimer()
for i := 0; i &lt; b.N; i++ {
fmt.Sprintf(&#34;%x&#34;, 23)
}
}</pre>
<p>
<em>Updating</em>:
Existing code is unaffected, although benchmarks that use <code>println</code>
or <code>panic</code> should be updated to the new interface.
</p>
<h2 id="go_command">The go command</h2> <h2 id="go_command">The go command</h2>
<h2 id="releases">Packaged releases</h2> <h2 id="releases">Packaged releases</h2>
......
...@@ -488,15 +488,15 @@ Several packages have moved under <code>exp</code> at the time of Go 1's release ...@@ -488,15 +488,15 @@ Several packages have moved under <code>exp</code> at the time of Go 1's release
</ul> </ul>
<p> <p>
Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>. All these packages are available under the same names, with the prefix <code>exp/</code>: <code>exp/ebnf</code> etc.
</p> </p>
<p> <p>
All these packages are available under the same names, with <code>exp/</code> prefixed: <code>exp/ebnf</code> etc. Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>.
</p> </p>
<p> <p>
Also, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while Finally, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while
<code>ebnflint</code> is now in <code>exp/ebnflint</code> <code>ebnflint</code> is now in <code>exp/ebnflint</code>
</p> </p>
...@@ -754,6 +754,31 @@ to be implemented in the future. ...@@ -754,6 +754,31 @@ to be implemented in the future.
No changes will be needed. No changes will be needed.
</p> </p>
<h3 id="flag">The flag package</h3>
<p>
In Go 1, the interface <a href="/pkg/flag/#Value"><code>flag.Value</code></a> has changed slightly.
The <code>Set</code> method now returns an <code>error</code> instead of
a <code>bool</code> to indicate success or failure.
</p>
<p>
There is also a new kind of flag, <code>Duration</code>, to support argument
values specifying time intervals.
Values for such flags must be given units, just as <code>time.Duration</code>
formats them: <code>10s</code>, <code>1h30m</code>, etc.
</p>
{{code "progs/go1.go" `/timeout/`}}
<p>
<em>Updating</em>:
Programs that implement their own flags will need minor manual fixes to update their
<code>Set</code> methods.
The <code>Duration</code> flag is new and affects no existing code.
</p>
<h3 id="go">The go/* packages</h3> <h3 id="go">The go/* packages</h3>
<p> <p>
...@@ -818,7 +843,6 @@ compiler will reject incorrect uses. Templates used in conjuction with any of th ...@@ -818,7 +843,6 @@ compiler will reject incorrect uses. Templates used in conjuction with any of th
to run-time errors. to run-time errors.
</p> </p>
<h3 id="hash">The hash package</h3> <h3 id="hash">The hash package</h3>
<p> <p>
...@@ -968,6 +992,20 @@ and <code>os.FileMode</code> API. ...@@ -968,6 +992,20 @@ and <code>os.FileMode</code> API.
Code that needs system-specific file details will need to be updated by hand. Code that needs system-specific file details will need to be updated by hand.
</p> </p>
<h3 id="runtime">The runtime package</h3>
<p>
The <code>runtime</code> package in Go 1 includes a new niladic function,
<a href="/pkg/runtime/#NumCPU"><code>runtime.NumCPU</code></a>, that returns the number of CPUs available
for parallel execution, as reported by the operating system kernel.
Its value can inform the setting of <code>GOMAXPROCS</code>.
</p>
<p>
<em>Updating</em>:
No existing code is affected.
</p>
<h3 id="strconv">The strconv package</h3> <h3 id="strconv">The strconv package</h3>
<p> <p>
...@@ -1063,6 +1101,22 @@ a cast that must be added by hand; gofix will warn about it. ...@@ -1063,6 +1101,22 @@ a cast that must be added by hand; gofix will warn about it.
</p> </p>
<h3 id="testing">The testing package</h3>
<p>
The testing package has a type, <code>B</code>, passed as an argument to benchmark functions.
In Go 1, <code>B</code> has new methods, analogous to those of <code>T</code>, enabling
logging and failure reporting.
</p>
{{code "progs/go1.go" `/func.*Benchmark/` `/^}/`}}
<p>
<em>Updating</em>:
Existing code is unaffected, although benchmarks that use <code>println</code>
or <code>panic</code> should be updated to the new interface.
</p>
<h2 id="go_command">The go command</h2> <h2 id="go_command">The go command</h2>
<h2 id="releases">Packaged releases</h2> <h2 id="releases">Packaged releases</h2>
......
...@@ -8,13 +8,16 @@ package main ...@@ -8,13 +8,16 @@ package main
import ( import (
"errors" "errors"
"flag"
"fmt" "fmt"
"log" "log"
"testing"
"time" "time"
"unicode" "unicode"
) )
func main() { func main() {
flag.Parse()
stringAppend() stringAppend()
mapDelete() mapDelete()
mapIteration() mapIteration()
...@@ -26,6 +29,8 @@ func main() { ...@@ -26,6 +29,8 @@ func main() {
timePackage() timePackage()
} }
var timeout = flag.Duration("timeout", 30*time.Second, "how long to wait for completion")
func mapDelete() { func mapDelete() {
m := map[string]int{"7": 7, "23": 23} m := map[string]int{"7": 7, "23": 23}
k := "7" k := "7"
...@@ -187,3 +192,17 @@ func init() { ...@@ -187,3 +192,17 @@ func init() {
go initializationFunction(c) go initializationFunction(c)
PackageGlobal = <-c PackageGlobal = <-c
} }
func BenchmarkSprintf(b *testing.B) {
// Verify correctness before running benchmark.
b.StopTimer()
got := fmt.Sprintf("%x", 23)
const expect = "17"
if expect != got {
b.Fatalf("expected %q; got %q", expect, got)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf("%x", 23)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment