介绍 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, ReadBytesReadString 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 ResolveIPAddrResolveUDPAddr, 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 ChanOfMapOf 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 TimeRound 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.

本文来自:开源中国博客

感谢作者:freewebsys

查看原文:介绍 google golang 1.1 的新特性

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。