blob: 51698198d376c96db2839ec3c5a7465883516c78 [file] [log] [blame]
What's new in Go 1.1
Andrew Gerrand
* Go 1.1
Not finished yet. Monday maybe?
* Language changes
* Integer division by zero
Integer division by constant zero is now a compile-time error:
.play go1.1/intdiv.go /func f/,/}/
* Unicode
Byte order marks are now permitted as the first character of a Go source file.
The misuse of "surrogate halves" is now caught by the compiler, libraries, and run-time.
If you don't know what this means, then it likely doesn't matter to you.
* Method values
Sometimes you want to turn a method into a plain function (binding it to its receiver). This is often referred to as "currying".
To do this under Go 1.0, you would use a closure:
.play go1.1/methodvals-old.go /var/,/hello/
Go 1.1 now implements method values, so you can achieve the same thing by simply taking the method as a value:
.play go1.1/methodvals.go /var/,/hello/
* Return requirements (1/2)
Before Go 1.1, a function that returned a value needed an explicit `return` or call to `panic` at the end of the function.
.code go1.1/return-old.go /func slurp/,/^}/
.code go1.1/return-old.go /func min/,/^}/
* Return requirements (2/2)
In Go 1.1, the rule about final "return" statements is more permissive.
.code go1.1/return.go /func slurp/,/^}/
.code go1.1/return.go /func min/,/^}/
* Implementation and tools
* Size of int on 64-bit architectures
Under Go 1.0, `int` and `uint` were 32 bits wide on all systems.
With Go 1.1, both the `gc` and `gccgo` implementations now make `int` and `uint` 64 bits on 64-bit platforms such as AMD64/x86-64.
Among other things, this enables the allocation of slices with more than 2 billion elements on 64-bit platforms.
* Heap size on 64-bit architectures
On 64-bit architectures, the maximum heap size has been enlarged substantially, from a few gigabytes to several tens of gigabytes. (The exact details depend on the system and may change.)
On 32-bit architectures, the heap size has not changed.
* Race detector
A major addition to the tools is a race detector, a way to find bugs in concurrent programs. To enable it, set the `-race` flag when building or testing your program.
.play go1.1/race.go
For now, it is only available on 64-bit Linux, OS X, and Windows systems.
* The go command
The go command now gives better error messages:
$ go build foo/quxx
can't load package: package foo/quxx: cannot find package "foo/quxx" in any of:
/home/you/go/src/pkg/foo/quxx (from $GOROOT)
/home/you/src/foo/quxx (from $GOPATH)
To help users better understand workspaces and `GOPATH`, the `go`get` command will fail if `GOPATH` is not set:
$ GOPATH= go get
package cannot download, $GOPATH not set.
For more details see: go help gopath
The `go`get` command will also fail if `GOPATH` and `GOROOT` have the same value:
warning: GOPATH set to GOROOT (/home/you/go) has no effect
package cannot download, $GOPATH must not be set to $GOROOT.
For more details see: go help gopath
* The go test command (1/2)
The `go`test` command no longer deletes the binary when run with profiling enabled, to make it easier to analyze the profile. After running
$ go test -cpuprofile cpuprof.out mypackage
the file `mypackage.test` will be left around for analysis.
* The go test command (2/2)
The `go`test` command can now generate profiling information that reports where goroutines are blocked, enabled with the `-blockprofile` option. The information is presented as a [[go1.1/blockprofile.svg][blocking profile]].
.image go1.1/blockprofile.png
* Build constraints
The "go1.1" tag has been added to the list of default build constraints.
To build a file only with Go 1.1 and above, add this build constraint:
// +build go1.1
To build a file only with Go 1.0.x, use the converse constraint:
// +build !go1.1
This permits packages to take advantage of the new features in Go 1.1 while remaining compatible with earlier versions of Go.
* Additional platforms
The Go 1.1 tool chain adds experimental support for `freebsd/arm`, `netbsd/386`, `netbsd/amd64`, `netbsd/arm`, `openbsd/386` and `openbsd/amd64` platforms.
An ARMv6 or later processor is required for `freebsd/arm` or `netbsd/arm`.
Go 1.1 adds experimental support for `cgo` on `linux/arm`.
* Standard library
* bufio.Scanner (1/2)
The old way:
.code go1.1/scanner.go /STARTold/,/STOP/
The new way:
.play go1.1/scanner.go /STARTnew/,/STOP/
* bufio.Scanner (2/2)
Custom split functions:
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)
For example, counting words:
.play go1.1/scanner2.go /START/,/STOP/
* reflect (1/2)
The new functions `ChanOf`, `MapOf` and `SliceOf` construct new types from existing types, for example to construct the type `chan`T` given only `T`.
.play go1.1/chanof.go /func/,$
* reflect (2/2)
The new function `MakeFunc` creates a wrapper function to make it easier to call a function with existing `Values`.
.play go1.1/makefunc.go /func/,$
* time (1/4)
On FreeBSD, Linux, NetBSD, OS X and OpenBSD, previous versions of the `time` package returned times with microsecond precision. Go 1.1 now provides nanosecond precision on these systems.
* time (2/4)
The new method on `time.Time`, `YearDay`, provides the one-indexed day number of the year.
.play go1.1/yearday.go /YearDay/
* time (3/4)
The `Timer` type has a new method `Reset` that modifies the timer to expire after a specified duration. This helps to avoid unnecessary repeated `Timer` allocations.
.play go1.1/timer.go /func main/,$
* time (4/4)
The new function `ParseInLocation` parses the time in the context of a time zone, ignoring time zone information in the parsed string. go1.1/ParseInLocation.go
This function addresses a common source of confusion in the time API.
* bytes and strings
`TrimPrefix` and `TrimSuffix` functions have been added to the `bytes` and `strings` packages.
Code under Go 1.0 like this:
s := "Please pass the salt"
if p := "Please "; strings.HasPrefix(s, p) {
s = s[len(p):]
May now be rewritten as:
s := "Please pass the salt"
s = strings.TrimPrefix(s, "Please ")
* Exp and old subtrees moved
The exp and old source subtrees, which are not included in binary distributions, have been moved to the new `go.exp` sub-repository at ``.
To access the `ssa` package, for example, run
$ go get
and then in Go source,
import ""
The old package `exp/norm` has also been moved, but to a new repository `go.text`, where the Unicode APIs and other text-related packages will be developed.
* Performance
* Performance
Code compiled with the Go 1.1 gc tool suite should be noticeably better for most Go programs.
Improvements relative to Go 1.0 seem to be about 30%-40%, sometimes much more, but occasionally less or even non-existent.
Major changes include:
- Better code generation by the gc compilers,
- In-lining of small functions by the gc compilers,
- A new map implementation that uses less memory and CPU time,
- A more parallel and precise garbage collector implementation,
- A new scheduler,
- Tighter coupling of the run-time and network libraries, resulting in fewer context switches on network operations.