Introduction to Go 1.1

TODO - overview - link back to Go 1 and also Go 1 Compatibility docs.

Changes to the language

The Go compatibility document promises that programs written to the Go 1 language specification will continue to operate, and those promises are maintained. In the interest of firming up the specification, though, there are details about some error cases that have been clarified. There are also some new language features.

Integer division by zero

In Go 1, integer division by a constant zero produced a run-time panic:

func f(x int) int {
	return x/0
}

In Go 1.1, an integer division by constant zero is not a legal program, so it is a compile-time error.

Surrogates in Unicode literals

The definition of string and rune literals has been refined to exclude surrogate halves from the set of valid Unicode code points. See the Unicode section for more information.

Method values

Go 1.1 now implements method values, which are functions that have been bound to a specific receiver value. For instance, given a Writer value w, the expression w.Write, a method value, is a function that will always write to w; it is equivalent to a function literal closing over w:

func (p []byte) (n int, err error) {
	return w.Write(n, err)
}

Method values are distinct from method expressions, which generate functions from methods of a given type; the method expression (*bufio.Writer).Write is equivalent to a function with an extra first argument, a receiver of type (*bufio.Writer):

func (w *bufio.Writer, p []byte) (n int, err error) {
	return w.Write(n, err)
}

Updating: No existing code is affected; the change is strictly backward-compatible.

Return requirements

Before Go 1.1, a function that returned a value needed an explicit "return" or call to panic at the end of the function; this was a simple way to make the programmer be explicit about the meaning of the function. But there are many cases where a final "return" is clearly unnecessary, such as a function with only an infinite "for" loop.

In Go 1.1, the rule about final "return" statements is more permissive. It introduces the concept of a terminating statement, a statement that is guaranteed to be the last one a function executes. Examples include "for" loops with no condition and "if-else" statements in which each half ends in a "return". If the final statement of a function can be shown syntactically to be a terminating statement, no final "return" statement is needed.

Note that the rule is purely syntactic: it pays no attention to the values in the code and therefore requires no complex analysis.

Updating: The change is backward-compatible, but existing code with superfluous "return" statements and calls to panic may be simplified manually. Such code can be identified by go vet.

Changes to the implementations and tools

Command-line flag parsing

In the gc tool chain, the compilers and linkers now use the same command-line flag parsing rules as the Go flag package, a departure from the traditional Unix flag parsing. This may affect scripts that invoke the tool directly. For example, go tool 6c -Fw -Dfoo must now be written go tool 6c -F -w -D foo.

Size of int on 64-bit platforms

The language allows the implementation to choose whether the int type and uint types are 32 or 64 bits. Previous Go implementations made int and uint 32 bits on all systems. 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.

Updating: Most programs will be unaffected by this change. Because Go does not allow implicit conversions between distinct numeric types, no programs will stop compiling due to this change. However, programs that contain implicit assumptions that int is only 32 bits may change behavior. For example, this code prints a positive number on 64-bit systems and a negative one on 32-bit systems:

x := ^uint32(0) // x is 0xffffffff
i := int(x)     // i is -1 on 32-bit systems, 0xffffffff on 64-bit
fmt.Println(i)

Portable code intending 32-bit sign extension (yielding -1 on all systems) would instead say:

i := int(int32(x))

Unicode

To make it possible to represent code points greater than 65535 in UTF-16, Unicode defines surrogate halves, a range of code points to be used only in the assembly of large values, and only in UTF-16. The code points in that surrogate range are illegal for any other purpose. In Go 1.1, this constraint is honored by the compiler, libraries, and run-time: a surrogate half is illegal as a rune value, when encoded as UTF-8, or when encoded in isolation as UTF-16. When encountered, for example in converting from a rune to UTF-8, it is treated as an encoding error and will yield the replacement rune, utf8.RuneError, U+FFFD.

This program,

import "fmt"

func main() {
    fmt.Printf("%+q\n", string(0xD800))
}

printed "\ud800" in Go 1.0, but prints "\ufffd" in Go 1.1.

Surrogate-half Unicode values are now illegal in rune and string constants, so constants such as '\ud800' and "\ud800" are now rejected by the compilers. When written explicitly as UTF-8 encoded bytes, such strings can still be created, as in "\xed\xa0\x80". However, when such a string is decoded as a sequence of runes, as in a range loop, it will yield only utf8.RuneError values.

The Unicode byte order marks U+FFFE and U+FEFF, encoded in UTF-8, are now permitted as the first character of a Go source file. Even though their appearance in the byte-order-free UTF-8 encoding is clearly unnecessary, some editors add them as a kind of "magic number" identifying a UTF-8 encoded file.

Updating: Most programs will be unaffected by the surrogate change. Programs that depend on the old behavior should be modified to avoid the issue. The byte-order-mark change is strictly backward-compatible.

The gc assemblers

Due to the change of the int to 64 bits and some other changes, the arrangement of function arguments on the stack has changed in the gc tool chain. Functions written in assembly will need to be revised at least to adjust frame pointer offsets.

Updating: The go vet command now checks that functions implemented in assembly match the Go function prototypes they implement.

Changes to the go command

The go command has acquired several changes intended to improve the experience for new Go users.

First, when compiling, testing, or running Go code, the go command will now give more detailed error messages, including a list of paths searched, when a package cannot be located.

$ 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) 

Second, the go get command no longer allows $GOROOT as the default destination when downloading package source. To use the go get command, a valid $GOPATH is now required.

$ GOPATH= go get code.google.com/p/foo/quxx
package code.google.com/p/foo/quxx: cannot download, $GOPATH not set. For more details see: go help gopath 

Finally, as a result of the previous change, the go get command will also fail when $GOPATH and $GOROOT are set to the same value.

$ GOPATH=$GOROOT go get code.google.com/p/foo/quxx
warning: GOPATH set to GOROOT (/home/User/go) has no effect
package code.google.com/p/foo/quxx: cannot download, $GOPATH must not be set to $GOROOT. For more details see: go help gopath

Changes to the go test command

The go test command no longer deletes the binary when run with profiling enabled, to make it easier to analyze the profile. The implementation sets the -c flag automatically, so after running,

$ go test -cpuprofile cpuprof.out mypackage

the file mypackage.test will be left in the directory where go test was run.

The go test command can now generate profiling information that reports where goroutines are blocked, that is, where they tend to stall waiting for an event such as a channel communication. The information is presented as a blocking profile enabled with the -blockprofile option of go test. Run go help test for more information.

Changes to the go fix command

The fix command, usually run as go fix, no longer applies fixes to update code from before Go 1 to use Go 1 APIs. To update pre-Go 1 code to Go 1.1, use a Go 1.0 tool chain to convert the code to Go 1.0 first.

Performance

TODO introduction

Changes to the standard library

bufio.Scanner

The various routines to scan textual input in the bufio package, ReadBytes, ReadString and particularly ReadLine, are needlessly complex to use for simple purposes. In Go 1.1, a new type, Scanner, has been added to make it easier to do simple tasks such as read the input as a sequence of lines or space-delimited words. It simplifies the problem by terminating the scan on problematic input such as pathologically long lines, and having a simple default: line-oriented input, with each line stripped of its terminator. Here is code to reproduce the input a line at a time:

scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
    fmt.Println(scanner.Text()) // Println will add back the final '\n'
}
if err := scanner.Err(); err != nil {
    fmt.Fprintln(os.Stderr, "reading standard input:", err)
}

Scanning behavior can be adjusted through a function to control subdividing the input (see the documentation for SplitFunc), but for tough problems or the need to continue past errors, the older interface may still be required.

net

The protocol-specific resolvers in the net package were formerly lax about the network name passed in. Although the documentation was clear that the only valid networks for ResolveTCPAddr are "tcp", "tcp4", and "tcp6", the Go 1.0 implementation silently accepted any string. The Go 1.1 implementation returns an error if the network is not one of those strings. The same is true of the other protocol-specific resolvers ResolveIPAddr, ResolveUDPAddr, and ResolveUnixAddr.

The previous implementation of ListenUnixgram returned a UDPConn as a representation of the connection endpoint. The Go 1.1 implementation instead returns a UnixConn to allow reading and writing with its ReadFrom and WriteTo methods.

reflect

The reflect package has several significant additions.

It is now possible to run a "select" statement using the reflect package; see the description of Select and SelectCase for details.

The new method Value.Convert (or Type.ConvertibleTo) provides functionality to execute a Go conversion or type assertion operation on a Value (or test for its possibility).

The new function MakeFunc creates a wrapper function to make it easier to call a function with existing Values, doing the standard Go conversions among the arguments, for instance to pass an actual int to a formal interface{}.

Finally, the new functions ChanOf, MapOf and SliceOf construct new Types from existing types, for example to construct a the type []T given only T.

time

On FreeBSD, Linux, NetBSD, OS X and OpenBSD, previous versions of the time package returned times with microsecond precision. The Go 1.1 implementation on these systems now returns times with nanosecond precision. Programs that write to an external format with microsecond precision and read it back, expecting to recover the original value, will be affected by the loss of precision. There are two new methods of Time, Round and Truncate, that can be used to remove precision from a time before passing it to external storage.

The new method YearDay returns the one-indexed integral day number of the year specified by the time value.

The Timer type has a new method Reset that modifies the timer to expire after a specified duration.

Finally, the new function ParseInLocation is like the existing Parse but parses the time in the context of a location (time zone), ignoring time zone information in the parsed string. This function addresses a common source of confusion in the time API.

Updating: Code that needs to read and write times using an external format with lower precision should be modified to use the new methods.

Exp and old subtrees moved to go.exp and go.text subrepositories

To make it easier for binary distributions to access them if desired, the exp and old source subtrees, which are not included in binary distributions, have been moved to the new go.exp subrepository at code.google.com/p/go.exp. To access the ssa package, for example, run

$ go get code.google.com/p/go.exp/ssa

and then in Go source,

import "code.google.com/p/go.exp/ssa"

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.

Minor changes to the library

The following list summarizes a number of minor changes to the library, mostly additions. See the relevant package documentation for more information about each change.