介绍 google golang 1.1 的新特性


https://go.googlecode.com/hg/doc/go1.1.html

go1.1 和go1 有部分改变:

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.

你可能感兴趣的:(介绍 google golang 1.1 的新特性)