blob: b9d36f29c3dd68c94276bf7995e9c79d16204d94 [file] [log] [blame]
Rob Pike5863b7d2013-09-09 13:29:08 +10001<!--{
2 "Title": "Go 1.2 Release Notes",
3 "Path": "/doc/go1.2",
4 "Template": true
5}-->
6
7<h2 id="introduction">Introduction to Go 1.2</h2>
8
9<p>
Rob Pike6cbc5382013-09-10 15:13:45 +100010Since the release of <a href="/doc/go1.1.html">Go version 1.1</a> in April, 2013,
11the release schedule has been shortened to make the release process more efficient.
12This release, Go version 1.2 or Go 1.2 for short, arrives roughly six months after 1.1,
13while 1.1 took over a year to appear after 1.0.
14Because of the shorter time scale, 1.2 is a smaller delta than the step from 1.0 to 1.1,
15but it still has some significant developments, including
16a better scheduler and one new language feature.
17Of course, Go 1.2 keeps the <a href="/doc/go1compat.html">promise
18of compatibility</a>.
19The overwhelming majority of programs built with Go 1.1 (or 1.0 for that matter)
20will run without any changes whatsoever when moved to 1.2,
21although the introduction of one restriction
22to a corner of the language may expose already-incorrect code
23(see the discussion of the <a href="#use_of_nil">use of nil</a>).
Rob Pike5863b7d2013-09-09 13:29:08 +100024</p>
25
26<h2 id="language">Changes to the language</h2>
27
28<p>
Rob Pike6cbc5382013-09-10 15:13:45 +100029In the interest of firming up the specification, one corner case has been clarified,
30with consequences for programs.
31There is also one new language feature.
Rob Pike5863b7d2013-09-09 13:29:08 +100032</p>
33
Rob Pike6cbc5382013-09-10 15:13:45 +100034<h3 id="use_of_nil">Use of nil</h3>
Rob Pike5863b7d2013-09-09 13:29:08 +100035
36<p>
Rob Pike6cbc5382013-09-10 15:13:45 +100037The language now specifies that, for safety reasons,
38certain uses of nil pointers are guaranteed to trigger a run-time panic.
39For instance, in Go 1.0, given code like
Rob Pike5863b7d2013-09-09 13:29:08 +100040</p>
41
Rob Pike6cbc5382013-09-10 15:13:45 +100042<pre>
43type T struct {
44 X [1<<24]byte
45 Field int32
46}
47
48func main() {
49 var x *T
50 ...
51}
52</pre>
53
54<p>
55the <code>nil</code> pointer <code>x</code> could be used to access memory incorrectly:
56the expression <code>x.Field</code> could access memory at address <code>1<<24</code>.
57To prevent such unsafe behavior, in Go 1.2 the compilers now guarantee that any indirection through
58a nil pointer, such as illustrated here but also in nil pointers to arrays, nil interface values,
59nil slices, and so on, will either panic or return a correct, safe non-nil value.
60In short, any expression that explicitly or implicitly requires evaluation of a nil address is an error.
61The implementation may inject extra tests into the compiled program to enforce this behavior.
62</p>
63
64<p>
65Further details are in the
Andrew Gerrand43ad89d2014-07-25 10:28:39 +100066<a href="//golang.org/s/go12nil">design document</a>.
Rob Pike6cbc5382013-09-10 15:13:45 +100067</p>
68
69<p>
70<em>Updating</em>:
71Most code that depended on the old behavior is erroneous and will fail when run.
72Such programs will need to be updated by hand.
73</p>
74
75<h3 id="three_index">Three-index slices</h3>
76
77<p>
78Go 1.2 adds the ability to specify the capacity as well as the length when using a slicing operation
79on an existing array or slice.
80A slicing operation creates a new slice by describing a contiguous section of an already-created array or slice:
81</p>
82
83<pre>
84var array [10]int
85slice := array[2:4]
86</pre>
87
88<p>
89The capacity of the slice is the maximum number of elements that the slice may hold, even after reslicing;
90it reflects the size of the underlying array.
91In this example, the capacity of the <code>slice</code> variable is 8.
92</p>
93
94<p>
95Go 1.2 adds new syntax to allow a slicing operation to specify the capacity as well as the length.
96A second
97colon introduces the capacity value, which must be less than or equal to the capacity of the
98source slice or array, adjusted for the origin. For instance,
99</p>
100
101<pre>
Rob Pike4977f9f2013-09-23 14:41:20 +1000102slice = array[2:4:7]
Rob Pike6cbc5382013-09-10 15:13:45 +1000103</pre>
104
105<p>
Rob Pike4977f9f2013-09-23 14:41:20 +1000106sets the slice to have the same length as in the earlier example but its capacity is now only 5 elements (7-2).
107It is impossible to use this new slice value to access the last three elements of the original array.
Rob Pike6cbc5382013-09-10 15:13:45 +1000108</p>
109
110<p>
111In this three-index notation, a missing first index (<code>[:i:j]</code>) defaults to zero but the other
112two indices must always be specified explicitly.
113It is possible that future releases of Go may introduce default values for these indices.
114</p>
115
116<p>
117Further details are in the
Andrew Gerrand43ad89d2014-07-25 10:28:39 +1000118<a href="//golang.org/s/go12slice">design document</a>.
Rob Pike6cbc5382013-09-10 15:13:45 +1000119</p>
120
121<p>
122<em>Updating</em>:
123This is a backwards-compatible change that affects no existing programs.
124</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000125
126<h2 id="impl">Changes to the implementations and tools</h2>
127
Rob Pikee4c1fff2013-09-21 17:53:44 +1000128<h3 id="preemption">Pre-emption in the scheduler</h3>
Rob Pike5863b7d2013-09-09 13:29:08 +1000129
Rob Pikee4c1fff2013-09-21 17:53:44 +1000130<p>
131In prior releases, a goroutine that was looping forever could starve out other
132goroutines on the same thread, a serious problem when GOMAXPROCS
133provided only one user thread.
Dominik Honneff7011612013-09-22 07:28:52 +1000134In Go 1.2, this is partially addressed: The scheduler is invoked occasionally
Rob Pikee4c1fff2013-09-21 17:53:44 +1000135upon entry to a function.
136This means that any loop that includes a (non-inlined) function call can
137be pre-empted, allowing other goroutines to run on the same thread.
138</p>
139
Rob Pike0f706d32013-10-30 08:54:53 -0700140<h3 id="thread_limit">Limit on the number of threads</h3>
141
142<p>
143Go 1.2 introduces a configurable limit (default 10,000) to the total number of threads
144a single program may have in its address space, to avoid resource starvation
145issues in some environments.
146Note that goroutines are multiplexed onto threads so this limit does not directly
147limit the number of goroutines, only the number that may be simultaneously blocked
148in a system call.
149In practice, the limit is hard to reach.
150</p>
151
152<p>
153The new <a href="/pkg/runtime/debug/#SetMaxThreads"><code>SetMaxThreads</code></a> function in the
154<a href="/pkg/runtime/debug/"><code>runtime/debug</code></a> package controls the thread count limit.
155</p>
156
157<p>
158<em>Updating</em>:
159Few functions will be affected by the limit, but if a program dies because it hits the
160limit, it could be modified to call <code>SetMaxThreads</code> to set a higher count.
161Even better would be to refactor the program to need fewer threads, reducing consumption
162of kernel resources.
163</p>
164
165<h3 id="stack_size">Stack size</h3>
166
167<p>
168In Go 1.2, the minimum size of the stack when a goroutine is created has been lifted from 4KB to 8KB.
169Many programs were suffering performance problems with the old size, which had a tendency
170to introduce expensive stack-segment switching in performance-critical sections.
171The new number was determined by empirical testing.
172</p>
173
174<p>
175At the other end, the new function <a href="/pkg/runtime/debug/#SetMaxStack"><code>SetMaxStack</code></a>
176in the <a href="/pkg/runtime/debug"><code>runtime/debug</code></a> package controls
177the <em>maximum</em> size of a single goroutine's stack.
178The default is 1GB on 64-bit systems and 250MB on 32-bit systems.
179Before Go 1.2, it was too easy for a runaway recursion to consume all the memory on a machine.
180</p>
181
182<p>
183<em>Updating</em>:
184The increased minimum stack size may cause programs with many goroutines to use
Rob Pike95ae85f2013-10-30 09:39:20 -0700185more memory. There is no workaround, but plans for future releases
Rob Pike0f706d32013-10-30 08:54:53 -0700186include new stack management technology that should address the problem better.
187</p>
188
Rob Pikee4c1fff2013-09-21 17:53:44 +1000189<h3 id="cgo_and_cpp">Cgo and C++</h3>
190
191<p>
192The <a href="/cmd/cgo/"><code>cgo</code></a> command will now invoke the C++
Andrew Gerrandc1ef8452013-11-05 13:54:48 +1100193compiler to build any pieces of the linked-to library that are written in C++;
194<a href="/cmd/cgo/">the documentation</a> has more detail.
Rob Pikee4c1fff2013-09-21 17:53:44 +1000195</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000196
Rob Pikeea78a4a2013-09-12 16:46:08 +1000197<h3 id="go_tools_godoc">Godoc and vet moved to the go.tools subrepository</h3>
Rob Pike6cbc5382013-09-10 15:13:45 +1000198
199<p>
Rob Pikeea78a4a2013-09-12 16:46:08 +1000200Both binaries are still included with the distribution, but the source code for the
201godoc and vet commands has moved to the
Andrew Gerrand43ad89d2014-07-25 10:28:39 +1000202<a href="//code.google.com/p/go.tools">go.tools</a> subrepository.
Rob Pikeea78a4a2013-09-12 16:46:08 +1000203</p>
204
205<p>
206Also, the core of the godoc program has been split into a
Rob Pike6cbc5382013-09-10 15:13:45 +1000207<a href="https://code.google.com/p/go/source/browse/?repo=tools#hg%2Fgodoc">library</a>,
208while the command itself is in a separate
209<a href="https://code.google.com/p/go/source/browse/?repo=tools#hg%2Fcmd%2Fgodoc">directory</a>.
210The move allows the code to be updated easily and the separation into a library and command
211makes it easier to construct custom binaries for local sites and different deployment methods.
212</p>
213
214<p>
215<em>Updating</em>:
Rob Pikeea78a4a2013-09-12 16:46:08 +1000216Since godoc and vet are not part of the library,
217no client Go code depends on the their source and no updating is required.
Rob Pike6cbc5382013-09-10 15:13:45 +1000218</p>
219
220<p>
Andrew Gerrand43ad89d2014-07-25 10:28:39 +1000221The binary distributions available from <a href="//golang.org">golang.org</a>
Rob Pikeea78a4a2013-09-12 16:46:08 +1000222include these binaries, so users of these distributions are unaffected.
Rob Pike6cbc5382013-09-10 15:13:45 +1000223</p>
224
225<p>
Rob Pikeea78a4a2013-09-12 16:46:08 +1000226When building from source, users must use "go get" to install godoc and vet.
Shenghou Mad00fb0a2013-09-23 01:23:42 -0400227(The binaries will continue to be installed in their usual locations, not
228<code>$GOPATH/bin</code>.)
Rob Pike6cbc5382013-09-10 15:13:45 +1000229</p>
230
231<pre>
232$ go get code.google.com/p/go.tools/cmd/godoc
Rob Pikeea78a4a2013-09-12 16:46:08 +1000233$ go get code.google.com/p/go.tools/cmd/vet
Rob Pike6cbc5382013-09-10 15:13:45 +1000234</pre>
235
Rob Pike5863b7d2013-09-09 13:29:08 +1000236<h3 id="gccgo">Status of gccgo</h3>
237
238<p>
Rob Pikec0ac6672013-09-12 10:12:26 +1000239We expect the future GCC 4.9 release to include gccgo with full
240support for Go 1.2.
241In the current (4.8.2) release of GCC, gccgo implements Go 1.1.2.
Rob Pike5863b7d2013-09-09 13:29:08 +1000242</p>
243
Rob Pike83da0fd2013-09-16 13:03:00 +1000244<h3 id="gc_changes">Changes to the gc compiler and linker</h3>
Rob Pike5863b7d2013-09-09 13:29:08 +1000245
246<p>
Rob Pike83da0fd2013-09-16 13:03:00 +1000247Go 1.2 has several semantic changes to the workings of the gc compiler suite.
248Most users will be unaffected by them.
Rob Pike5863b7d2013-09-09 13:29:08 +1000249</p>
250
Rob Pike83da0fd2013-09-16 13:03:00 +1000251<p>
252The <a href="/cmd/cgo/"><code>cgo</code></a> command now
253works when C++ is included in the library being linked against.
254See the <a href="/cmd/cgo/"><code>cgo</code></a> documentation
255for details.
256</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000257
Rob Pike83da0fd2013-09-16 13:03:00 +1000258<p>
259The gc compiler displayed a vestigial detail of its origins when
260a program had no <code>package</code> clause: it assumed
261the file was in package <code>main</code>.
262The past has been erased, and a missing <code>package</code> clause
263is now an error.
264</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000265
Rob Pike83da0fd2013-09-16 13:03:00 +1000266<p>
267On the ARM, the toolchain supports "external linking", which
268is a step towards being able to build shared libraries with the gc
269tool chain and to provide dynamic linking support for environments
270in which that is necessary.
271</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000272
Rob Pike83da0fd2013-09-16 13:03:00 +1000273<p>
274In the runtime for the ARM, with <code>5a</code>, it used to be possible to refer
275to the runtime-internal <code>m</code> (machine) and <code>g</code>
276(goroutine) variables using <code>R9</code> and <code>R10</code> directly.
277It is now necessary to refer to them by their proper names.
278</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000279
Rob Pike83da0fd2013-09-16 13:03:00 +1000280<p>
281Also on the ARM, the <code>5l</code> linker (sic) now defines the
282<code>MOVBS</code> and <code>MOVHS</code> instructions
283as synonyms of <code>MOVB</code> and <code>MOVH</code>,
284to make clearer the separation between signed and unsigned
285sub-word moves; the unsigned versions already existed with a
286<code>U</code> suffix.
287</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000288
Rob Pikeea78a4a2013-09-12 16:46:08 +1000289<h3 id="cover">Test coverage</h3>
290
291<p>
292One major new feature of <a href="/pkg/go/"><code>go test</code></a> is
293that it can now compute and, with help from a new, separately installed
294"go tool cover" program, display test coverage results.
295</p>
296
297<p>
298The cover tool is part of the
299<a href="https://code.google.com/p/go/source/checkout?repo=tools"><code>go.tools</code></a>
300subrepository.
301It can be installed by running
302</p>
303
304<pre>
305$ go get code.google.com/p/go.tools/cmd/cover
306</pre>
307
308<p>
309The cover tool does two things.
310First, when "go test" is given the <code>-cover</code> flag, it is run automatically
311to rewrite the source for the package and insert instrumentation statements.
312The test is then compiled and run as usual, and basic coverage statistics are reported:
313</p>
314
315<pre>
316$ go test -cover fmt
317ok fmt 0.060s coverage: 91.4% of statements
318$
319</pre>
320
321<p>
322Second, for more detailed reports, different flags to "go test" can create a coverage profile file,
323which the cover program, invoked with "go tool cover", can then analyze.
324</p>
325
326<p>
327Details on how to generate and analyze coverage statistics can be found by running the commands
328</p>
329
330<pre>
331$ go help testflag
332$ go tool cover -help
333</pre>
334
335<h3 id="go_doc">The go doc command is deleted</h3>
336
337<p>
338The "go doc" command is deleted.
339Note that the <a href="/cmd/godoc/"><code>godoc</code></a> tool itself is not deleted,
340just the wrapping of it by the <a href="/cmd/go/"><code>go</code></a> command.
341All it did was show the documents for a package by package path,
342which godoc itself already does with more flexibility.
343It has therefore been deleted to reduce the number of documentation tools and,
344as part of the restructuring of godoc, encourage better options in future.
345</p>
346
347<p>
348<em>Updating</em>: For those who still need the precise functionality of running
349</p>
350
351<pre>
352$ go doc
353</pre>
354
355<p>
356in a directory, the behavior is identical to running
357</p>
358
359<pre>
360$ godoc .
361</pre>
362
Rob Pike5863b7d2013-09-09 13:29:08 +1000363<h3 id="gocmd">Changes to the go command</h3>
364
Rob Pikeea78a4a2013-09-12 16:46:08 +1000365<p>
366The <a href="/cmd/go/"><code>go get</code></a> command
367now has a <code>-t</code> flag that causes it to download the dependencies
368of the tests run by the package, not just those of the package itself.
369By default, as before, dependencies of the tests are not downloaded.
370</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000371
Rob Pike5863b7d2013-09-09 13:29:08 +1000372<h2 id="performance">Performance</h2>
373
374<p>
Rob Piked237f3c2013-09-16 10:28:53 +1000375There are a number of significant performance improvements in the standard library; here are a few of them.
Rob Pike5863b7d2013-09-09 13:29:08 +1000376</p>
377
Rob Piked237f3c2013-09-16 10:28:53 +1000378<ul>
379
380<li>
381The <a href="/pkg/compress/bzip2/"><code>compress/bzip2</code></a>
382decompresses about 30% faster.
Rob Pike5863b7d2013-09-09 13:29:08 +1000383</li>
384
Rob Piked237f3c2013-09-16 10:28:53 +1000385<li>
386The <a href="/pkg/crypto/des/"><code>crypto/des</code></a> package
387is about five times faster.
Rob Pike5863b7d2013-09-09 13:29:08 +1000388</li>
389
Rob Piked237f3c2013-09-16 10:28:53 +1000390<li>
391The <a href="/pkg/encoding/json/"><code>encoding/json</code></a> package
392encodes about 30% faster.
Rob Pike5863b7d2013-09-09 13:29:08 +1000393</li>
394
Rob Piked237f3c2013-09-16 10:28:53 +1000395<li>
396Networking performance on Windows and BSD systems is about 30% faster through the use
397of an integrated network poller in the runtime, similar to what was done for Linux and OS X
398in Go 1.1.
Rob Pike5863b7d2013-09-09 13:29:08 +1000399</li>
400
Rob Pike5863b7d2013-09-09 13:29:08 +1000401</ul>
402
403<h2 id="library">Changes to the standard library</h2>
404
Rob Pikea97a7c52013-09-12 09:08:59 +1000405
406<h3 id="archive_tar_zip">The archive/tar and archive/zip packages</h3>
Rob Pike5863b7d2013-09-09 13:29:08 +1000407
408<p>
Rob Piked237f3c2013-09-16 10:28:53 +1000409The
410<a href="/pkg/archive/tar/"><code>archive/tar</code></a>
411and
412<a href="/pkg/archive/zip/"><code>archive/zip</code></a>
413packages have had a change to their semantics that may break existing programs.
414The issue is that they both provided an implementation of the
415<a href="/pkg/os/#FileInfo"><code>os.FileInfo</code></a>
416interface that was not compliant with the specification for that interface.
417In particular, their <code>Name</code> method returned the full
418path name of the entry, but the interface specification requires that
419the method return only the base name (final path element).
420</p>
421
422<p>
423<em>Updating</em>: Since this behavior was newly implemented and
424a bit obscure, it is possible that no code depends on the broken behavior.
425If there are programs that do depend on it, they will need to be identified
426and fixed manually.
Rob Pike5863b7d2013-09-09 13:29:08 +1000427</p>
428
Rob Pikea97a7c52013-09-12 09:08:59 +1000429<h3 id="encoding">The new encoding package</h3>
Rob Pike5863b7d2013-09-09 13:29:08 +1000430
Rob Pikea97a7c52013-09-12 09:08:59 +1000431<p>
Rob Piked237f3c2013-09-16 10:28:53 +1000432There is a new package, <a href="/pkg/encoding/"><code>encoding</code></a>,
433that defines a set of standard encoding interfaces that may be used to
434build custom marshalers and unmarshalers for packages such as
435<a href="/pkg/encoding/xml/"><code>encoding/xml</code></a>,
436<a href="/pkg/encoding/json/"><code>encoding/json</code></a>,
437and
438<a href="/pkg/encoding/binary/"><code>encoding/binary</code></a>.
439These new interfaces have been used to tidy up some implementations in
440the standard library.
441</p>
442
443<p>
444The new interfaces are called
445<a href="/pkg/encoding/#BinaryMarshaler"><code>BinaryMarshaler</code></a>,
446<a href="/pkg/encoding/#BinaryUnmarshaler"><code>BinaryUnmarshaler</code></a>,
447<a href="/pkg/encoding/#TextMarshaler"><code>TextMarshaler</code></a>,
448and
449<a href="/pkg/encoding/#TextUnmarshaler"><code>TextUnmarshaler</code></a>.
450Full details are in the <a href="/pkg/encoding/">documentation</a> for the package
Andrew Gerrand43ad89d2014-07-25 10:28:39 +1000451and a separate <a href="//golang.org/s/go12encoding">design document</a>.
Rob Pikea97a7c52013-09-12 09:08:59 +1000452</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000453
Rob Pikea97a7c52013-09-12 09:08:59 +1000454<h3 id="fmt_indexed_arguments">The fmt package</h3>
Rob Pike5863b7d2013-09-09 13:29:08 +1000455
Rob Pikea97a7c52013-09-12 09:08:59 +1000456<p>
457The <a href="/pkg/fmt/"><code>fmt</code></a> package's formatted print
458routines such as <a href="/pkg/fmt/#Printf"><code>Printf</code></a>
459now allow the data items to be printed to be accessed in arbitrary order
460by using an indexing operation in the formatting specifications.
461Wherever an argument is to be fetched from the argument list for formatting,
462either as the value to be formatted or as a width or specification integer,
463a new optional indexing notation <code>[</code><em>n</em><code>]</code>
464fetches argument <em>n</em> instead.
465The value of <em>n</em> is 1-indexed.
466After such an indexing operating, the next argument to be fetched by normal
467processing will be <em>n</em>+1.
468</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000469
Rob Pikea97a7c52013-09-12 09:08:59 +1000470<p>
471For example, the normal <code>Printf</code> call
472</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000473
Rob Pikea97a7c52013-09-12 09:08:59 +1000474<pre>
475fmt.Sprintf("%c %c %c\n", 'a', 'b', 'c')
476</pre>
477
478<p>
479would create the string <code>"a b c"</code>, but with indexing operations like this,
480</p>
481
482<pre>
483fmt.Sprintf("%[3]c %[1]c %c\n", 'a', 'b', 'c')
484</pre>
485
486<p>
487the result is "<code>"c a b"</code>. The <code>[3]</code> index accesses the third formatting
Rob Piked237f3c2013-09-16 10:28:53 +1000488argument, which is <code>'c'</code>, <code>[1]</code> accesses the first, <code>'a'</code>,
Rob Pikea97a7c52013-09-12 09:08:59 +1000489and then the next fetch accesses the argument following that one, <code>'b'</code>.
490</p>
491
492<p>
493The motivation for this feature is programmable format statements to access
494the arguments in different order for localization, but it has other uses:
495</p>
496
497<pre>
498log.Printf("trace: value %v of type %[1]T\n", expensiveFunction(a.b[c]))
499</pre>
500
501<p>
502<em>Updating</em>: The change to the syntax of format specifications
503is strictly backwards compatible, so it affects no working programs.
504</p>
505
506<h3 id="text_template">The text/template and html/template packages</h3>
507
508<p>
509The
510<a href="/pkg/text/template/"><code>text/template</code></a> package
511has a couple of changes in Go 1.2, both of which are also mirrored in the
512<a href="/pkg/html/template/"><code>html/template</code></a> package.
513</p>
514
515<p>
516First, there are new default functions for comparing basic types.
517The functions are listed in this table, which shows their names and
518the associated familiar comparison operator.
519</p>
520
521<table cellpadding="0" summary="Template comparison functions">
522<tr>
523<th width="50"></th><th width="100">Name</th> <th width="50">Operator</th>
524</tr>
525<tr>
526<td></td><td><code>eq</code></td> <td><code>==</code></td>
527</tr>
528<tr>
529<td></td><td><code>ne</code></td> <td><code>!=</code></td>
530</tr>
531<tr>
532<td></td><td><code>lt</code></td> <td><code>&lt;</code></td>
533</tr>
534<tr>
535<td></td><td><code>le</code></td> <td><code>&lt;=</code></td>
536</tr>
537<tr>
538<td></td><td><code>gt</code></td> <td><code>&gt;</code></td>
539</tr>
540<tr>
541<td></td><td><code>ge</code></td> <td><code>&gt;=</code></td>
542</tr>
543</table>
544
545<p>
546These functions behave slightly differently from the corresponding Go operators.
547First, they operate only on basic types (<code>bool</code>, <code>int</code>,
548<code>float64</code>, <code>string</code>, etc.).
549(Go allows comparison of arrays and structs as well, under some circumstances.)
550Second, values can be compared as long as they are the same sort of value:
551any signed integer value can be compared to any other signed integer value for example. (Go
552does not permit comparing an <code>int8</code> and an <code>int16</code>).
553Finally, the <code>eq</code> function (only) allows comparison of the first
554argument with one or more following arguments. The template in this example,
555</p>
556
557<pre>
558{{"{{"}}if eq .A 1 2 3 {{"}}"}} equal {{"{{"}}else{{"}}"}} not equal {{"{{"}}end{{"}}"}}
559</pre>
560
561<p>
562reports "equal" if <code>.A</code> is equal to <em>any</em> of 1, 2, or 3.
563</p>
564
565<p>
566The second change is that a small addition to the grammar makes "if else if" chains easier to write.
567Instead of writing,
568</p>
569
570<pre>
571{{"{{"}}if eq .A 1{{"}}"}} X {{"{{"}}else{{"}}"}} {{"{{"}}if eq .A 2{{"}}"}} Y {{"{{"}}end{{"}}"}} {{"{{"}}end{{"}}"}}
572</pre>
573
574<p>
575one can fold the second "if" into the "else" and have only one "end", like this:
576</p>
577
578<pre>
579{{"{{"}}if eq .A 1{{"}}"}} X {{"{{"}}else if eq .A 2{{"}}"}} Y {{"{{"}}end{{"}}"}}
580</pre>
581
582<p>
583The two forms are identical in effect; the difference is just in the syntax.
584</p>
585
586<p>
Rob Pikeea78a4a2013-09-12 16:46:08 +1000587<em>Updating</em>: Neither the "else if" change nor the comparison functions
588affect existing programs. Those that
Rob Pikea97a7c52013-09-12 09:08:59 +1000589already define functions called <code>eq</code> and so on through a function
590map are unaffected because the associated function map will override the new
591default function definitions.
592</p>
Rob Pike5863b7d2013-09-09 13:29:08 +1000593
Nigel Tao765479c2013-09-16 16:26:07 +1000594<h3 id="new_packages">New packages</h3>
595
596<p>
597There are two new packages.
598</p>
599
600<ul>
601<li>
602The <a href="/pkg/encoding/"><code>encoding</code></a> package is
603<a href="#encoding">described above</a>.
604</li>
605<li>
606The <a href="/pkg/image/color/palette/"><code>image/color/palette</code></a> package
607provides standard color palettes.
608</li>
609</ul>
610
Rob Pike5863b7d2013-09-09 13:29:08 +1000611<h3 id="minor_library_changes">Minor changes to the library</h3>
612
613<p>
614The following list summarizes a number of minor changes to the library, mostly additions.
615See the relevant package documentation for more information about each change.
616</p>
617
618<ul>
619
620<li>
621The <a href="/pkg/archive/zip/"><code>archive/zip</code></a> package
622adds the
623<a href="/pkg/archive/zip/#File.DataOffset"><code>DataOffset</code></a> accessor
624to return the offset of a file's (possibly compressed) data within the archive.
625</li>
626
627<li>
628The <a href="/pkg/bufio/"><code>bufio</code></a> package
629adds <a href="/pkg/bufio/#Reader.Reset"><code>Reset</code></a>
630methods to <a href="/pkg/bufio/#Reader"><code>Reader</code></a> and
631<a href="/pkg/bufio/#Writer"><code>Writer</code></a>.
Andrew Gerrandcf694aa2013-10-09 07:05:43 +1100632These methods allow the <a href="/pkg/io/#Reader"><code>Readers</code></a>
633and <a href="/pkg/io/#Writer"><code>Writers</code></a>
Rob Pike5863b7d2013-09-09 13:29:08 +1000634to be re-used on new input and output readers and writers, saving
635allocation overhead.
636</li>
637
638<li>
639The <a href="/pkg/compress/bzip2/"><code>compress/bzip2</code></a>
640can now decompress concatenated archives.
641</li>
642
643<li>
644The <a href="/pkg/compress/flate/"><code>compress/flate</code></a>
Andrew Gerrandcf694aa2013-10-09 07:05:43 +1100645package adds a <a href="/pkg/compress/flate/#Writer.Reset"><code>Reset</code></a>
Rob Pike5863b7d2013-09-09 13:29:08 +1000646method on the <a href="/pkg/compress/flate/#Writer"><code>Writer</code></a>,
Rob Pikeea78a4a2013-09-12 16:46:08 +1000647to make it possible to reduce allocation when, for instance, constructing an
648archive to hold multiple compressed files.
Rob Pike5863b7d2013-09-09 13:29:08 +1000649</li>
650
651<li>
Rob Pikeea78a4a2013-09-12 16:46:08 +1000652The <a href="/pkg/compress/gzip/"><code>compress/gzip</code></a> package's
653<a href="/pkg/compress/gzip/#Writer"><code>Writer</code></a> type adds a
654<a href="/pkg/compress/gzip/#Writer.Reset"><code>Reset</code></a>
655so it may be reused.
656</li>
657
658<li>
659The <a href="/pkg/compress/zlib/"><code>compress/zlib</code></a> package's
660<a href="/pkg/compress/zlib/#Writer"><code>Writer</code></a> type adds a
661<a href="/pkg/compress/zlib/#Writer.Reset"><code>Reset</code></a>
662so it may be reused.
Rob Pike5863b7d2013-09-09 13:29:08 +1000663</li>
664
665<li>
666The <a href="/pkg/container/heap/"><code>container/heap</code></a> package
667adds a <a href="/pkg/container/heap/#Fix"><code>Fix</code></a>
668method to provide a more efficient way to update an item's position in the heap.
669</li>
670
671<li>
672The <a href="/pkg/container/list/"><code>container/list</code></a> package
Andrew Gerrandcf694aa2013-10-09 07:05:43 +1100673adds the <a href="/pkg/container/list/#List.MoveBefore"><code>MoveBefore</code></a>
Rob Pike5863b7d2013-09-09 13:29:08 +1000674and
Andrew Gerrandcf694aa2013-10-09 07:05:43 +1100675<a href="/pkg/container/list/#List.MoveAfter"><code>MoveAfter</code></a>
Rob Pike5863b7d2013-09-09 13:29:08 +1000676methods, which implement the obvious rearrangement.
677</li>
678
679<li>
680The <a href="/pkg/crypto/cipher/"><code>crypto/cipher</code></a> package
681adds the a new GCM mode (Galois Counter Mode), which is almost always
682used with AES encryption.
683</li>
684
685<li>
686The
687<a href="/pkg/crypto/md5/"><code>crypto/md5</code></a> package
688adds a new <a href="/pkg/crypto/md5/#Sum"><code>Sum</code></a> function
689to simplify hashing without sacrificing performance.
690</li>
691
692<li>
693Similarly, the
694<a href="/pkg/crypto/md5/"><code>crypto/sha1</code></a> package
695adds a new <a href="/pkg/crypto/sha1/#Sum"><code>Sum</code></a> function.
696</li>
697
698<li>
699Also, the
700<a href="/pkg/crypto/sha256/"><code>crypto/sha256</code></a> package
701adds <a href="/pkg/crypto/sha256/#Sum256"><code>Sum256</code></a>
702and <a href="/pkg/crypto/sha256/#Sum224"><code>Sum224</code></a> functions.
703</li>
704
705<li>
706Finally, the <a href="/pkg/crypto/sha512/"><code>crypto/sha512</code></a> package
707adds <a href="/pkg/crypto/sha512/#Sum512"><code>Sum512</code></a> and
708<a href="/pkg/crypto/sha512/#Sum384"><code>Sum384</code></a> functions.
709</li>
710
711<li>
712The <a href="/pkg/crypto/x509/"><code>crypto/x509</code></a> package
713adds support for reading and writing arbitrary extensions.
714</li>
715
716<li>
717The <a href="/pkg/crypto/tls/"><code>crypto/tls</code></a> package adds
718support for TLS 1.1, 1.2 and AES-GCM.
719</li>
720
721<li>
722The <a href="/pkg/database/sql/"><code>database/sql</code></a> package adds a
723<a href="/pkg/database/sql/#DB.SetMaxOpenConns"><code>SetMaxOpenConns</code></a>
724method on <a href="/pkg/database/sql/#DB"><code>DB</code></a> to limit the
725number of open connections to the database.
726</li>
727
728<li>
729The <a href="/pkg/encoding/csv/"><code>encoding/csv</code></a> package
730now always allows trailing commas on fields.
731</li>
732
733<li>
734The <a href="/pkg/encoding/gob/"><code>encoding/gob</code></a> package
Rob Piked237f3c2013-09-16 10:28:53 +1000735now treats channel and function fields of structures as if they were unexported,
736even if they are not. That is, it ignores them completely. Previously they would
737trigger an error, which could cause unexpected compatibility problems if an
738embedded structure added such a field.
Russ Cox7dd086e2013-11-13 21:29:19 -0500739The package also now supports the generic <code>BinaryMarshaler</code> and
740<code>BinaryUnmarshaler</code> interfaces of the
Rob Pike5863b7d2013-09-09 13:29:08 +1000741<a href="/pkg/encoding/"><code>encoding</code></a> package
742described above.
743</li>
744
745<li>
746The <a href="/pkg/encoding/json/"><code>encoding/json</code></a> package
Rob Pike6cbc5382013-09-10 15:13:45 +1000747now will always escape ampersands as "\u0026" when printing strings.
Rob Pike5863b7d2013-09-09 13:29:08 +1000748It will now accept but correct invalid UTF-8 in
749<a href="/pkg/encoding/json/#Marshal"><code>Marshal</code></a>
750(such input was previously rejected).
751Finally, it now supports the generic encoding interfaces of the
752<a href="/pkg/encoding/"><code>encoding</code></a> package
753described above.
754</li>
755
756<li>
757The <a href="/pkg/encoding/xml/"><code>encoding/xml</code></a> package
758now allows attributes stored in pointers to be marshaled.
759It also supports the generic encoding interfaces of the
760<a href="/pkg/encoding/"><code>encoding</code></a> package
761described above through the new
762<a href="/pkg/encoding/xml/#Marshaler"><code>Marshaler</code></a>,
Rob Pike6cbc5382013-09-10 15:13:45 +1000763<a href="/pkg/encoding/xml/#Unmarshaler"><code>Unmarshaler</code></a>,
Rob Pike5863b7d2013-09-09 13:29:08 +1000764and related
765<a href="/pkg/encoding/xml/#MarshalerAttr"><code>MarshalerAttr</code></a> and
766<a href="/pkg/encoding/xml/#UnmarshalerAttr"><code>UnmarshalerAttr</code></a>
767interfaces.
Rob Piked237f3c2013-09-16 10:28:53 +1000768The package also adds a
769<a href="/pkg/encoding/xml/#Encoder.Flush"><code>Flush</code></a> method
770to the
771<a href="/pkg/encoding/xml/#Encoder"><code>Encoder</code></a>
772type for use by custom encoders. See the documentation for
773<a href="/pkg/encoding/xml/#Encoder.EncodeToken"><code>EncodeToken</code></a>
774to see how to use it.
Rob Pike5863b7d2013-09-09 13:29:08 +1000775</li>
776
777<li>
778The <a href="/pkg/flag/"><code>flag</code></a> package now
779has a <a href="/pkg/flag/#Getter"><code>Getter</code></a> interface
780to allow the value of a flag to be retrieved. Due to the
781Go 1 compatibility guidelines, this method cannot be added to the existing
782<a href="/pkg/flag/#Value"><code>Value</code></a>
783interface, but all the existing standard flag types implement it.
784The package also now exports the <a href="/pkg/flag/#CommandLine"><code>CommandLine</code></a>
785flag set, which holds the flags from the command line.
786</li>
787
788<li>
Rob Pike4be17b72013-09-25 16:17:54 +1000789The <a href="/pkg/go/ast/"><code>go/ast</code></a> package's
790<a href="/pkg/go/ast/#SliceExpr"><code>SliceExpr</code></a> struct
791has a new boolean field, <code>Slice3</code>, which is set to true
792when representing a slice expression with three indices (two colons).
793The default is false, representing the usual two-index form.
794</li>
795
796<li>
Rob Pike5863b7d2013-09-09 13:29:08 +1000797The <a href="/pkg/go/build/"><code>go/build</code></a> package adds
Andrew Gerrandcf694aa2013-10-09 07:05:43 +1100798the <code>AllTags</code> field
Rob Pike5863b7d2013-09-09 13:29:08 +1000799to the <a href="/pkg/go/build/#Package"><code>Package</code></a> type,
800to make it easier to process build tags.
801</li>
802
803<li>
804The <a href="/pkg/image/draw/"><code>image/draw</code></a> package now
805exports an interface, <a href="/pkg/image/draw/#Drawer"><code>Drawer</code></a>,
806that wraps the standard <a href="/pkg/image/draw/#Draw"><code>Draw</code></a> method.
807The Porter-Duff operators now implement this interface, in effect binding an operation to
808the draw operator rather than providing it explicitly.
809Given a paletted image as its destination, the new
810<a href="/pkg/image/draw/#FloydSteinberg"><code>FloydSteinberg</code></a>
811implementation of the
812<a href="/pkg/image/draw/#Drawer"><code>Drawer</code></a>
813interface will use the Floyd-Steinberg error diffusion algorithm to draw the image.
814To create palettes suitable for such processing, the new
815<a href="/pkg/image/draw/#Quantizer"><code>Quantizer</code></a> interface
816represents implementations of quantization algorithms that choose a palette
817given a full-color image.
818There are no implementations of this interface in the library.
819</li>
820
821<li>
822The <a href="/pkg/image/gif/"><code>image/gif</code></a> package
823can now create GIF files using the new
824<a href="/pkg/image/gif/#Encode"><code>Encode</code></a>
825and <a href="/pkg/image/gif/#EncodeAll"><code>EncodeAll</code></a>
826functions.
827Their options argument allows specification of an image
828<a href="/pkg/image/draw/#Quantizer"><code>Quantizer</code></a> to use;
829if it is <code>nil</code>, the generated GIF will use the
830<a href="/pkg/image/color/palette/#Plan9"><code>Plan9</code></a>
831color map (palette) defined in the new
832<a href="/pkg/image/color/palette/"><code>image/color/palette</code></a> package.
833The options also specify a
834<a href="/pkg/image/draw/#Drawer"><code>Drawer</code></a>
835to use to create the output image;
836if it is <code>nil</code>, Floyd-Steinberg error diffusion is used.
837</li>
838
839<li>
Mikio Hara28a8e9b2013-09-12 15:12:40 +0900840The <a href="/pkg/io/#Copy"><code>Copy</code></a> method of the
Rob Pike5863b7d2013-09-09 13:29:08 +1000841<a href="/pkg/io/"><code>io</code></a> package now prioritizes its
842arguments differently.
843If one argument implements <a href="/pkg/io/#WriterTo"><code>WriterTo</code></a>
Mikio Hara28a8e9b2013-09-12 15:12:40 +0900844and the other implements <a href="/pkg/io/#ReaderFrom"><code>ReaderFrom</code></a>,
Rob Pike5863b7d2013-09-09 13:29:08 +1000845<a href="/pkg/io/#Copy"><code>Copy</code></a> will now invoke
846<a href="/pkg/io/#WriterTo"><code>WriterTo</code></a> to do the work,
847so that less intermediate buffering is required in general.
848</li>
849
850<li>
Rob Pikeea78a4a2013-09-12 16:46:08 +1000851The <a href="/pkg/net/"><code>net</code></a> package requires cgo by default
852because the host operating system must in general mediate network call setup.
853On some systems, though, it is possible to use the network without cgo, and useful
854to do so, for instance to avoid dynamic linking.
855The new build tag <code>netgo</code> (off by default) allows the construction of a
856<code>net</code> package in pure Go on those systems where it is possible.
Rob Pike5863b7d2013-09-09 13:29:08 +1000857</li>
858
859<li>
Mikio Hara28a8e9b2013-09-12 15:12:40 +0900860The <a href="/pkg/net/"><code>net</code></a> package adds a new field
861<code>DualStack</code> to the <a href="/pkg/net/#Dialer"><code>Dialer</code></a>
862struct for TCP connection setup using a dual IP stack as described in
863<a href="http://tools.ietf.org/html/rfc6555">RFC 6555</a>.
864</li>
865
866<li>
Rob Piked237f3c2013-09-16 10:28:53 +1000867The <a href="/pkg/net/http/"><code>net/http</code></a> package will no longer
868transmit cookies that are incorrect according to
869<a href="http://tools.ietf.org/html/rfc6265">RFC 6265</a>.
870It just logs an error and sends nothing.
871Also,
872the <a href="/pkg/net/http/"><code>net/http</code></a> package's
873<a href="/pkg/net/http/#ReadResponse"><code>ReadResponse</code></a>
874function now permits the <code>*Request</code> parameter to be <code>nil</code>,
875whereupon it assumes a GET request.
876Finally, an HTTP server will now serve HEAD
877requests transparently, without the need for special casing in handler code.
878While serving a HEAD request, writes to a
879<a href="/pkg/net/http/#Handler"><code>Handler</code></a>'s
880<a href="/pkg/net/http/#ResponseWriter"><code>ResponseWriter</code></a>
881are absorbed by the
882<a href="/pkg/net/http/#Server"><code>Server</code></a>
883and the client receives an empty body as required by the HTTP specification.
Rob Pike5863b7d2013-09-09 13:29:08 +1000884</li>
885
886<li>
Andrew Gerrandd851c6d2013-09-23 15:14:26 +1000887The <a href="/pkg/os/exec/"><code>os/exec</code></a> package's
888<a href="/pkg/os/exec/#Cmd.StdinPipe"><code>Cmd.StdinPipe</code></a> method
889returns an <code>io.WriteCloser</code>, but has changed its concrete
890implementation from <code>*os.File</code> to an unexported type that embeds
891<code>*os.File</code>, and it is now safe to close the returned value.
892Before Go 1.2, there was an unavoidable race that this change fixes.
893Code that needs access to the methods of <code>*os.File</code> can use an
894interface type assertion, such as <code>wc.(interface{ Sync() error })</code>.
895</li>
896
897<li>
Rob Pike5863b7d2013-09-09 13:29:08 +1000898The <a href="/pkg/runtime/"><code>runtime</code></a> package relaxes
899the constraints on finalizer functions in
900<a href="/pkg/runtime/#SetFinalizer"><code>SetFinalizer</code></a>: the
901actual argument can now be any type that is assignable to the formal type of
902the function, as is the case for any normal function call in Go.
903</li>
904
905<li>
906The <a href="/pkg/sort/"><code>sort</code></a> package has a new
907<a href="/pkg/sort/#Stable"><code>Stable</code></a> function that implements
908stable sorting. It is less efficient than the normal sort algorithm, however.
909</li>
910
911<li>
912The <a href="/pkg/strings/"><code>strings</code></a> package adds
913an <a href="/pkg/strings/#IndexByte"><code>IndexByte</code></a>
914function for consistency with the <a href="/pkg/bytes/"><code>bytes</code></a> package.
915</li>
916
917<li>
Rob Pikea97a7c52013-09-12 09:08:59 +1000918The <a href="/pkg/sync/atomic/"><code>sync/atomic</code></a> package
919adds a new set of swap functions that atomically exchange the argument with the
920value stored in the pointer, returning the old value.
921The functions are
922<a href="/pkg/sync/atomic/#SwapInt32"><code>SwapInt32</code></a>,
923<a href="/pkg/sync/atomic/#SwapInt64"><code>SwapInt64</code></a>,
924<a href="/pkg/sync/atomic/#SwapUint32"><code>SwapUint32</code></a>,
925<a href="/pkg/sync/atomic/#SwapUint64"><code>SwapUint64</code></a>,
926<a href="/pkg/sync/atomic/#SwapUintptr"><code>SwapUintptr</code></a>,
927and
928<a href="/pkg/sync/atomic/#SwapPointer"><code>SwapPointer</code></a>,
929which swaps an <code>unsafe.Pointer</code>.
930</li>
931
932<li>
Mikio Haraeb002c52013-10-04 13:13:56 +0900933The <a href="/pkg/syscall/"><code>syscall</code></a> package now implements
934<a href="/pkg/syscall/#Sendfile"><code>Sendfile</code></a> for Darwin.
Rob Pike5863b7d2013-09-09 13:29:08 +1000935</li>
936
937<li>
938The <a href="/pkg/testing/"><code>testing</code></a> package
Rob Piked237f3c2013-09-16 10:28:53 +1000939now exports the <a href="/pkg/testing/#TB"><code>TB</code></a> interface.
Rob Pike5863b7d2013-09-09 13:29:08 +1000940It records the methods in common with the
941<a href="/pkg/testing/#T"><code>T</code></a>
942and
943<a href="/pkg/testing/#B"><code>B</code></a> types,
944to make it easier to share code between tests and benchmarks.
945Also, the
946<a href="/pkg/testing/#AllocsPerRun"><code>AllocsPerRun</code></a>
947function now quantizes the return value to an integer (although it
948still has type <code>float64</code>), to round off any error caused by
949initialization and make the result more repeatable.
950</li>
951
952<li>
953The <a href="/pkg/text/template/"><code>text/template</code></a> package
954now automatically dereferences pointer values when evaluating the arguments
955to "escape" functions such as "html", to bring the behavior of such functions
956in agreement with that of other printing functions such as "printf".
957</li>
958
959<li>
960In the <a href="/pkg/time/"><code>time</code></a> package, the
961<a href="/pkg/time/#Parse"><code>Parse</code></a> function
962and
Andrew Gerrandcf694aa2013-10-09 07:05:43 +1100963<a href="/pkg/time/#Time.Format"><code>Format</code></a>
Rob Pike5863b7d2013-09-09 13:29:08 +1000964method
965now handle time zone offsets with seconds, such as in the historical
966date "1871-01-01T05:33:02+00:34:08".
967Also, pattern matching in the formats for those routines is stricter: a non-lowercase letter
968must now follow the standard words such as "Jan" and "Mon".
969</li>
970
971<li>
972The <a href="/pkg/unicode/"><code>unicode</code></a> package
973adds <a href="/pkg/unicode/#In"><code>In</code></a>,
974a nicer-to-use but equivalent version of the original
975<a href="/pkg/unicode/#IsOneOf"><code>IsOneOf</code></a>,
976to see whether a character is a member of a Unicode category.
977</li>
978
979</ul>