blob: 768c1ca6fa67dbd144d44331cb272e37d9f40af5 [file] [log] [blame]
Andrew Gerrand7cb21a72012-01-19 11:24:54 +11001<!--{
2 "Title": "Go 1 Release Notes"
3}-->
Rob Pike9d59c402011-12-08 11:35:28 -08004<!--
5 DO NOT EDIT: created by
6 tmpltohtml go1.tmpl
7-->
8
Rob Pikebab4dec2011-12-07 14:33:37 -08009
10<h2 id="introduction">Introduction to Go 1</h2>
11
12<p>
13For a full explanation of the motivation and design of Go 1, see XXX.
14Here follows a summary.
15</p>
16
17<p>
Rob Pike2fa987b2011-12-07 16:11:17 -080018Go 1 is intended to be a stable language and core library set that
19will form a reliable foundation for people and organizations that
20want to make a long-term commitment to developing in the Go programming
21language. Go will continue to develop, but in a way that guarantees
22code written to the Go 1 specification will continue to work. For
23instance, Go 1 will be a supported platform on Google App Engine
24for the next few years. Incompatible changes to the environment,
25should they arise, will be done in a distinct version.
Rob Pikebab4dec2011-12-07 14:33:37 -080026</p>
27
28<p>
Rob Pike2fa987b2011-12-07 16:11:17 -080029This document describes the changes in the language and libraries
30in Go 1, relative to the previous release, r60 (at the time of
31writing, tagged as r60.3). It also explains how to update code at
32r60 to compile and run under Go 1. Finally, it outlines the new
33<code>go</code> command for building Go programs and the new binary
34release process being introduced. Most of these topics have more
35thorough presentations elsewhere; such documents are linked below.
Rob Pike136c04f2011-12-08 16:39:05 -080036</p>
Rob Pikebab4dec2011-12-07 14:33:37 -080037
38<h2 id="language">Changes to the language</h2>
39
40<h3 id="append">Append</h3>
41
Rob Pike136c04f2011-12-08 16:39:05 -080042<p>
43The <code>append</code> built-in function is variadic, so one can
44append to a byte slice using the <code>...</code> syntax in the
45call.
46</p>
47
48<pre><!--{{code "progs/go1.go" `/greeting := ..byte/` `/append.*hello/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +110049--> greeting := []byte{}
Andrew Gerrand5353e1e2012-01-06 09:20:31 +110050 greeting = append(greeting, []byte(&#34;hello &#34;)...)</pre>
Rob Pike136c04f2011-12-08 16:39:05 -080051
52<p>
53By analogy with the similar property of <code>copy</code>, Go 1
54permits a string to be appended (byte-wise) directly to a byte
55slice; the conversion is no longer necessary:
56</p>
57
58<pre><!--{{code "progs/go1.go" `/append.*world/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +110059--> greeting = append(greeting, &#34;world&#34;...)</pre>
Rob Pike136c04f2011-12-08 16:39:05 -080060
61<p>
62<em>Updating</em>:
63This is a new feature, so existing code needs no changes.
64</p>
65
Rob Pikebab4dec2011-12-07 14:33:37 -080066<h3 id="close">Close</h3>
67
Rob Pike136c04f2011-12-08 16:39:05 -080068<p>
69The <code>close</code> built-in function lets a sender tell a receiver
70that no more data will be transmitted on the channel. In Go 1 the
71type system enforces the directionality when possible: it is illegal
72to call <code>close</code> on a receive-only channel:
73</p>
74
75<pre>
76 var c chan int
77 var csend chan<- int = c
78 var crecv <-chan int = c
79 close(c) // legal
80 close(csend) // legal
81 close(crecv) // illegal
82</pre>
83
84<p>
85<em>Updating</em>:
86Existing code that attempts to close a receive-only channel was
87erroneous even before Go 1 and should be fixed. The compiler will
88now reject such code.
89</p>
90
Rob Pike9d59c402011-12-08 11:35:28 -080091<h3 id="literals">Composite literals</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -080092
Rob Pike2e338fa2011-12-09 08:31:57 -080093<p>
94In Go 1, a composite literal of array, slice, or map type can elide the
95type specification for the elements' initializers if they are of pointer type.
96All four of the initializations in this example are legal; the last one was illegal before Go 1.
97</p>
98
99<pre><!--{{code "progs/go1.go" `/type Date struct/` `/STOP/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100100--> type Date struct {
Rob Pike2e338fa2011-12-09 08:31:57 -0800101 month string
102 day int
103 }
104 // Struct values, fully qualified; always legal.
105 holiday1 := []Date{
106 Date{&#34;Feb&#34;, 14},
107 Date{&#34;Nov&#34;, 11},
108 Date{&#34;Dec&#34;, 25},
109 }
110 // Struct values, type name elided; always legal.
111 holiday2 := []Date{
112 {&#34;Feb&#34;, 14},
113 {&#34;Nov&#34;, 11},
114 {&#34;Dec&#34;, 25},
115 }
116 // Pointers, fully qualified, always legal.
117 holiday3 := []*Date{
118 &amp;Date{&#34;Feb&#34;, 14},
119 &amp;Date{&#34;Nov&#34;, 11},
120 &amp;Date{&#34;Dec&#34;, 25},
121 }
122 // Pointers, type name elided; legal in Go 1.
123 holiday4 := []*Date{
124 {&#34;Feb&#34;, 14},
125 {&#34;Nov&#34;, 11},
126 {&#34;Dec&#34;, 25},
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100127 }</pre>
Rob Pike2e338fa2011-12-09 08:31:57 -0800128
129<p>
130<em>Updating</em>:
131This change has no effect on existing code, but the command
132<code>gofmt</code> <code>-s</code> applied to existing source
133will, among other things, elide explicit element types wherever permitted.
134</p>
135
136
Rob Pike9d59c402011-12-08 11:35:28 -0800137<h3 id="init">Goroutines during init</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800138
Rob Pike136c04f2011-12-08 16:39:05 -0800139<p>
140Go 1 allows goroutines to be created and run during initialization.
141(They used to be created but were not run until after initialization
142completed.) Code that uses goroutines can now be called from
143<code>init</code> routines and global initialization expressions
144without introducing a deadlock.
145</p>
146
147<pre><!--{{code "progs/go1.go" `/PackageGlobal/` `/^}/`}}
148-->var PackageGlobal int
149
150func init() {
151 c := make(chan int)
152 go initializationFunction(c)
153 PackageGlobal = &lt;-c
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100154}</pre>
Rob Pike136c04f2011-12-08 16:39:05 -0800155
156<p>
157<em>Updating</em>:
158This is a new feature, so existing code needs no changes,
159although it's possible that code that depends on goroutines not starting before <code>main</code> will break.
160There was no such code in the standard repository.
161</p>
162
Rob Pikebab4dec2011-12-07 14:33:37 -0800163<h3 id="rune">The rune type</h3>
164
Rob Pike2e338fa2011-12-09 08:31:57 -0800165<p>
166Go 1 introduces a new basic type, <code>rune</code>, to be used to represent
167individual Unicode code points.
168It is an alias for <code>int32</code>, analogous to <code>byte</code>
169as an alias for <code>uint8</code>.
170</p>
171
172<p>
173Character literals such as <code>'a'</code>, <code>'語'</code>, and <code>'\u0345'</code>
174now have default type <code>rune</code>,
175analogous to <code>1.0</code> having default type <code>float64</code>.
176A variable initialized to a character constant will therefore
177have type <code>rune</code> unless otherwise specified.
178</p>
179
180<p>
181Libraries have been updated to use <code>rune</code> rather than <code>int</code>
182when appropriate. For instance, the functions <code>unicode.ToLower</code> and
183relatives now take and return a <code>rune</code>.
184</p>
185
186<pre><!--{{code "progs/go1.go" `/STARTRUNE/` `/ENDRUNE/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100187--> delta := &#39;δ&#39; // delta has type rune.
Rob Pike2e338fa2011-12-09 08:31:57 -0800188 var DELTA rune
189 DELTA = unicode.ToUpper(delta)
190 epsilon := unicode.ToLower(DELTA + 1)
191 if epsilon != &#39;δ&#39;+1 {
192 log.Fatal(&#34;inconsistent casing for Greek&#34;)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100193 }</pre>
Rob Pike2e338fa2011-12-09 08:31:57 -0800194
195<p>
196<em>Updating</em>:
197Most source code will be unaffected by this because the type inference from
198<code>:=</code> initializers introduces the new type silently, and it propagates
199from there.
200Some code may get type errors that a trivial conversion will resolve.
201</p>
202
203<h3 id="error">The error type</h3>
204
205<p>
206Go 1 introduces a new built-in type, <code>error</code>, which has the following definition:
207</p>
208
209<pre>
210 type error interface {
211 Error() string
212 }
213</pre>
214
215<p>
216Since the consequences of this type are all in the package library,
Rob Pikeebdcbf12011-12-12 12:26:56 -0800217it is discussed <a href="#errors">below</a>.
Rob Pike2e338fa2011-12-09 08:31:57 -0800218</p>
219
Rob Pike9d59c402011-12-08 11:35:28 -0800220<h3 id="delete">Deleting from maps</h3>
Rob Pike2fa987b2011-12-07 16:11:17 -0800221
222<p>
223The original syntax for deleting an element in a map was:
224</p>
225
226<pre>
Rob Pike2e338fa2011-12-09 08:31:57 -0800227 m[k] = ignored, false
Rob Pike2fa987b2011-12-07 16:11:17 -0800228</pre>
229
230<p>
Rob Pike9d59c402011-12-08 11:35:28 -0800231In Go 1, that syntax has gone; instead there is a new built-in
Rob Pike2fa987b2011-12-07 16:11:17 -0800232function, <code>delete</code>. The call
233</p>
234
235<pre><!--{{code "progs/go1.go" `/delete\(m, k\)/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100236--> delete(m, k)</pre>
Rob Pike2fa987b2011-12-07 16:11:17 -0800237
238<p>
239will delete the map entry retrieved by the expression <code>m[k]</code>.
240There is no return value. Deleting a non-existent entry is a no-op.
241</p>
242
243<p>
244<em>Updating</em>:
245Gofix will convert expressions of the form <code>m[k] = ignored,
246false</code> into <code>delete(m, k)</code> when it is clear that
247the ignored value can be safely discarded from the program and
248<code>false</code> refers to the predefined boolean constant. Gofix
249will flag other uses of the syntax for inspection by the programmer.
250</p>
251
Rob Pike136c04f2011-12-08 16:39:05 -0800252<h3 id="iteration">Iterating in maps</h3>
253
254<p>
255In Go 1, the order in which elements are visited when iterating
256over a map using a <code>for</code> <code>range</code> statement
257is defined to be unpredictable, even if the same loop is run multiple
258times with the same map.
259Code should not assume that the elements are visited in any particular order.
260</p>
261
262<pre><!--{{code "progs/go1.go" `/Sunday/` `/^ }/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100263--> m := map[string]int{&#34;Sunday&#34;: 0, &#34;Monday&#34;: 1}
Rob Pike136c04f2011-12-08 16:39:05 -0800264 for name, value := range m {
265 // This loop should not assume Sunday will be visited first.
266 f(name, value)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100267 }</pre>
Rob Pike136c04f2011-12-08 16:39:05 -0800268
269<p>
270<em>Updating</em>:
271This is one change where tools cannot help. Most existing code
272will be unaffected, but some programs may break or misbehave; we
273recommend manual checking of all range statements over maps to
274verify they do not depend on iteration order. There were a few such
275examples in the standard repository; they have been fixed.
276Note that it was already incorrect to depend on the iteration order, which
277was unspecified. This change codifies the unpredictability.
278</p>
Rob Pikebab4dec2011-12-07 14:33:37 -0800279
280<h3 id="multiple_assignment">Multiple assignment</h3>
281
Rob Pike136c04f2011-12-08 16:39:05 -0800282<p>
283Go 1 fully specifies the evaluation order in multiple assignment
284statements. In particular, if the left-hand side of the assignment
285statement contains expressions that require evaluation, such as
286function calls or array indexing operations, these will all be done
287using the usual left-to-right rule before any variables are assigned
288their value. Once everything is evaluated, the actual assignments
289proceed in left-to-right order.
290</p>
291
292<p>
293These examples illustrate the behavior.
294</p>
295
296<pre><!--{{code "progs/go1.go" `/sa :=/` `/then sc.0. = 2/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100297--> sa := []int{1, 2, 3}
Rob Pike136c04f2011-12-08 16:39:05 -0800298 i := 0
299 i, sa[i] = 1, 2 // sets i = 1, sa[0] = 2
300
301 sb := []int{1, 2, 3}
302 j := 0
303 sb[j], j = 2, 1 // sets sb[0] = 2, j = 1
304
305 sc := []int{1, 2, 3}
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100306 sc[0], sc[0] = 1, 2 // sets sc[0] = 1, then sc[0] = 2 (so sc[0] = 2 at end)</pre>
Rob Pike136c04f2011-12-08 16:39:05 -0800307
Rob Pike2e338fa2011-12-09 08:31:57 -0800308<p>
Rob Pike136c04f2011-12-08 16:39:05 -0800309<em>Updating</em>:
310This is one change where tools cannot help, but breakage is unlikely.
311No code in the standard repository was broken by this change, and code
312that depended on the previous unspecified behavior was already incorrect.
313</p>
314
Rob Pikebab4dec2011-12-07 14:33:37 -0800315<h3 id="shadowing">Returns and shadowed variables</h3>
316
Rob Pike136c04f2011-12-08 16:39:05 -0800317<p>
318A shadowed variable is one that has the same name as another variable in an inner scope.
319In functions with named return values,
320the Go 1 compilers disallow return statements without arguments if any of the named return values is shadowed at the point of the return statement.
321(It isn't part of the specification, because this is one area we are still exploring;
322the situation is analogous to the compilers rejecting functions that do not end with an explicit return statement.)
323</p>
324
325<p>
326This function implicitly returns a shadowed return value and will be rejected by the compiler:
327</p>
328
329<pre>
330 func Bug() (i, j, k int) {
331 for i = 0; i < 5; i++ {
332 for j := 0; j < 5; j++ { // Redeclares j.
333 k += i*j
334 if k > 100 {
335 return // Rejected: j is shadowed here.
336 }
337 }
338 }
339 return // OK: j is not shadowed here.
340 }
341</pre>
342
343<p>
344<em>Updating</em>:
345Code that shadows return values in this way will be rejected by the compiler and will need to be fixed by hand.
346The few cases that arose in the standard repository were mostly bugs.
347</p>
348
349<h3 id="unexported">Copying structs with unexported fields</h3>
350
Rob Pike2e338fa2011-12-09 08:31:57 -0800351<p>
352Go 1 relaxes the rules about accessing structs with unexported (lower-case) fields,
353permitting a client package to assign (and therefore copy) such a struct.
354Of course, the client package still cannot access such fields individually.
355</p>
356
357<p>
358As an example, if package <code>p</code> includes the definitions,
359</p>
360
361<pre>
362 type Struct struct {
363 Public int
364 secret int
365 }
366 func NewStruct(a int) Struct { // Note: not a pointer.
367 return Struct{a, f(a)}
368 }
369 func (s Struct) String() string {
370 return fmt.Sprintf("{%d (secret %d)}", s.Public, s.secret)
371 }
372</pre>
373
374<p>
375a package that imports <code>p</code> can assign and copy values of type
376<code>p.Struct</code> at will.
377Behind the scenes the unexported fields will be assigned and copied just
378as if they were exported,
379but the client code will never be aware of them. The code
380</p>
381
382<pre>
383 import "p"
384
385 myStruct := p.NewStruct(23)
386 copyOfMyStruct := myStruct
387 fmt.Println(myStruct, copyOfMyStruct)
388</pre>
389
390<p>
391will show that the secret field of the struct has been copied to the new value.
392</p>
393
394<p>
395<em>Updating</em>:
396This is a new feature, so existing code needs no changes.
397</p>
398
Rob Pike9d59c402011-12-08 11:35:28 -0800399<h3 id="equality">Equality of structs and arrays</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800400
Rob Pike136c04f2011-12-08 16:39:05 -0800401<p>
402Go 1 defines equality and inequality (<code>==</code> and
403<code>!=</code>) for struct and array values, respectively, provided
404the elements of the data structures can themselves be compared.
405That is, if equality is defined for all the fields of a struct (or
406an array element), then it is defined for the struct (or array).
407</p>
408
409<p>
410As a result, structs and arrays can now be used as map keys:
411</p>
412
413<pre><!--{{code "progs/go1.go" `/type Day struct/` `/Printf/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100414--> type Day struct {
Rob Pike5fa18e12011-12-12 21:08:03 -0800415 long string
416 short string
417 }
418 Christmas := Day{&#34;Christmas&#34;, &#34;XMas&#34;}
419 Thanksgiving := Day{&#34;Thanksgiving&#34;, &#34;Turkey&#34;}
420 holiday := map[Day]bool{
421 Christmas: true,
422 Thanksgiving: true,
423 }
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100424 fmt.Printf(&#34;Christmas is a holiday: %t\n&#34;, holiday[Christmas])</pre>
Rob Pike136c04f2011-12-08 16:39:05 -0800425
426<p>
427Note that equality is still undefined for slices, for which the
428calculation is in general infeasible. Also note that the ordered
429comparison operators (<code>&lt;</code> <code>&lt;=</code>
430<code>&gt;</code> <code>&gt;=</code>) are still undefined for
431structs and arrays.
432
433<p>
434<em>Updating</em>:
435This is a new feature, so existing code needs no changes.
436</p>
437
438<h3 id="funcs">Function and map equality</h3>
439
440<p>
441Go 1 disallows checking for equality of functions and maps,
442respectively, except to compare them directly to <code>nil</code>.
443</p>
444
445<p>
446<em>Updating</em>:
447Existing code that depends on function or map equality will be
448rejected by the compiler and will need to be fixed by hand.
449Few programs will be affected, but the fix may require some
450redesign.
451</p>
452
Rob Pike0a1376a2012-01-20 14:28:48 -0800453<h2 id="packages">The package hierarchy</h2>
454
455<p>
456This section describes how the packages have been rearranged in Go 1.
457Some have moved, some have been renamed, some have been deleted.
458New packages are described in later sections.
459</p>
Rob Pikebab4dec2011-12-07 14:33:37 -0800460
Rob Pike9d59c402011-12-08 11:35:28 -0800461<h3 id="hierarchy">The package hierarchy</h3>
462
463<p>
464Go 1 has a rearranged package hierarchy that groups related items
465into subdirectories. For instance, <code>utf8</code> and
466<code>utf16</code> now occupy subdirectories of <code>unicode</code>.
467Also, <a href="#subrepo">some packages</a> have moved into
468subrepositories of
469<a href="http://code.google.com/p/go"><code>code.google.com/p/go</code></a>
470while <a href="#deleted">others</a> have been deleted outright.
471</p>
472
473<table class="codetable" frame="border" summary="Moved packages">
474<colgroup align="left" width="60%"></colgroup>
475<colgroup align="left" width="40%"></colgroup>
476<tr>
477<th align="left">Old path</th>
478<th align="left">New path</th>
479</tr>
Rob Pike71ccf732011-12-09 14:12:51 -0800480<tr>
481<td colspan="2"><hr></td>
482</tr>
Rob Pike136c04f2011-12-08 16:39:05 -0800483<tr><td>asn1</td> <td>encoding/asn1</td></tr>
Rob Pike9d59c402011-12-08 11:35:28 -0800484<tr><td>csv</td> <td>encoding/csv</td></tr>
485<tr><td>gob</td> <td>encoding/gob</td></tr>
486<tr><td>json</td> <td>encoding/json</td></tr>
487<tr><td>xml</td> <td>encoding/xml</td></tr>
488<tr>
489<td colspan="2"><hr></td>
490</tr>
491<tr><td>exp/template/html</td> <td>html/template</td></tr>
492<tr>
493<td colspan="2"><hr></td>
494</tr>
495<tr><td>big</td> <td>math/big</td></tr>
496<tr><td>cmath</td> <td>math/cmplx</td></tr>
497<tr><td>rand</td> <td>math/rand</td></tr>
498<tr>
499<td colspan="2"><hr></td>
500</tr>
501<tr><td>http</td> <td>net/http</td></tr>
502<tr><td>http/cgi</td> <td>net/http/cgi</td></tr>
503<tr><td>http/fcgi</td> <td>net/http/fcgi</td></tr>
504<tr><td>http/httptest</td> <td>net/http/httptest</td></tr>
505<tr><td>http/pprof</td> <td>net/http/pprof</td></tr>
506<tr><td>mail</td> <td>net/mail</td></tr>
507<tr><td>rpc</td> <td>net/rpc</td></tr>
508<tr><td>rpc/jsonrpc</td> <td>net/rpc/jsonrpc</td></tr>
509<tr><td>smtp</td> <td>net/smtp</td></tr>
510<tr><td>url</td> <td>net/url</td></tr>
511<tr>
512<td colspan="2"><hr></td>
513</tr>
514<tr><td>exec</td> <td>os/exec</td></tr>
515<tr>
516<td colspan="2"><hr></td>
517</tr>
518<tr><td>scanner</td> <td>text/scanner</td></tr>
519<tr><td>tabwriter</td> <td>text/tabwriter</td></tr>
520<tr><td>template</td> <td>text/template</td></tr>
521<tr><td>template/parse</td> <td>text/template/parse</td></tr>
522<tr>
523<td colspan="2"><hr></td>
524</tr>
525<tr><td>utf8</td> <td>unicode/utf8</td></tr>
526<tr><td>utf16</td> <td>unicode/utf16</td></tr>
527</table>
528
529<p>
530Note that the package names for the old <code>cmath</code> and
531<code>exp/template/html</code> packages have changed to <code>cmplx</code>
532and <code>template</code>.
533</p>
534
535<p>
536<em>Updating</em>:
537Gofix will update all imports and package renames for packages that
538remain inside the standard repository. Programs that import packages
539that are no longer in the standard repository will need to be edited
540by hand.
Rob Pike71ccf732011-12-09 14:12:51 -0800541<br>
542<font color="red">TODO: gofix should warn about deletions.</font>
Rob Pike9d59c402011-12-08 11:35:28 -0800543</p>
Rob Pikebab4dec2011-12-07 14:33:37 -0800544
Rob Pike0a1376a2012-01-20 14:28:48 -0800545<h3 id="exp">The package tree exp</h3>
546
547<p>
548Because they are not standardized, the packages under the <code>exp</code> directory will not be available in the
549standard Go 1 release distributions, although they will be available in source code form
550in <a href="http://code.google.com/p/go/">the repository</a> for
551developers who wish to use them.
552</p>
553
554<p>
555Several packages have moved under <code>exp</code> at the time of Go 1's release:
556</p>
557
558<ul>
559<li><code>ebnf</code></li>
560<li><code>go/types</code></li>
561<li><code>http/spdy</code></li>
562</ul>
563
564<p>
Rob Pike531ded92012-01-20 15:38:03 -0800565All these packages are available under the same names, with the prefix <code>exp/</code>: <code>exp/ebnf</code> etc.
566</p>
567
568<p>
Rob Pike0a1376a2012-01-20 14:28:48 -0800569Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>.
570</p>
571
572<p>
Rob Pike531ded92012-01-20 15:38:03 -0800573Finally, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while
Rob Pike0a1376a2012-01-20 14:28:48 -0800574<code>ebnflint</code> is now in <code>exp/ebnflint</code>
575</p>
576
577<p>
578<em>Updating</em>:
579Code that uses packages in <code>exp</code> will need to be updated by hand,
580or else compiled from an installation that has <code>exp</code> available.
581Gofix or the compiler will complain about such uses.
582<br>
583<font color="red">TODO: gofix should warn about such uses.</font>
584</p>
585
586<h3 id="old">The package tree old</h3>
587
588<p>
589Because they are deprecated, the packages under the <code>old</code> directory will not be available in the
590standard Go 1 release distributions, although they will be available in source code form for
591developers who wish to use them.
592</p>
593
594<p>
595The packages in their new locations are:
596</p>
597
598<ul>
599<li><code>old/netchan</code></li>
600<li><code>old/regexp</code></li>
601<li><code>old/template</code></li>
602</ul>
603
604<p>
605<em>Updating</em>:
606Code that uses packages now in <code>old</code> will need to be updated by hand,
607or else compiled from an installation that has <code>old</code> available.
608Gofix will warn about such uses.
609<br>
610<font color="red">TODO: gofix should warn about such uses.</font>
611</p>
612
613<h3 id="deleted">Deleted packages</h3>
614
615<p>
616Go 1 deletes several packages outright:
617</p>
618
619<ul>
620<li><code>container/vector</code></li>
621<li><code>exp/datafmt</code></li>
622<li><code>go/typechecker</code></li>
623<li><code>try</code></li>
624</ul>
625
626<p>
627and also the command <code>gotry</code>.
628</p>
629
630<p>
631<em>Updating</em>:
632Code that uses <code>container/vector</code> should be updated to use
633slices directly. See
634<a href="http://code.google.com/p/go-wiki/wiki/SliceTricks">the Go
635Language Community Wiki</a> for some suggestions.
636Code that uses the other packages (there should be almost zero) will need to be rethought.
637<br>
638<font color="red">TODO: gofix should warn such uses.</font>
639</p>
640
641<h3 id="subrepo">Packages moving to subrepositories</h3>
642
Rob Pike7eaad5e2012-01-25 13:29:25 -0800643<p>
644Go 1 has moved a number of packages into sub-repositories of
645<a href="http://code.google.com/p/go/">the main Go repository</a>.
646This table lists the old and new import paths:
647
648<table class="codetable" frame="border" summary="Sub-repositories">
649<colgroup align="left" width="40%"></colgroup>
650<colgroup align="left" width="60%"></colgroup>
651<tr>
652<th align="left">Old</th>
653<th align="left">New</th>
654</tr>
655<tr>
656<td colspan="2"><hr></td>
657</tr>
658<tr><td>crypto/bcrypt</td> <td>code.google.com/p/go.crypto/bcrypt</tr>
659<tr><td>crypto/blowfish</td> <td>code.google.com/p/go.crypto/blowfish</tr>
660<tr><td>crypto/cast5</td> <td>code.google.com/p/go.crypto/cast5</tr>
661<tr><td>crypto/md4</td> <td>code.google.com/p/go.crypto/md4</tr>
662<tr><td>crypto/ocsp</td> <td>code.google.com/p/go.crypto/ocsp</tr>
663<tr><td>crypto/openpgp</td> <td>code.google.com/p/go.crypto/openpgp</tr>
664<tr><td>crypto/openpgp/armor</td> <td>code.google.com/p/go.crypto/openpgp/armor</tr>
665<tr><td>crypto/openpgp/elgamal</td> <td>code.google.com/p/go.crypto/openpgp/elgamal</tr>
666<tr><td>crypto/openpgp/errors</td> <td>code.google.com/p/go.crypto/openpgp/errors</tr>
667<tr><td>crypto/openpgp/packet</td> <td>code.google.com/p/go.crypto/openpgp/packet</tr>
668<tr><td>crypto/openpgp/s2k</td> <td>code.google.com/p/go.crypto/openpgp/s2k</tr>
669<tr><td>crypto/ripemd160</td> <td>code.google.com/p/go.crypto/ripemd160</tr>
670<tr><td>crypto/twofish</td> <td>code.google.com/p/go.crypto/twofish</tr>
671<tr><td>crypto/xtea</td> <td>code.google.com/p/go.crypto/xtea</tr>
672<tr><td>exp/ssh</td> <td>code.google.com/p/go.crypto/ssh</tr>
673<tr>
674<td colspan="2"><hr></td>
675</tr>
676<tr><td>net/dict</td> <td>code.google.com/p/go.net/dict</tr>
677<tr><td>net/websocket</td> <td>code.google.com/p/go.net/websocket</tr>
678<tr><td>exp/spdy</td> <td>code.google.com/p/go.net/spdy</tr>
679<tr>
680<td colspan="2"><hr></td>
681</tr>
682<tr><td>encoding/git85</td> <td>code.google.com/p/go.codereview/git85</tr>
683<tr><td>patch</td> <td>code.google.com/p/go.codereview/patch</tr>
684</table>
685
686<p>
687<em>Updating</em>:
688Gofix will update imports of these packages to use the new import paths.
689Installations that depend on these packages will need to install them using
690a <code>go install</code> command.
691</p>
Rob Pike0a1376a2012-01-20 14:28:48 -0800692
693<h2 id="major">Major changes to the library</h2>
694
695<p>
696This section describes significant changes to the core libraries, the ones that
697affect the most programs.
698</p>
699
Rob Pike71ccf732011-12-09 14:12:51 -0800700<h3 id="errors">The error type and errors package</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800701
Rob Pikeebdcbf12011-12-12 12:26:56 -0800702<p>
703As mentioned above, Go 1 introduces a new built-in interface type called <code>error</code>.
704Its intent is to replace the old <code>os.Error</code> type with a more central concept.
705So the widely-used <code>String</code> method does not cause accidental satisfaction
706of the <code>error</code> interface, the <code>error</code> interface uses instead
707the name <code>Error</code> for that method:
708</p>
709
710<pre>
711 type error interface {
712 Error() string
713 }
714</pre>
715
716<p>
717The <code>fmt</code> library automatically invokes <code>Error</code>, as it already
718does for <code>String</code>, for easy printing of error values.
719</p>
720
721<pre><!--{{code "progs/go1.go" `/START ERROR EXAMPLE/` `/END ERROR EXAMPLE/`}}
722-->type SyntaxError struct {
723 File string
724 Line int
725 Message string
726}
727
728func (se *SyntaxError) Error() string {
729 return fmt.Sprintf(&#34;%s:%d: %s&#34;, se.File, se.Line, se.Message)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100730}</pre>
Rob Pikeebdcbf12011-12-12 12:26:56 -0800731
732<p>
733All standard packages have been updated to use the new interface; the old <code>os.Error</code> is gone.
734</p>
735
736<p>
737A new package, <a href="/pkg/errors/"><code>errors</code></a>, contains the function
738</p>
739
740<pre>
741func New(text string) error
742</pre>
743
744<p>
745to turn a string into an error. It replaces the old <code>os.NewError</code>.
746</p>
747
748<pre><!--{{code "progs/go1.go" `/ErrSyntax/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100749--> var ErrSyntax = errors.New(&#34;syntax error&#34;)</pre>
Rob Pikeebdcbf12011-12-12 12:26:56 -0800750
751<p>
752<em>Updating</em>:
753Gofix will update almost all code affected by the change.
754Code that defines error types with a <code>String</code> method will need to be updated
755by hand to rename the methods to <code>Error</code>.
756</p>
757
Rob Pike9d59c402011-12-08 11:35:28 -0800758<h3 id="errno">System call errors</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800759
Rob Pike71ccf732011-12-09 14:12:51 -0800760<p>
761In Go 1, the
Rob Pikeebdcbf12011-12-12 12:26:56 -0800762<a href="/pkg/syscall/"><code>syscall</code></a>
Rob Pike71ccf732011-12-09 14:12:51 -0800763package returns an <code>error</code> for system call errors,
764rather than plain integer <code>errno</code> values.
765On Unix, the implementation is done by a
Rob Pikeebdcbf12011-12-12 12:26:56 -0800766<a href="/pkg/syscall/#Errno"><code>syscall.Errno</code></a> type
Rob Pike71ccf732011-12-09 14:12:51 -0800767that satisfies <code>error</code> and replaces the old <code>os.Errno</code>.
768</p>
769
770<p>
771<em>Updating</em>:
772Gofix will update almost all code affected by the change.
773Regardless, most code should use the <code>os</code> package
774rather than <code>syscall</code> and so will be unaffected.
775</p>
776
Rob Pikebab4dec2011-12-07 14:33:37 -0800777<h3 id="time">Time</h3>
778
Rob Pike5fa18e12011-12-12 21:08:03 -0800779<p>
780One of the most sweeping changes in the Go 1 library is the
781complete redesign of the
782<a href="/pkg/time/"><code>time</code></a> package.
783Instead of an integer number of nanoseconds as an <code>int64</code>,
784and a separate <code>*time.Time</code> type to deal with human
785units such as hours and years,
786there are now two fundamental types:
787<a href="/pkg/time/#Time"><code>time.Time</code></a>
788(a value, so the <code>*</code> is gone), which represents a moment in time;
789and <a href="/pkg/time/#Duration"><code>time.Duration</code></a>,
790which represents an interval.
791Both have nanosecond resolution.
792A <code>Time</code> can represent any time into the ancient
793past and remote future, while a <code>Duration</code> can
794span plus or minus only about 290 years.
795There are methods on these types, plus a number of helpful
796predefined constant durations such as <code>time.Second</code>.
797</p>
798
799<p>
800Among the new methods are things like
801<a href="/pkg/time/#Time.Add"><code>Time.Add</code></a>,
802which adds a <code>Duration</code> to a <code>Time</code>, and
803<a href="/pkg/time/#Time.Sub"><code>Time.Sub</code></a>,
804which subtracts two <code>Times</code> to yield a <code>Duration</code>.
805</p>
806
807<p>
808The most important semantic change is that the Unix epoch (Jan 1, 1970) is now
809relevant only for those functions and methods that mention Unix:
810<a href="/pkg/time/#Unix"><code>time.Unix</code></a>
811and the <a href="/pkg/time/#Time.Unix"><code>Unix</code></a>
812and <a href="/pkg/time/#Time.UnixNano"><code>UnixNano</code></a> methods
813of the <code>Time</code> type.
814In particular,
815<a href="/pkg/time/#Now"><code>time.Now</code></a>
816returns a <code>time.Time</code> value rather than, in the old
817API, an integer nanosecond count since the Unix epoch.
818</p>
819
820<pre><!--{{code "progs/go1.go" `/sleepUntil/` `/^}/`}}
821-->// sleepUntil sleeps until the specified time. It returns immediately if it&#39;s too late.
822func sleepUntil(wakeup time.Time) {
823 now := time.Now() // A Time.
824 if !wakeup.After(now) {
825 return
826 }
827 delta := wakeup.Sub(now) // A Duration.
828 log.Printf(&#34;Sleeping for %.3fs&#34;, delta.Seconds())
829 time.Sleep(delta)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100830}</pre>
Rob Pike5fa18e12011-12-12 21:08:03 -0800831
832<p>
833The new types, methods, and constants have been propagated through
834all the standard packages that use time, such as <code>os</code> and
835its representation of file time stamps.
836</p>
837
838<p>
839<em>Updating</em>:
840Gofix will update many uses of the old <code>time</code> package to use the new
841types and methods, although it does not replace values such as <code>1e9</code>
842representing nanoseconds per second.
843Also, because of type changes in some of the values that arise,
844some of the expressions rewritten by gofix may require
845further hand editing; in such cases the rewrite will include
846the correct function or method for the old functionality, but
847may have the wrong type or require further analysis.
848</p>
849
Rob Pike0a1376a2012-01-20 14:28:48 -0800850<h2 id="minor">Minor changes to the library</h2>
851
852<p>
853This section describes smaller changes, such as those to less commonly
854used packages or that affect
855few programs beyond the need to run gofix.
856This category includes packages that are new in Go 1.
857</p>
858
859<h3 id="crypto_elliptic">The crypto/elliptic package</h3>
860
861<p>
862In Go 1, <a href="/pkg/crypto/elliptic/#Curve"><code>elliptic.Curve</code></a>
863has been made an interface to permit alternative implementations. The curve
864parameters have been moved to the
865<a href="/pkg/crypto/elliptic/#CurveParams"><code>elliptic.CurveParams</code></a>
866structure.
867</p>
868
869<p>
870<em>Updating</em>:
871Existing users of <code>*elliptic.Curve</code> will need to change to
872simply <code>elliptic.Curve</code>. Calls to <code>Marshal</code>,
873<code>Unmarshal</code> and <code>GenerateKey</code> are now functions
874in <code>crypto/elliptic</code> that take an <code>elliptic.Curve</code>
875as their first argument.
876</p>
877
878<h3 id="crypto_x509">The crypto/x509 package</h3>
879
880<p>
881In Go 1, the
882<a href="/pkg/crypto/x509/#CreateCertificate"><code>CreateCertificate</code></a>
883and
884<a href="/pkg/crypto/x509/#CreateCRL"><code>CreateCRL</code></a>
885functions in <code>crypto/x509</code> have been altered to take an
886<code>interface{}</code> where they previously took a <code>*rsa.PublicKey</code>
887or <code>*rsa.PrivateKey</code>. This will allow other public key algorithms
888to be implemented in the future.
889</p>
890
891<p>
892<em>Updating</em>:
893No changes will be needed.
894</p>
895
Rob Pike531ded92012-01-20 15:38:03 -0800896<h3 id="flag">The flag package</h3>
897
898<p>
899In Go 1, the interface <a href="/pkg/flag/#Value"><code>flag.Value</code></a> has changed slightly.
900The <code>Set</code> method now returns an <code>error</code> instead of
901a <code>bool</code> to indicate success or failure.
902</p>
903
904<p>
905There is also a new kind of flag, <code>Duration</code>, to support argument
906values specifying time intervals.
907Values for such flags must be given units, just as <code>time.Duration</code>
908formats them: <code>10s</code>, <code>1h30m</code>, etc.
909</p>
910
911<pre><!--{{code "progs/go1.go" `/timeout/`}}
912-->var timeout = flag.Duration(&#34;timeout&#34;, 30*time.Second, &#34;how long to wait for completion&#34;)</pre>
913
914<p>
915<em>Updating</em>:
916Programs that implement their own flags will need minor manual fixes to update their
917<code>Set</code> methods.
918The <code>Duration</code> flag is new and affects no existing code.
919</p>
920
921
Rob Pike0a1376a2012-01-20 14:28:48 -0800922<h3 id="go">The go/* packages</h3>
923
924<p>
925Several packages under <code>go</code> have slightly revised APIs.
926</p>
927
928<p>
929The modes <code>AllowIllegalChars</code> and <code>InsertSemis</code> have been removed
930from the <a href="/pkg/go/scanner/"><code>go/scanner</code></a> package. They were mostly
931useful for scanning text other then Go source files. Instead, the
932<a href="/pkg/text/scanner/"><code>text/scanner</code></a> package should be used
933for that purpose.
934</p>
935
936<p>
937The set of parse functions provided by the <a href="/pkg/go/parser/"><code>go/parser</code></a>
938package has been reduced to the primary parse function
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -0200939<a href="/pkg/go/parser/#ParseFile"><code>ParseFile</code></a>, and a couple of
940convenience functions <a href="/pkg/go/parser/#ParseDir"><code>ParseDir</code></a>
941and <a href="/pkg/go/parser/#ParseExpr"><code>ParseExpr</code></a>.
Rob Pike0a1376a2012-01-20 14:28:48 -0800942</p>
943
944<p>
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -0200945The type names of the <a href="/pkg/go/doc/"><code>go/doc</code></a> package have been
Rob Pike0a1376a2012-01-20 14:28:48 -0800946streamlined by removing the <code>Doc</code> suffix: <code>PackageDoc</code>
947is now <code>Package</code>, <code>ValueDoc</code> is <code>Value</code>, etc.
948Also, all types now consistently have a <code>Name</code> field (or <code>Names</code>,
Robert Griesemerd571c5c2012-01-25 16:48:06 -0800949in the case of type <code>Value</code>) and <code>Type.Factories</code> has become
950<code>Type.Funcs</code>.
Rob Pike0a1376a2012-01-20 14:28:48 -0800951Instead of calling <code>doc.NewPackageDoc(pkg, importpath)</code>,
952documentation for a package is created with:
953</p>
954
955<pre>
956 doc.New(pkg, importpath, mode)
957</pre>
958
959<p>
960where the new <code>mode</code> parameter specifies the operation mode:
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -0200961if set to <a href="/pkg/go/doc/#AllDecls"><code>AllDecls</code></a>, all declarations
Rob Pike0a1376a2012-01-20 14:28:48 -0800962(not just exported ones) are considered.
963The function <code>NewFileDoc</code> was removed, and the function
964<code>CommentText</code> has become the method
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -0200965<a href="/pkg/go/ast/#Text"><code>Text</code></a> of
966<a href="/pkg/go/ast/#CommentGroup"><code>ast.CommentGroup</code></a>.
Rob Pike0a1376a2012-01-20 14:28:48 -0800967</p>
968
969<p>
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -0200970In package <a href="/pkg/go/token/"><code>go/token</code></a>, the
971<a href="/pkg/go/token/#FileSet"><code>token.FileSet</code></a> method <code>Files</code>
Rob Pike0a1376a2012-01-20 14:28:48 -0800972(which originally returned a channel of <code>*token.File</code>s) has been replaced
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -0200973with the iterator <a href="/pkg/go/token/#FileSet.Iterate"><code>Iterate</code></a> that
Rob Pike0a1376a2012-01-20 14:28:48 -0800974accepts a function argument instead.
975</p>
976
977<p>
978<em>Updating</em>:
979Code that uses packages in <code>go</code> will have to be updated by hand; the
980compiler will reject incorrect uses. Templates used in conjuction with any of the
981<code>go/doc</code> types may need manual fixes; the renamed fields will lead
982to run-time errors.
983</p>
984
Russ Cox1f1c9ba2012-01-18 10:36:43 -0500985<h3 id="hash">The hash package</h3>
986
987<p>
988In Go 1, the definition of <a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> includes
989a new method, <code>BlockSize</code>. This new method is used primarily in the
990cryptographic libraries.
991</p>
992
993<p>
Rob Pike03ea8b12012-01-24 16:36:40 -0800994The <code>Sum</code> method of the
995<a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> interface now takes a
996<code>[]byte</code> argument, to which the hash value will be appended.
997The previous behavior can be recreated by adding a <code>nil</code> argument to the call.
998</p>
999
1000<p>
Russ Cox1f1c9ba2012-01-18 10:36:43 -05001001<em>Updating</em>:
1002Existing implementations of <code>hash.Hash</code> will need to add a
1003<code>BlockSize</code> method. Hashes that process the input one byte at
1004a time can implement <code>BlockSize</code> to return 1.
Rob Pike03ea8b12012-01-24 16:36:40 -08001005Gofix will update calls to the <code>Sum</code> methods of the various
1006implementations of <code>hash.Hash</code>.
Rob Pikef76bd4f2011-12-12 19:25:25 -08001007</p>
1008
1009<p>
1010<em>Updating</em>:
1011Since the package's functionality is new, no updating is necessary.
1012</p>
1013
Rob Pikebab4dec2011-12-07 14:33:37 -08001014<h3 id="http">The http package</h3>
1015
Rob Pikef76bd4f2011-12-12 19:25:25 -08001016<p>
Jongmin Kim343098e2012-01-17 09:47:34 -08001017In Go 1 the <a href="/pkg/net/http/"><code>http</code></a> package is refactored,
Rob Pikef76bd4f2011-12-12 19:25:25 -08001018putting some of the utilities into a
Jongmin Kim343098e2012-01-17 09:47:34 -08001019<a href="/pkg/net/httputil/"><code>httputil</code></a> subdirectory.
Rob Pikef76bd4f2011-12-12 19:25:25 -08001020These pieces are only rarely needed by HTTP clients.
1021The affected items are:
1022</p>
1023
1024<ul>
1025<li>ClientConn</li>
1026<li>DumpRequest</li>
1027<li>DumpRequest</li>
1028<li>DumpRequestOut</li>
1029<li>DumpResponse</li>
1030<li>NewChunkedReader</li>
1031<li>NewChunkedWriter</li>
1032<li>NewClientConn</li>
1033<li>NewProxyClientConn</li>
1034<li>NewServerConn</li>
1035<li>NewSingleHostReverseProxy</li>
1036<li>ReverseProxy</li>
1037<li>ServerConn</li>
1038</ul>
1039
1040<p>
1041Also, the <code>Request.RawURL</code> field has been removed; it was a
1042historical artifact.
1043</p>
1044
1045<p>
1046<em>Updating</em>:
1047Gofix will update the few programs that are affected except for
1048uses of <code>RawURL</code>, which must be fixed by hand.
1049</p>
1050
Rob Pike2257e762012-01-23 16:11:49 -08001051<h3 id="image">The image package</h3>
1052
1053<p>
1054The <a href="/pkg/image/"><code>image</code></a> package has had a number of
1055minor changes, rearrangements and renamings.
1056</p>
1057
1058<p>
1059Most of the color handling code has been moved into its own package,
1060<a href="/pkg/image/color/"><code>image/color</code></a>.
1061For the elements that moved, a symmetry arises; for instance,
1062each pixel of an
1063<a href="/pkg/image/#RGBA"><code>image.RGBA</code></a>
1064is a
1065<a href="/pkg/image/color/#RGBA"><code>color.RGBA</code></a>.
1066</p>
1067
1068<p>
1069The old <code>image/ycbcr</code> package has been folded, with some
1070renamings, into the
1071<a href="/pkg/image/"><code>image</code></a>
1072and
1073<a href="/pkg/image/color/"><code>image/color</code></a>
1074packages.
1075</p>
1076
1077<p>
1078The old <code>image.ColorImage</code> type is still in the <code>image</code>
1079package but has been renamed
1080<a href="/pkg/image/#Uniform"><code>image.Uniform</code></a>,
1081while <code>image.Tiled</code>
1082has been renamed
1083<a href="/pkg/image/#Repeated"><code>image.Repeated</code></a>.
1084</p>
1085
1086<p>
1087This table lists the renamings.
1088</p>
1089
1090<table class="codetable" frame="border" summary="image renames">
1091<colgroup align="left" width="50%"></colgroup>
1092<colgroup align="left" width="50%"></colgroup>
1093<tr>
1094<th align="left">Old</th>
1095<th align="left">New</th>
1096</tr>
1097<tr>
1098<td colspan="2"><hr></td>
1099</tr>
1100<tr><td>image.Color</td> <td>color.Color</td></tr>
1101<tr><td>image.ColorModel</td> <td>color.Model</td></tr>
1102<tr><td>image.ColorModelFunc</td> <td>color.ModelFunc</td></tr>
1103<tr><td>image.PalettedColorModel</td> <td>color.Palette</td></tr>
1104<tr>
1105<td colspan="2"><hr></td>
1106</tr>
1107<tr><td>image.RGBAColor</td> <td>color.RGBA</td></tr>
1108<tr><td>image.RGBA64Color</td> <td>color.RGBA64</td></tr>
1109<tr><td>image.NRGBAColor</td> <td>color.NRGBA</td></tr>
1110<tr><td>image.NRGBA64Color</td> <td>color.NRGBA64</td></tr>
1111<tr><td>image.AlphaColor</td> <td>color.Alpha</td></tr>
1112<tr><td>image.Alpha16Color</td> <td>color.Alpha16</td></tr>
1113<tr><td>image.GrayColor</td> <td>color.Gray</td></tr>
1114<tr><td>image.Gray16Color</td> <td>color.Gray16</td></tr>
1115<tr>
1116<td colspan="2"><hr></td>
1117</tr>
1118<tr><td>image.RGBAColorModel</td> <td>color.RGBAModel</td></tr>
1119<tr><td>image.RGBA64ColorModel</td> <td>color.RGBA64Model</td></tr>
1120<tr><td>image.NRGBAColorModel</td> <td>color.NRGBAModel</td></tr>
1121<tr><td>image.NRGBA64ColorModel</td> <td>color.NRGBA64Model</td></tr>
1122<tr><td>image.AlphaColorModel</td> <td>color.AlphaModel</td></tr>
1123<tr><td>image.Alpha16ColorModel</td> <td>color.Alpha16Model</td></tr>
1124<tr><td>image.GrayColorModel</td> <td>color.GrayModel</td></tr>
1125<tr><td>image.Gray16ColorModel</td> <td>color.Gray16Model</td></tr>
1126<tr>
1127<td colspan="2"><hr></td>
1128</tr>
1129<tr><td>ycbcr.RGBToYCbCr</td> <td>color.RGBToYCbCr</td></tr>
1130<tr><td>ycbcr.YCbCrToRGB</td> <td>color.YCbCrToRGB</td></tr>
1131<tr><td>ycbcr.YCbCrColorModel</td> <td>color.YCbCrModel</td></tr>
1132<tr><td>ycbcr.YCbCrColor</td> <td>color.YCbCr</td></tr>
1133<tr><td>ycbcr.YCbCr</td> <td>image.YCbCr</td></tr>
1134<tr>
1135<td colspan="2"><hr></td>
1136</tr>
1137<tr><td>ycbcr.SubsampleRatio444</td> <td>image.YCbCrSubsampleRatio444</td></tr>
1138<tr><td>ycbcr.SubsampleRatio422</td> <td>image.YCbCrSubsampleRatio422</td></tr>
1139<tr><td>ycbcr.SubsampleRatio420</td> <td>image.YCbCrSubsampleRatio420</td></tr>
1140<tr>
1141<td colspan="2"><hr></td>
1142</tr>
1143<tr><td>image.ColorImage</td> <td>image.Uniform</td></tr>
1144<tr><td>image.Tiled</td> <td>image.Repeated</td></tr>
1145</table>
1146
1147<p>
1148The image package's <code>New</code> functions
1149(<a href="/pkg/image/#NewRGBA"><code>NewRGBA</code></a>,
1150<a href="/pkg/image/#NewRGBA64"><code>NewRGBA64</code></a>, etc.)
1151take an <a href="/pkg/image/#Rectangle"><code>image.Rectangle</code></a> as an argument
1152instead of four integers.
1153</p>
1154
1155<p>
1156Finally, there are new predefined <code>color.Color</code> variables
1157<a href="/pkg/image/color/#Black"><code>color.Black</code></a>,
1158<a href="/pkg/image/color/#White"><code>color.White</code></a>,
1159<a href="/pkg/image/color/#Opaque"><code>color.Opaque</code></a>
1160and
1161<a href="/pkg/image/color/#Transparent"><code>color.Transparent</code></a>.
1162</p>
1163
1164<p>
1165<em>Updating</em>:
1166Gofix will update almost all code affected by the change.
1167</p>
1168
Rob Pikedd442a52012-01-24 17:02:06 -08001169<h3 id="mime">The mime package</h3>
1170
1171<p>
1172In Go 1, the <a href="/pkg/mime/#FormatMediaType"><code>FormatMediaType</code></a> function
1173of the <code>mime</code> package has been simplified to make it
1174consistent with
1175<a href="/pkg/mime/#ParseMediaType"><code>ParseMediaType</code></a>.
1176It now takes <code>"text/html"</code> rather than <code>"text"</code> and <code>"html"</code>.
1177</p>
1178
1179<p>
1180<em>Updating</em>:
1181What little code is affected will be caught by the compiler and must be updated by hand.
1182</p>
1183
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001184<h3 id="net">The net package</h3>
1185
Rob Pike03ea8b12012-01-24 16:36:40 -08001186<p>
1187In Go 1, the various <code>SetTimeout</code>,
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001188<code>SetReadTimeout</code>, and <code>SetWriteTimeout</code> methods
Rob Pike03ea8b12012-01-24 16:36:40 -08001189have been replaced with
1190<a href="/pkg/net/#IPConn.SetDeadline"><code>SetDeadline</code></a>,
1191<a href="/pkg/net/#IPConn.SetReadDeadline"><code>SetReadDeadline</code></a>, and
1192<a href="/pkg/net/#IPConn.SetWriteDeadline"><code>SetWriteDeadline</code></a>,
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001193respectively. Rather than taking a timeout value in nanoseconds that
1194apply to any activity on the connection, the new methods set an
1195absolute deadline (as a <code>time.Time</code> value) after which
Rob Pike03ea8b12012-01-24 16:36:40 -08001196reads and writes will time out and no longer block.
1197</p>
1198
1199<p>
Rob Pikedd442a52012-01-24 17:02:06 -08001200There is also a new <a href="/pkg/net/#DialTimeout"><code>net.DialTimeout</code></a> method to simplify
Rob Pike03ea8b12012-01-24 16:36:40 -08001201timing out dialing a network address.
1202</p>
1203
1204<p>
1205<em>Updating</em>:
1206Code that uses the old methods will fail to compile and must be updated by hand.
1207The semantic change makes it difficult for gofix to update automatically.
1208</p>
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001209
Rob Pike0a1376a2012-01-20 14:28:48 -08001210<h3 id="os_fileinfo">The os.FileInfo type</h3>
1211
1212<p>
1213Go 1 redefines the <a href="/pkg/os/#FileInfo"><code>os.FileInfo</code></a> type,
1214changing it from a struct to an interface:
1215</p>
1216
1217<pre>
1218 type FileInfo interface {
1219 Name() string // base name of the file
1220 Size() int64 // length in bytes
1221 Mode() FileMode // file mode bits
1222 ModTime() time.Time // modification time
1223 IsDir() bool // abbreviation for Mode().IsDir()
1224 }
1225</pre>
1226
1227<p>
1228The file mode information has been moved into a subtype called
1229<a href="/pkg/os/#FileMode"><code>os.FileMode</code></a>,
1230a simple integer type with <code>IsDir</code>, <code>Perm</code>, and <code>String</code>
1231methods.
1232</p>
1233
1234<p>
1235The system-specific details of file modes and properties such as (on Unix)
1236i-number have been removed from <code>FileInfo</code> altogether.
1237Instead, each operating system's <code>os</code> package provides an
1238implementation of the <code>FileInfo</code> interface, <code>*os.FileStat</code>,
1239which in turn contains a <code>Sys</code> field that stores the
1240system-specific representation of file metadata.
1241For instance, to discover the i-number of a file on a Unix system, unpack
1242the <code>FileInfo</code> like this:
1243</p>
1244
1245<pre>
1246 fi, err := os.Stat("hello.go")
1247 if err != nil {
1248 log.Fatal(err)
1249 }
1250 // Make sure it's an implementation known to package os.
1251 fileStat, ok := fi.(*os.FileStat)
1252 if !ok {
1253 log.Fatal("hello.go: not an os File")
1254 }
1255 // Now check that it's a Unix file.
1256 unixStat, ok := fileStat.Sys.(*syscall.Stat_t)
1257 if !ok {
1258 log.Fatal("hello.go: not a Unix file")
1259 }
1260 fmt.Printf("file i-number: %d\n", unixStat.Ino)
1261</pre>
1262
1263<p>
1264Assuming (which is unwise) that <code>"hello.go"</code> is a Unix file,
1265the i-number expression could be contracted to
1266</p>
1267
1268<pre>
1269 fi.(*os.FileStat).Sys.(*syscall.Stat_t).Ino
1270</pre>
1271
1272<p>
1273The vast majority of uses of <code>FileInfo</code> need only the methods
1274of the standard interface.
1275</p>
1276
1277<p>
1278<em>Updating</em>:
1279Gofix will update code that uses the old equivalent of the current <code>os.FileInfo</code>
1280and <code>os.FileMode</code> API.
1281Code that needs system-specific file details will need to be updated by hand.
1282</p>
1283
Rob Pikedd442a52012-01-24 17:02:06 -08001284<h3 id="path_filepath">The path/filepath package</h3>
1285
1286<p>
1287In Go 1, the <a href="/pkg/path/filepath/#Walk"><code>Walk</code></a> function of the
1288<code>path/filepath</code> package
1289has been changed to take a function value of type
1290<a href="/pkg/path/filepath/#WalkFunc"><code>WalkFunc</code></a>
1291instead of a <code>Visitor</code> interface value.
1292<code>WalkFunc</code> unifies the handling of both files and directories.
1293</p>
1294
1295<pre>
1296 type WalkFunc func(path string, info *os.FileInfo, err os.Error) os.Error
1297</pre>
1298
1299<p>
1300The <code>WalkFunc</code> function will be called even for files or directories that could not be opened;
1301in such cases the error argument will describe the failure.
1302If a directory's contents are to be skipped,
1303the function should return the value <code>SkipDir</code>.
1304</p>
1305
1306<p>
1307<font color="red">TODO: add an example?</font>
1308</p>
1309
1310<p>
1311<em>Updating</em>:
1312The change simplifies most code but has subtle consequences, so affected programs
1313will need to be updated by hand.
1314The compiler will catch code using the old interface.
1315</p>
1316
Rob Pike531ded92012-01-20 15:38:03 -08001317<h3 id="runtime">The runtime package</h3>
1318
1319<p>
1320The <code>runtime</code> package in Go 1 includes a new niladic function,
1321<a href="/pkg/runtime/#NumCPU"><code>runtime.NumCPU</code></a>, that returns the number of CPUs available
1322for parallel execution, as reported by the operating system kernel.
1323Its value can inform the setting of <code>GOMAXPROCS</code>.
1324</p>
1325
1326<p>
1327<em>Updating</em>:
1328No existing code is affected.
1329</p>
1330
Rob Pikebab4dec2011-12-07 14:33:37 -08001331<h3 id="strconv">The strconv package</h3>
1332
Rob Pike71ccf732011-12-09 14:12:51 -08001333<p>
1334In Go 1, the
Rob Pikeebdcbf12011-12-12 12:26:56 -08001335<a href="/pkg/strconv/"><code>strconv</code></a>
Rob Pike71ccf732011-12-09 14:12:51 -08001336package has been significantly reworked to make it more Go-like and less C-like,
1337although <code>Atoi</code> lives on (it's similar to
1338<code>int(ParseInt(x, 10, 0))</code>, as does
1339<code>Itoa(x)</code> (<code>FormatInt(int64(x), 10)</code>).
1340There are also new variants of some of the functions that append to byte slices rather than
1341return strings, to allow control over allocation.
1342</p>
1343
1344<p>
1345This table summarizes the renamings; see the
Rob Pikeebdcbf12011-12-12 12:26:56 -08001346<a href="/pkg/strconv/">package documentation</a>
Rob Pike71ccf732011-12-09 14:12:51 -08001347for full details.
1348</p>
1349
1350<table class="codetable" frame="border" summary="strconv renames">
1351<colgroup align="left" width="50%"></colgroup>
1352<colgroup align="left" width="50%"></colgroup>
1353<tr>
1354<th align="left">Old call</th>
1355<th align="left">New call</th>
1356</tr>
1357<tr>
1358<td colspan="2"><hr></td>
1359</tr>
1360<tr><td>Atob(x)</td> <td>ParseBool(x)</td></tr>
1361<tr>
1362<td colspan="2"><hr></td>
1363</tr>
1364<tr><td>Atof32(x)</td> <td>ParseFloat(x, 32)§</td></tr>
1365<tr><td>Atof64(x)</td> <td>ParseFloat(x, 64)</td></tr>
1366<tr><td>AtofN(x, n)</td> <td>ParseFloat(x, n)</td></tr>
1367<tr>
1368<td colspan="2"><hr></td>
1369</tr>
1370<tr><td>Atoi(x)</td> <td>Atoi(x)</td></tr>
1371<tr><td>Atoi(x)</td> <td>ParseInt(x, 10, 0)§</td></tr>
1372<tr><td>Atoi64(x)</td> <td>ParseInt(x, 10, 64)</td></tr>
1373<tr>
1374<td colspan="2"><hr></td>
1375</tr>
1376<tr><td>Atoui(x)</td> <td>ParseUint(x, 10, 0)§</td></tr>
1377<tr><td>Atoi64(x)</td> <td>ParseInt(x, 10, 64)</td></tr>
1378<tr>
1379<td colspan="2"><hr></td>
1380</tr>
1381<tr><td>Btoi64(x, b)</td> <td>ParseInt(x, b, 64)</td></tr>
1382<tr><td>Btoui64(x, b)</td> <td>ParseUint(x, b, 64)</td></tr>
1383<tr>
1384<td colspan="2"><hr></td>
1385</tr>
1386<tr><td>Btoa(x)</td> <td>FormatBool(x)</td></tr>
1387<tr>
1388<td colspan="2"><hr></td>
1389</tr>
1390<tr><td>Ftoa32(x, f, p)</td> <td>FormatFloat(x, float64(f), p, 32)</td></tr>
1391<tr><td>Ftoa64(x, f, p)</td> <td>FormatFloat(x, f, p, 64)</td></tr>
1392<tr><td>FtoaN(x, f, p, n)</td> <td>FormatFloat(x, f, p, n)</td></tr>
1393<tr>
1394<td colspan="2"><hr></td>
1395</tr>
1396<tr><td>Itoa(x)</td> <td>Itoa(x)</td></tr>
1397<tr><td>Itoa(x)</td> <td>FormatInt(int64(x), 10)</td></tr>
1398<tr><td>Itoa64(x)</td> <td>FormatInt(x, 10)</td></tr>
1399<tr>
1400<td colspan="2"><hr></td>
1401</tr>
1402<tr><td>Itob(x, b)</td> <td>FormatInt(int64(x), b)</td></tr>
1403<tr><td>Itob64(x, b)</td> <td>FormatInt(x, b)</td></tr>
1404<tr>
1405<td colspan="2"><hr></td>
1406</tr>
1407<tr><td>Uitoa(x)</td> <td>FormatUint(uint64(x), 10)</td></tr>
1408<tr><td>Uitoa64(x)</td> <td>FormatUint(x, 10)</td></tr>
1409<tr>
1410<td colspan="2"><hr></td>
1411</tr>
1412<tr><td>Uitob(x, b)</td> <td>FormatUint(uint64(x), b)</td></tr>
1413<tr><td>Uitob64(x, b)</td> <td>FormatUint(x, b)</td></tr>
1414</table>
1415
1416<p>
1417<em>Updating</em>:
1418Gofix will update almost all code affected by the change.
1419<br>
1420§ <code>Atoi</code> persists but <code>Atoui</code> and <code>Atof32</code> do not, so
1421they may require
1422a cast that must be added by hand; gofix will warn about it.
1423</p>
1424
Rob Pikebab4dec2011-12-07 14:33:37 -08001425
Rob Pike531ded92012-01-20 15:38:03 -08001426<h3 id="testing">The testing package</h3>
1427
1428<p>
1429The testing package has a type, <code>B</code>, passed as an argument to benchmark functions.
1430In Go 1, <code>B</code> has new methods, analogous to those of <code>T</code>, enabling
1431logging and failure reporting.
1432</p>
1433
1434<pre><!--{{code "progs/go1.go" `/func.*Benchmark/` `/^}/`}}
1435-->func BenchmarkSprintf(b *testing.B) {
1436 // Verify correctness before running benchmark.
1437 b.StopTimer()
1438 got := fmt.Sprintf(&#34;%x&#34;, 23)
1439 const expect = &#34;17&#34;
1440 if expect != got {
1441 b.Fatalf(&#34;expected %q; got %q&#34;, expect, got)
1442 }
1443 b.StartTimer()
1444 for i := 0; i &lt; b.N; i++ {
1445 fmt.Sprintf(&#34;%x&#34;, 23)
1446 }
1447}</pre>
1448
1449<p>
1450<em>Updating</em>:
1451Existing code is unaffected, although benchmarks that use <code>println</code>
Rob Pike2257e762012-01-23 16:11:49 -08001452or <code>panic</code> should be updated to use the new methods.
Rob Pike531ded92012-01-20 15:38:03 -08001453</p>
1454
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02001455<h3 id="url">The url package</h3>
1456
1457<p>
1458In Go 1 several fields from the <a href="/pkg/net/url#URL"><code>url.URL</code></a> type
1459were removed or replaced.
1460</p>
1461
1462<p>
1463The <a href="/pkg/net/url#URL.String"><code>String</code></a> method now
1464predictably rebuilds an encoded URL string using all of <code>URL</code>'s
1465fields as necessary. The resulting string will also no longer have
1466passwords escaped.
1467</p>
1468
1469<p>
1470The <code>Raw</code> field has been removed. In most cases the <code>String</code>
1471method may be used in its place.
1472</p>
1473
1474<p>
1475The old <code>RawUserinfo</code> field is replaced by the <code>User</code>
1476field, of type <a href="/pkg/net/url#Userinfo"><code>*net.Userinfo</code></a>.
1477Values of this type may be created using the new <a href="/pkg/net/url#User"><code>net.User</code></a>
1478and <a href="/pkg/net/url#UserPassword"><code>net.UserPassword</code></a>
1479functions. The <code>EscapeUserinfo</code> and <code>UnescapeUserinfo</code>
1480functions are also gone.
1481</p>
1482
1483<p>
1484The <code>RawAuthority</code> field has been removed. The same information is
1485available in the <code>Host</code> and <code>User</code> fields.
1486</p>
1487
1488<p>
1489The <code>RawPath</code> field and the <code>EncodedPath</code> method have
1490been removed. The path information in rooted URLs (with a slash following the
1491schema) is now available only in decoded form in the <code>Path</code> field.
1492Occasionally, the encoded data may be required to obtain information that
1493was lost in the decoding process. These cases must be handled by accessing
1494the data the URL was built from.
1495</p>
1496
1497<p>
1498URLs with non-rooted paths, such as <code>"mailto:dev@golang.org?subject=Hi"</code>,
1499are also handled differently. The <code>OpaquePath</code> boolean field has been
1500removed and a new <code>Opaque</code> string field introduced to hold the encoded
1501path for such URLs. In Go 1, the cited URL parses as:
1502</p>
1503
1504<pre>
1505 URL{
1506 Scheme: "mailto",
1507 Opaque: "dev@golang.org",
1508 RawQuery: "subject=Hi",
1509 }
1510</pre>
1511
1512<p>
1513A new <a href="/pkg/net/url#URL.RequestURI"><code>RequestURI</code></a> method was
1514added to <code>URL</code>.
1515</p>
1516
1517<p>
1518<em>Updating</em>:
1519Code that uses the old fields will fail to compile and must be updated by hand.
1520The semantic changes make it difficult for gofix to update automatically.
1521</p>
1522
Rob Pikebab4dec2011-12-07 14:33:37 -08001523<h2 id="go_command">The go command</h2>
1524
1525<h2 id="releases">Packaged releases</h2>
1526