1
0
mirror of https://github.com/golang/go synced 2024-09-23 23:20:14 -06:00
go/doc/go1.2.html
Rob Pike 6cbc5387c3 doc/go1.2.html: introduction, language changes
R=golang-dev, remyoudompheng, dominik.honnef, adg
CC=golang-dev
https://golang.org/cl/13341049
2013-09-10 15:13:45 +10:00

641 lines
21 KiB
HTML

<!--{
"Title": "Go 1.2 Release Notes",
"Path": "/doc/go1.2",
"Template": true
}-->
<h2 id="introduction">Introduction to Go 1.2</h2>
<p>
<font color=red>
RED TEXT IS FROM THE 1.1 DOC AND NEEDS TO BE UPDATED. (It is here for
formatting and style reference.)
</font>
</p>
<p>
Since the release of <a href="/doc/go1.1.html">Go version 1.1</a> in April, 2013,
the release schedule has been shortened to make the release process more efficient.
This release, Go version 1.2 or Go 1.2 for short, arrives roughly six months after 1.1,
while 1.1 took over a year to appear after 1.0.
Because of the shorter time scale, 1.2 is a smaller delta than the step from 1.0 to 1.1,
but it still has some significant developments, including
a better scheduler and one new language feature.
Of course, Go 1.2 keeps the <a href="/doc/go1compat.html">promise
of compatibility</a>.
The overwhelming majority of programs built with Go 1.1 (or 1.0 for that matter)
will run without any changes whatsoever when moved to 1.2,
although the introduction of one restriction
to a corner of the language may expose already-incorrect code
(see the discussion of the <a href="#use_of_nil">use of nil</a>).
</p>
<h2 id="language">Changes to the language</h2>
<p>
In the interest of firming up the specification, one corner case has been clarified,
with consequences for programs.
There is also one new language feature.
</p>
<h3 id="use_of_nil">Use of nil</h3>
<p>
The language now specifies that, for safety reasons,
certain uses of nil pointers are guaranteed to trigger a run-time panic.
For instance, in Go 1.0, given code like
</p>
<pre>
type T struct {
X [1<<24]byte
Field int32
}
func main() {
var x *T
...
}
</pre>
<p>
the <code>nil</code> pointer <code>x</code> could be used to access memory incorrectly:
the expression <code>x.Field</code> could access memory at address <code>1<<24</code>.
To prevent such unsafe behavior, in Go 1.2 the compilers now guarantee that any indirection through
a nil pointer, such as illustrated here but also in nil pointers to arrays, nil interface values,
nil slices, and so on, will either panic or return a correct, safe non-nil value.
In short, any expression that explicitly or implicitly requires evaluation of a nil address is an error.
The implementation may inject extra tests into the compiled program to enforce this behavior.
</p>
<p>
Further details are in the
<a href="http://golang.org/s/go12nil">design document</a>.
</p>
<p>
<em>Updating</em>:
Most code that depended on the old behavior is erroneous and will fail when run.
Such programs will need to be updated by hand.
</p>
<h3 id="three_index">Three-index slices</h3>
<p>
Go 1.2 adds the ability to specify the capacity as well as the length when using a slicing operation
on an existing array or slice.
A slicing operation creates a new slice by describing a contiguous section of an already-created array or slice:
</p>
<pre>
var array [10]int
slice := array[2:4]
</pre>
<p>
The capacity of the slice is the maximum number of elements that the slice may hold, even after reslicing;
it reflects the size of the underlying array.
In this example, the capacity of the <code>slice</code> variable is 8.
</p>
<p>
Go 1.2 adds new syntax to allow a slicing operation to specify the capacity as well as the length.
A second
colon introduces the capacity value, which must be less than or equal to the capacity of the
source slice or array, adjusted for the origin. For instance,
</p>
<pre>
slice = array[2:4:6]
</pre>
<p>
sets the slice to have the same length as in the earlier example but its capacity is now only 4 elements (6-2).
It is impossible to use this new slice value to access the last two elements of the original array.
</p>
<p>
In this three-index notation, a missing first index (<code>[:i:j]</code>) defaults to zero but the other
two indices must always be specified explicitly.
It is possible that future releases of Go may introduce default values for these indices.
</p>
<p>
Further details are in the
<a href="http://golang.org/s/go12slice">design document</a>.
</p>
<p>
<em>Updating</em>:
This is a backwards-compatible change that affects no existing programs.
</p>
<h2 id="impl">Changes to the implementations and tools</h2>
<ul>
<li>
runtime: preemption of goroutines at function entry (CL 12371043).
</li>
<li>
go/build: support including C++ code with cgo (CL 8248043).
</li>
</ul>
<h3 id="go_tools_godoc">Godoc moved to the go.tools subrepository</h3>
<p>
A binary is still included with the distribution, but the source code for the
<code>godoc</code> command has moved to the
<a href="http://code.google.com/p/go.tools">go.tools</a> subrepository.
The core of the program has been split into a
<a href="https://code.google.com/p/go/source/browse/?repo=tools#hg%2Fgodoc">library</a>,
while the command itself is in a separate
<a href="https://code.google.com/p/go/source/browse/?repo=tools#hg%2Fcmd%2Fgodoc">directory</a>.
The move allows the code to be updated easily and the separation into a library and command
makes it easier to construct custom binaries for local sites and different deployment methods.
</p>
<p>
<em>Updating</em>:
Since godoc was not part of the library,
no client code depends on the godoc sources and no updating is required.
</p>
<p>
The binary distributions available from <a href="http://golang.org>golang.org</a>
include a godoc binary, so users of these distributions are unaffected.
</p>
<p>
When building from source, users must use "go get" to install godoc.
</p>
<pre>
$ go get code.google.com/p/go.tools/cmd/godoc
</pre>
<h3 id="go_tools_vet">The vet tool moved to the go.tools subrepository</h3>
<p>
TODO
</p>
<h3 id="gccgo">Status of gccgo</h3>
<p>
<font color=red>
The GCC release schedule does not coincide with the Go release schedule, so some skew is inevitable in
<code>gccgo</code>'s releases.
The 4.8.0 version of GCC shipped in March, 2013 and includes a nearly-Go 1.1 version of <code>gccgo</code>.
Its library is a little behind the release, but the biggest difference is that method values are not implemented.
Sometime around July 2013, we expect 4.8.2 of GCC to ship with a <code>gccgo</code>
providing a complete Go 1.1 implementation.
</font>
</p>
<h3 id="gc_changes">TODO</h3>
<p>
TODO: write prose
</p>
<ul>
<li>cmd/5a: removed support for R9/R10 (use m/g instead) (CL 9840043).
</li>
<li>cmd/5l: add MOVBS, MOVHS etc for sub-word moves (CL 12682043).
</li>
<li>cmd/5l: support for external linking for linux/arm (CL 12871044).
</li>
<li>cmd/cgo, cmd/go: support including C++ code with cgo (CL 8248043).
</li>
<li>cmd/gc: make missing package error fatal (CL 12677043).
</li>
</ul>
<h3 id="gocmd">Changes to the go command</h3>
<ul>
<li>cmd/go: test coverage (CL 10413044).
</li>
<li>cmd/go: add -t flag to 'go get' to download test dependencies (CL 12566046).
</li>
<li>cmd/go: delete 'go doc' (CL 12974043).
</li>
</ul>
<h3 id="platforms">Additional platforms</h3>
<p>
<font color=red>
The Go 1.1 tool chain adds experimental support for <code>freebsd/arm</code>,
<code>netbsd/386</code>, <code>netbsd/amd64</code>, <code>netbsd/arm</code>,
<code>openbsd/386</code> and <code>openbsd/amd64</code> platforms.
</font>
</p>
<p>
<font color=red>
An ARMv6 or later processor is required for <code>freebsd/arm</code> or
<code>netbsd/arm</code>.
</font>
</p>
<p>
<font color=red>
Go 1.1 adds experimental support for <code>cgo</code> on <code>linux/arm</code>.
</font>
</p>
<h2 id="performance">Performance</h2>
<p>
<font color=red>
The performance of code compiled with the Go 1.1 gc tool suite should be noticeably
better for most Go programs.
Typical improvements relative to Go 1.0 seem to be about 30%-40%, sometimes
much more, but occasionally less or even non-existent.
There are too many small performance-driven tweaks through the tools and libraries
to list them all here, but the following major changes are worth noting:
</font>
</p>
<ul>
<li>compress/bzip2: faster decompression by 30% (CL 9915043).
</li>
<li>crypto/des: 5x faster encoding/decoding (CL 11874043, 12072045).
</li>
<li>encoding/json: faster encoding (CL 9129044).
</li>
<li>net: improve windows performance by up to 30% (CL 8670044).
</li>
<li>net: improve performance on BSD by up to 30% (CL 8264043, 12927048, 13080043).
</li>
</ul>
<h2 id="library">Changes to the standard library</h2>
<h3 id="foo_bar">foo.Bar</h3>
<p>
TODO: choose which to call out
<font color=red>
The various routines to scan textual input in the
<a href="/pkg/bufio/"><code>bufio</code></a>
package,
<a href="/pkg/bufio/#Reader.ReadBytes"><code>ReadBytes</code></a>,
<a href="/pkg/bufio/#Reader.ReadString"><code>ReadString</code></a>
and particularly
<a href="/pkg/bufio/#Reader.ReadLine"><code>ReadLine</code></a>,
are needlessly complex to use for simple purposes.
In Go 1.1, a new type,
<a href="/pkg/bufio/#Scanner"><code>Scanner</code></a>,
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:
</font>
<p>
<font color=red>
<em>Updating</em>:
To correct breakage caused by the new struct field,
<code>go fix</code> will rewrite code to add tags for these types.
More generally, <code>go vet</code> will identify composite literals that
should be revised to use field tags.
</font>
</p>
<ul>
<li>
Breaking change:
archive/tar,archive/zip: fix os.FileInfo implementation to provide base name only (CL 13118043).
</li>
<li>
encoding: new package defining generic encoding interfaces (CL 12541051).
</li>
<li>
fmt: indexed access to arguments in Printf etc. (CL 9680043).
</li>
<li>
sync/atomic: add Swap functions (CL 12670045).
</li>
<li>
text/template: add comparison functions (CL 13091045).
</li>
<li>
text/template: allow {{"{{"}}else if ... {{"}}"}} to simplify if chains (CL 13327043).
</li>
</ul>
<h3 id="minor_library_changes">Minor changes to the library</h3>
<p>
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.
</p>
<ul>
<li>
The <a href="/pkg/archive/zip/"><code>archive/zip</code></a> package
adds the
<a href="/pkg/archive/zip/#File.DataOffset"><code>DataOffset</code></a> accessor
to return the offset of a file's (possibly compressed) data within the archive.
</li>
<li>
The <a href="/pkg/bufio/"><code>bufio</code></a> package
adds <a href="/pkg/bufio/#Reader.Reset"><code>Reset</code></a>
methods to <a href="/pkg/bufio/#Reader"><code>Reader</code></a> and
<a href="/pkg/bufio/#Writer"><code>Writer</code></a>.
These methods allow the <a href="/pkg/Reader/"><code>Readers</code></a>
and <a href="/pkg/Writer/"><code>Writers</code></a>
to be re-used on new input and output readers and writers, saving
allocation overhead.
</li>
<li>
The <a href="/pkg/compress/bzip2/"><code>compress/bzip2</code></a>
can now decompress concatenated archives.
</li>
<li>
The <a href="/pkg/compress/flate/"><code>compress/flate</code></a>
package adds a <a href="/pkg/compress/flate/#Reset"><code>Reset</code></a>
method on the <a href="/pkg/compress/flate/#Writer"><code>Writer</code></a>,
allowing compression of one file to start with another's dictionary.
</li>
<li>
compress/gzip: add Reset method on Writer (CL 13435043).
</li>
<li>
The <a href="/pkg/container/heap/"><code>container/heap</code></a> package
adds a <a href="/pkg/container/heap/#Fix"><code>Fix</code></a>
method to provide a more efficient way to update an item's position in the heap.
</li>
<li>
The <a href="/pkg/container/list/"><code>container/list</code></a> package
adds the <a href="/pkg/container/list/#MoveBefore"><code>MoveBefore</code></a>
and
<a href="/pkg/container/list/#MoveAfter"><code>MoveAfter</code></a>
methods, which implement the obvious rearrangement.
</li>
<li>
The <a href="/pkg/crypto/cipher/"><code>crypto/cipher</code></a> package
adds the a new GCM mode (Galois Counter Mode), which is almost always
used with AES encryption.
</li>
<li>
The
<a href="/pkg/crypto/md5/"><code>crypto/md5</code></a> package
adds a new <a href="/pkg/crypto/md5/#Sum"><code>Sum</code></a> function
to simplify hashing without sacrificing performance.
</li>
<li>
Similarly, the
<a href="/pkg/crypto/md5/"><code>crypto/sha1</code></a> package
adds a new <a href="/pkg/crypto/sha1/#Sum"><code>Sum</code></a> function.
</li>
<li>
Also, the
<a href="/pkg/crypto/sha256/"><code>crypto/sha256</code></a> package
adds <a href="/pkg/crypto/sha256/#Sum256"><code>Sum256</code></a>
and <a href="/pkg/crypto/sha256/#Sum224"><code>Sum224</code></a> functions.
</li>
<li>
Finally, the <a href="/pkg/crypto/sha512/"><code>crypto/sha512</code></a> package
adds <a href="/pkg/crypto/sha512/#Sum512"><code>Sum512</code></a> and
<a href="/pkg/crypto/sha512/#Sum384"><code>Sum384</code></a> functions.
</li>
<li>
The <a href="/pkg/crypto/x509/"><code>crypto/x509</code></a> package
adds support for reading and writing arbitrary extensions.
</li>
<li>
The <a href="/pkg/crypto/tls/"><code>crypto/tls</code></a> package adds
support for TLS 1.1, 1.2 and AES-GCM.
</li>
<li>
The <a href="/pkg/database/sql/"><code>database/sql</code></a> package adds a
<a href="/pkg/database/sql/#DB.SetMaxOpenConns"><code>SetMaxOpenConns</code></a>
method on <a href="/pkg/database/sql/#DB"><code>DB</code></a> to limit the
number of open connections to the database.
</li>
<li>
The <a href="/pkg/encoding/csv/"><code>encoding/csv</code></a> package
now always allows trailing commas on fields.
</li>
<li>
The <a href="/pkg/encoding/gob/"><code>encoding/gob</code></a> package
now supports the generic encoding interfaces of the
<a href="/pkg/encoding/"><code>encoding</code></a> package
described above.
</li>
<li>
The <a href="/pkg/encoding/json/"><code>encoding/json</code></a> package
now will always escape ampersands as "\u0026" when printing strings.
It will now accept but correct invalid UTF-8 in
<a href="/pkg/encoding/json/#Marshal"><code>Marshal</code></a>
(such input was previously rejected).
Finally, it now supports the generic encoding interfaces of the
<a href="/pkg/encoding/"><code>encoding</code></a> package
described above.
</li>
<li>
The <a href="/pkg/encoding/xml/"><code>encoding/xml</code></a> package
now allows attributes stored in pointers to be marshaled.
It also supports the generic encoding interfaces of the
<a href="/pkg/encoding/"><code>encoding</code></a> package
described above through the new
<a href="/pkg/encoding/xml/#Marshaler"><code>Marshaler</code></a>,
<a href="/pkg/encoding/xml/#Unmarshaler"><code>Unmarshaler</code></a>,
and related
<a href="/pkg/encoding/xml/#MarshalerAttr"><code>MarshalerAttr</code></a> and
<a href="/pkg/encoding/xml/#UnmarshalerAttr"><code>UnmarshalerAttr</code></a>
interfaces.
</li>
<li>
The <a href="/pkg/flag/"><code>flag</code></a> package now
has a <a href="/pkg/flag/#Getter"><code>Getter</code></a> interface
to allow the value of a flag to be retrieved. Due to the
Go 1 compatibility guidelines, this method cannot be added to the existing
<a href="/pkg/flag/#Value"><code>Value</code></a>
interface, but all the existing standard flag types implement it.
The package also now exports the <a href="/pkg/flag/#CommandLine"><code>CommandLine</code></a>
flag set, which holds the flags from the command line.
</li>
<li>
The <a href="/pkg/go/build/"><code>go/build</code></a> package adds
the <a href="/pkg/go/build/#Package.AllTags"><code>AllTags</code></a> field
to the <a href="/pkg/go/build/#Package"><code>Package</code></a> type,
to make it easier to process build tags.
</li>
<li>
The <a href="/pkg/image/draw/"><code>image/draw</code></a> package now
exports an interface, <a href="/pkg/image/draw/#Drawer"><code>Drawer</code></a>,
that wraps the standard <a href="/pkg/image/draw/#Draw"><code>Draw</code></a> method.
The Porter-Duff operators now implement this interface, in effect binding an operation to
the draw operator rather than providing it explicitly.
Given a paletted image as its destination, the new
<a href="/pkg/image/draw/#FloydSteinberg"><code>FloydSteinberg</code></a>
implementation of the
<a href="/pkg/image/draw/#Drawer"><code>Drawer</code></a>
interface will use the Floyd-Steinberg error diffusion algorithm to draw the image.
To create palettes suitable for such processing, the new
<a href="/pkg/image/draw/#Quantizer"><code>Quantizer</code></a> interface
represents implementations of quantization algorithms that choose a palette
given a full-color image.
There are no implementations of this interface in the library.
</li>
<li>
The <a href="/pkg/image/gif/"><code>image/gif</code></a> package
can now create GIF files using the new
<a href="/pkg/image/gif/#Encode"><code>Encode</code></a>
and <a href="/pkg/image/gif/#EncodeAll"><code>EncodeAll</code></a>
functions.
Their options argument allows specification of an image
<a href="/pkg/image/draw/#Quantizer"><code>Quantizer</code></a> to use;
if it is <code>nil</code>, the generated GIF will use the
<a href="/pkg/image/color/palette/#Plan9"><code>Plan9</code></a>
color map (palette) defined in the new
<a href="/pkg/image/color/palette/"><code>image/color/palette</code></a> package.
The options also specify a
<a href="/pkg/image/draw/#Drawer"><code>Drawer</code></a>
to use to create the output image;
if it is <code>nil</code>, Floyd-Steinberg error diffusion is used.
</li>
<li>
The<a href="/pkg/io/#Copy"><code>Copy</code></a> method of the
<a href="/pkg/io/"><code>io</code></a> package now prioritizes its
arguments differently.
If one argument implements <a href="/pkg/io/#WriterTo"><code>WriterTo</code></a>
and the other implements i<a href="/pkg/o/#ReaderFrom"><code>ReaderFrom</code></a>,
<a href="/pkg/io/#Copy"><code>Copy</code></a> will now invoke
<a href="/pkg/io/#WriterTo"><code>WriterTo</code></a> to do the work,
so that less intermediate buffering is required in general.
</li>
<li>
net: new build tag netgo for building a pure Go net package (CL 7100050).
</li>
<li>
net/http: don't allow sending invalid cookie lines (CL 12204043).
</li>
<li>
net/http: allow ReadResponse with nil *Request parameter (CL 9821043).
</li>
<li>
net/http: allow responses to HEAD requests, detect type and length (CL 12583043).
</li>
<li>
The <a href="/pkg/runtime/"><code>runtime</code></a> package relaxes
the constraints on finalizer functions in
<a href="/pkg/runtime/#SetFinalizer"><code>SetFinalizer</code></a>: the
actual argument can now be any type that is assignable to the formal type of
the function, as is the case for any normal function call in Go.
</li>
<li>
The <a href="/pkg/sort/"><code>sort</code></a> package has a new
<a href="/pkg/sort/#Stable"><code>Stable</code></a> function that implements
stable sorting. It is less efficient than the normal sort algorithm, however.
</li>
<li>
The <a href="/pkg/strings/"><code>strings</code></a> package adds
an <a href="/pkg/strings/#IndexByte"><code>IndexByte</code></a>
function for consistency with the <a href="/pkg/bytes/"><code>bytes</code></a> package.
</li>
<li>
syscall: implemented Sendfile for Darwin, added Syscall9 for Darwin/amd64 (CL 10980043).
</li>
<li>
The <a href="/pkg/testing/"><code>testing</code></a> package
now exports the<a href="/pkg/testing/#TB"><code>TB</code></a> interface.
It records the methods in common with the
<a href="/pkg/testing/#T"><code>T</code></a>
and
<a href="/pkg/testing/#B"><code>B</code></a> types,
to make it easier to share code between tests and benchmarks.
Also, the
<a href="/pkg/testing/#AllocsPerRun"><code>AllocsPerRun</code></a>
function now quantizes the return value to an integer (although it
still has type <code>float64</code>), to round off any error caused by
initialization and make the result more repeatable.
</li>
<li>
The <a href="/pkg/text/template/"><code>text/template</code></a> package
now automatically dereferences pointer values when evaluating the arguments
to "escape" functions such as "html", to bring the behavior of such functions
in agreement with that of other printing functions such as "printf".
</li>
<li>
In the <a href="/pkg/time/"><code>time</code></a> package, the
<a href="/pkg/time/#Parse"><code>Parse</code></a> function
and
<a href="/pkg/time/#Format"><code>Format</code></a>
method
now handle time zone offsets with seconds, such as in the historical
date "1871-01-01T05:33:02+00:34:08".
Also, pattern matching in the formats for those routines is stricter: a non-lowercase letter
must now follow the standard words such as "Jan" and "Mon".
</li>
<li>
The <a href="/pkg/unicode/"><code>unicode</code></a> package
adds <a href="/pkg/unicode/#In"><code>In</code></a>,
a nicer-to-use but equivalent version of the original
<a href="/pkg/unicode/#IsOneOf"><code>IsOneOf</code></a>,
to see whether a character is a member of a Unicode category.
</li>
</ul>