From: Rob Pike Date: Fri, 20 Jan 2012 23:38:03 +0000 (-0800) Subject: doc/go1: flag, runtime, testing X-Git-Tag: weekly.2012-01-27~128 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=531ded922f4eeb8c4634924b935599165b9f407b;p=gostls13.git doc/go1: flag, runtime, testing R=golang-dev, dsymonds, gri CC=golang-dev https://golang.org/cl/5557076 --- diff --git a/doc/go1.html b/doc/go1.html index 34e4f9cd84..77bde0adac 100644 --- a/doc/go1.html +++ b/doc/go1.html @@ -564,15 +564,15 @@ Several packages have moved under exp at the time of Go 1's release

-Also, the utf8.String type has been moved to its own package, exp/utf8string. +All these packages are available under the same names, with the prefix exp/: exp/ebnf etc.

-All these packages are available under the same names, with exp/ prefixed: exp/ebnf etc. +Also, the utf8.String type has been moved to its own package, exp/utf8string.

-Also, the gotype command now resides in exp/gotype, while +Finally, the gotype command now resides in exp/gotype, while ebnflint is now in exp/ebnflint

@@ -850,6 +850,32 @@ to be implemented in the future. No changes will be needed.

+

The flag package

+ +

+In Go 1, the interface flag.Value has changed slightly. +The Set method now returns an error instead of +a bool to indicate success or failure. +

+ +

+There is also a new kind of flag, Duration, to support argument +values specifying time intervals. +Values for such flags must be given units, just as time.Duration +formats them: 10s, 1h30m, etc. +

+ +
var timeout = flag.Duration("timeout", 30*time.Second, "how long to wait for completion")
+ +

+Updating: +Programs that implement their own flags will need minor manual fixes to update their +Set methods. +The Duration flag is new and affects no existing code. +

+ +

The go/* packages

@@ -914,7 +940,6 @@ compiler will reject incorrect uses. Templates used in conjuction with any of th to run-time errors.

-

The hash package

@@ -1064,6 +1089,20 @@ and os.FileMode API. Code that needs system-specific file details will need to be updated by hand.

+

The runtime package

+ +

+The runtime package in Go 1 includes a new niladic function, +runtime.NumCPU, that returns the number of CPUs available +for parallel execution, as reported by the operating system kernel. +Its value can inform the setting of GOMAXPROCS. +

+ +

+Updating: +No existing code is affected. +

+

The strconv package

@@ -1159,6 +1198,35 @@ a cast that must be added by hand; gofix will warn about it.

+

The testing package

+ +

+The testing package has a type, B, passed as an argument to benchmark functions. +In Go 1, B has new methods, analogous to those of T, enabling +logging and failure reporting. +

+ +
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)
+    }
+}
+ +

+Updating: +Existing code is unaffected, although benchmarks that use println +or panic should be updated to the new interface. +

+

The go command

Packaged releases

diff --git a/doc/go1.tmpl b/doc/go1.tmpl index 0518d08139..51dd0baca8 100644 --- a/doc/go1.tmpl +++ b/doc/go1.tmpl @@ -488,15 +488,15 @@ Several packages have moved under exp at the time of Go 1's release

-Also, the utf8.String type has been moved to its own package, exp/utf8string. +All these packages are available under the same names, with the prefix exp/: exp/ebnf etc.

-All these packages are available under the same names, with exp/ prefixed: exp/ebnf etc. +Also, the utf8.String type has been moved to its own package, exp/utf8string.

-Also, the gotype command now resides in exp/gotype, while +Finally, the gotype command now resides in exp/gotype, while ebnflint is now in exp/ebnflint

@@ -754,6 +754,31 @@ to be implemented in the future. No changes will be needed.

+

The flag package

+ +

+In Go 1, the interface flag.Value has changed slightly. +The Set method now returns an error instead of +a bool to indicate success or failure. +

+ +

+There is also a new kind of flag, Duration, to support argument +values specifying time intervals. +Values for such flags must be given units, just as time.Duration +formats them: 10s, 1h30m, etc. +

+ +{{code "progs/go1.go" `/timeout/`}} + +

+Updating: +Programs that implement their own flags will need minor manual fixes to update their +Set methods. +The Duration flag is new and affects no existing code. +

+ +

The go/* packages

@@ -818,7 +843,6 @@ compiler will reject incorrect uses. Templates used in conjuction with any of th to run-time errors.

-

The hash package

@@ -968,6 +992,20 @@ and os.FileMode API. Code that needs system-specific file details will need to be updated by hand.

+

The runtime package

+ +

+The runtime package in Go 1 includes a new niladic function, +runtime.NumCPU, that returns the number of CPUs available +for parallel execution, as reported by the operating system kernel. +Its value can inform the setting of GOMAXPROCS. +

+ +

+Updating: +No existing code is affected. +

+

The strconv package

@@ -1063,6 +1101,22 @@ a cast that must be added by hand; gofix will warn about it.

+

The testing package

+ +

+The testing package has a type, B, passed as an argument to benchmark functions. +In Go 1, B has new methods, analogous to those of T, enabling +logging and failure reporting. +

+ +{{code "progs/go1.go" `/func.*Benchmark/` `/^}/`}} + +

+Updating: +Existing code is unaffected, although benchmarks that use println +or panic should be updated to the new interface. +

+

The go command

Packaged releases

diff --git a/doc/progs/go1.go b/doc/progs/go1.go index 0eccca321b..0348aa315e 100644 --- a/doc/progs/go1.go +++ b/doc/progs/go1.go @@ -8,13 +8,16 @@ package main import ( "errors" + "flag" "fmt" "log" + "testing" "time" "unicode" ) func main() { + flag.Parse() stringAppend() mapDelete() mapIteration() @@ -26,6 +29,8 @@ func main() { timePackage() } +var timeout = flag.Duration("timeout", 30*time.Second, "how long to wait for completion") + func mapDelete() { m := map[string]int{"7": 7, "23": 23} k := "7" @@ -187,3 +192,17 @@ func init() { go initializationFunction(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) + } +}