blob: 3309a4073069690bc6fea453aa117fea38e98df8 [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>
Rob Pikeb36d25f2012-02-25 08:02:35 +110013Go version 1, Go 1 for short, defines a language and a set of core libraries
14that provide a stable foundation for creating reliable products, projects, and
15publications.
Rob Pikebab4dec2011-12-07 14:33:37 -080016</p>
17
18<p>
Rob Pikeb36d25f2012-02-25 08:02:35 +110019The driving motivation for Go 1 is stability for its users. People should be able to
20write Go programs and expect that they will continue to compile and run without
21change, on a time scale of years, including in production environments such as
22Google App Engine. Similarly, people should be able to write books about Go, be
23able to say which version of Go the book is describing, and have that version
24number still be meaningful much later.
Rob Pikebab4dec2011-12-07 14:33:37 -080025</p>
26
27<p>
Rob Pikeb36d25f2012-02-25 08:02:35 +110028Code that compiles in Go 1 should, with few exceptions, continue to compile and
29run throughout the lifetime of that version, even as we issue updates and bug
30fixes such as Go version 1.1, 1.2, and so on. Other than critical fixes, changes
31made to the language and library for subsequent releases of Go 1 may
32add functionality but will not break existing Go 1 programs.
33<a href="go1compat.html">The Go 1 compatibility document</a>
34explains the compatibility guidelines in more detail.
35</p>
36
37<p>
38Go 1 is a representation of Go as it used today, not a wholesale rethinking of
39the language. We avoided designing new features and instead focused on cleaning
40up problems and inconsistencies and improving portability. There are a number
41changes to the Go language and packages that we had considered for some time and
42prototyped but not released primarily because they are significant and
43backwards-incompatible. Go 1 was an opportunity to get them out, which is
44helpful for the long term, but also means that Go 1 introduces incompatibilities
45for old programs. Fortunately, the <code>go</code> <code>fix</code> tool can
46automate much of the work needed to bring programs up to the Go 1 standard.
47</p>
48
49<p>
50This document outlines the major changes in Go 1 that will affect programmers
51updating existing code; its reference point is the prior release, r60 (tagged as
52r60.3). It also explains how to update code from r60 to run under Go 1.
Rob Pike136c04f2011-12-08 16:39:05 -080053</p>
Rob Pikebab4dec2011-12-07 14:33:37 -080054
55<h2 id="language">Changes to the language</h2>
56
57<h3 id="append">Append</h3>
58
Rob Pike136c04f2011-12-08 16:39:05 -080059<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +110060The <code>append</code> predeclared variadic function makes it easy to grow a slice
61by adding elements to the end.
62A common use is to add bytes to the end of a byte slice when generating output.
63However, <code>append</code> did not provide a way to append a string to a <code>[]byte</code>,
64which is another common case.
Rob Pike136c04f2011-12-08 16:39:05 -080065</p>
66
67<pre><!--{{code "progs/go1.go" `/greeting := ..byte/` `/append.*hello/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +110068--> greeting := []byte{}
Andrew Gerrand5353e1e2012-01-06 09:20:31 +110069 greeting = append(greeting, []byte(&#34;hello &#34;)...)</pre>
Rob Pike136c04f2011-12-08 16:39:05 -080070
71<p>
72By analogy with the similar property of <code>copy</code>, Go 1
73permits a string to be appended (byte-wise) directly to a byte
Rob Pike68c7e8a2012-02-27 07:31:34 +110074slice, reducing the friction between strings and byte slices.
75The conversion is no longer necessary:
Rob Pike136c04f2011-12-08 16:39:05 -080076</p>
77
78<pre><!--{{code "progs/go1.go" `/append.*world/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +110079--> greeting = append(greeting, &#34;world&#34;...)</pre>
Rob Pike136c04f2011-12-08 16:39:05 -080080
81<p>
82<em>Updating</em>:
83This is a new feature, so existing code needs no changes.
84</p>
85
Rob Pikebab4dec2011-12-07 14:33:37 -080086<h3 id="close">Close</h3>
87
Rob Pike136c04f2011-12-08 16:39:05 -080088<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +110089The <code>close</code> predeclared function provides a mechanism
90for a sender to signal that no more values will be sent.
91It is important to the implementation of <code>for</code> <code>range</code>
92loops over channels and is helpful in other situations.
93Partly by design and partly because of race conditions that can occur otherwise,
94it is intended for use only by the goroutine sending on the channel,
95not by the goroutine receiving data.
96However, before Go 1 there was no compile-time checking that <code>close</code>
97was being used correctly.
98</p>
99
100<p>
101To close this gap, at least in part, Go 1 disallows <code>close</code> on receive-only channels.
102Attempting to close such a channel is a compile-time error.
Rob Pike136c04f2011-12-08 16:39:05 -0800103</p>
104
105<pre>
106 var c chan int
Robert Hencke7c9ee5f2012-01-25 21:09:46 -0800107 var csend chan&lt;- int = c
108 var crecv &lt;-chan int = c
Rob Pike136c04f2011-12-08 16:39:05 -0800109 close(c) // legal
110 close(csend) // legal
111 close(crecv) // illegal
112</pre>
113
114<p>
115<em>Updating</em>:
116Existing code that attempts to close a receive-only channel was
117erroneous even before Go 1 and should be fixed. The compiler will
118now reject such code.
119</p>
120
Rob Pike9d59c402011-12-08 11:35:28 -0800121<h3 id="literals">Composite literals</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800122
Rob Pike2e338fa2011-12-09 08:31:57 -0800123<p>
124In Go 1, a composite literal of array, slice, or map type can elide the
125type specification for the elements' initializers if they are of pointer type.
126All four of the initializations in this example are legal; the last one was illegal before Go 1.
127</p>
128
129<pre><!--{{code "progs/go1.go" `/type Date struct/` `/STOP/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100130--> type Date struct {
Rob Pike2e338fa2011-12-09 08:31:57 -0800131 month string
132 day int
133 }
134 // Struct values, fully qualified; always legal.
135 holiday1 := []Date{
136 Date{&#34;Feb&#34;, 14},
137 Date{&#34;Nov&#34;, 11},
138 Date{&#34;Dec&#34;, 25},
139 }
140 // Struct values, type name elided; always legal.
141 holiday2 := []Date{
142 {&#34;Feb&#34;, 14},
143 {&#34;Nov&#34;, 11},
144 {&#34;Dec&#34;, 25},
145 }
146 // Pointers, fully qualified, always legal.
147 holiday3 := []*Date{
148 &amp;Date{&#34;Feb&#34;, 14},
149 &amp;Date{&#34;Nov&#34;, 11},
150 &amp;Date{&#34;Dec&#34;, 25},
151 }
152 // Pointers, type name elided; legal in Go 1.
153 holiday4 := []*Date{
154 {&#34;Feb&#34;, 14},
155 {&#34;Nov&#34;, 11},
156 {&#34;Dec&#34;, 25},
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100157 }</pre>
Rob Pike2e338fa2011-12-09 08:31:57 -0800158
159<p>
160<em>Updating</em>:
161This change has no effect on existing code, but the command
162<code>gofmt</code> <code>-s</code> applied to existing source
163will, among other things, elide explicit element types wherever permitted.
164</p>
165
166
Rob Pike9d59c402011-12-08 11:35:28 -0800167<h3 id="init">Goroutines during init</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800168
Rob Pike136c04f2011-12-08 16:39:05 -0800169<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100170The old language defined that <code>go</code> statements executed during initialization created goroutines but that they did not begin to run until initialization of the entire program was complete.
171This introduced clumsiness in many places and, in effect, limited the utility
172of the <code>init</code> construct:
173if it was possible for another package to use the library during initialization, the library
174was forced to avoid goroutines.
175This design was done for reasons of simplicity and safety but,
176as our confidence in the language grew, it seemed unnecessary.
177Running goroutines during initialization is no more complex or unsafe than running them during normal execution.
178</p>
179
180<p>
181In Go 1, code that uses goroutines can be called from
Rob Pike136c04f2011-12-08 16:39:05 -0800182<code>init</code> routines and global initialization expressions
183without introducing a deadlock.
184</p>
185
186<pre><!--{{code "progs/go1.go" `/PackageGlobal/` `/^}/`}}
187-->var PackageGlobal int
188
189func init() {
190 c := make(chan int)
191 go initializationFunction(c)
192 PackageGlobal = &lt;-c
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100193}</pre>
Rob Pike136c04f2011-12-08 16:39:05 -0800194
195<p>
196<em>Updating</em>:
197This is a new feature, so existing code needs no changes,
198although it's possible that code that depends on goroutines not starting before <code>main</code> will break.
199There was no such code in the standard repository.
200</p>
201
Rob Pikebab4dec2011-12-07 14:33:37 -0800202<h3 id="rune">The rune type</h3>
203
Rob Pike2e338fa2011-12-09 08:31:57 -0800204<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100205The language spec allows the <code>int</code> type to be 32 or 64 bits wide, but current implementations set <code>int</code> to 32 bits even on 64-bit platforms.
206It would be preferable to have <code>int</code> be 64 bits on 64-bit platforms.
207(There are important consequences for indexing large slices.)
208However, this change would waste space when processing Unicode characters with
209the old language because the <code>int</code> type was also used to hold Unicode code points: each code point would waste an extra 32 bits of storage if <code>int</code> grew from 32 bits to 64.
210</p>
211
212<p>
213To make changing to 64-bit <code>int</code> feasible,
214Go 1 introduces a new basic type, <code>rune</code>, to represent
Rob Pike2e338fa2011-12-09 08:31:57 -0800215individual Unicode code points.
216It is an alias for <code>int32</code>, analogous to <code>byte</code>
217as an alias for <code>uint8</code>.
218</p>
219
220<p>
221Character literals such as <code>'a'</code>, <code>'่ชž'</code>, and <code>'\u0345'</code>
222now have default type <code>rune</code>,
223analogous to <code>1.0</code> having default type <code>float64</code>.
224A variable initialized to a character constant will therefore
225have type <code>rune</code> unless otherwise specified.
226</p>
227
228<p>
229Libraries have been updated to use <code>rune</code> rather than <code>int</code>
230when appropriate. For instance, the functions <code>unicode.ToLower</code> and
231relatives now take and return a <code>rune</code>.
232</p>
233
234<pre><!--{{code "progs/go1.go" `/STARTRUNE/` `/ENDRUNE/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100235--> delta := &#39;ฮด&#39; // delta has type rune.
Rob Pike2e338fa2011-12-09 08:31:57 -0800236 var DELTA rune
237 DELTA = unicode.ToUpper(delta)
238 epsilon := unicode.ToLower(DELTA + 1)
239 if epsilon != &#39;ฮด&#39;+1 {
240 log.Fatal(&#34;inconsistent casing for Greek&#34;)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100241 }</pre>
Rob Pike2e338fa2011-12-09 08:31:57 -0800242
243<p>
244<em>Updating</em>:
245Most source code will be unaffected by this because the type inference from
246<code>:=</code> initializers introduces the new type silently, and it propagates
247from there.
248Some code may get type errors that a trivial conversion will resolve.
249</p>
250
251<h3 id="error">The error type</h3>
252
253<p>
254Go 1 introduces a new built-in type, <code>error</code>, which has the following definition:
255</p>
256
257<pre>
258 type error interface {
259 Error() string
260 }
261</pre>
262
263<p>
264Since the consequences of this type are all in the package library,
Rob Pikeebdcbf12011-12-12 12:26:56 -0800265it is discussed <a href="#errors">below</a>.
Rob Pike2e338fa2011-12-09 08:31:57 -0800266</p>
267
Rob Pike9d59c402011-12-08 11:35:28 -0800268<h3 id="delete">Deleting from maps</h3>
Rob Pike2fa987b2011-12-07 16:11:17 -0800269
270<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100271In the old language, to delete the entry with key <code>k</code> from map <code>m</code>, one wrote the statement,
Rob Pike2fa987b2011-12-07 16:11:17 -0800272</p>
273
274<pre>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100275 m[k] = value, false
Rob Pike2fa987b2011-12-07 16:11:17 -0800276</pre>
277
278<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100279This syntax was a peculiar special case, the only two-to-one assignment.
280It required passing a value (usually ignored) that is evaluated but discarded,
281plus a boolean that was nearly always the constant <code>false</code>.
282It did the job but was odd and a point of contention.
283</p>
284
285<p>
Rob Pike9d59c402011-12-08 11:35:28 -0800286In Go 1, that syntax has gone; instead there is a new built-in
Rob Pike2fa987b2011-12-07 16:11:17 -0800287function, <code>delete</code>. The call
288</p>
289
290<pre><!--{{code "progs/go1.go" `/delete\(m, k\)/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100291--> delete(m, k)</pre>
Rob Pike2fa987b2011-12-07 16:11:17 -0800292
293<p>
294will delete the map entry retrieved by the expression <code>m[k]</code>.
295There is no return value. Deleting a non-existent entry is a no-op.
296</p>
297
298<p>
299<em>Updating</em>:
Rob Pike68c7e8a2012-02-27 07:31:34 +1100300Running <code>go</code> <code>fix</code> will convert expressions of the form <code>m[k] = value,
Rob Pike2fa987b2011-12-07 16:11:17 -0800301false</code> into <code>delete(m, k)</code> when it is clear that
302the ignored value can be safely discarded from the program and
Rob Pike27836912012-02-04 07:49:51 +1100303<code>false</code> refers to the predefined boolean constant.
304The fix tool
Rob Pike2fa987b2011-12-07 16:11:17 -0800305will flag other uses of the syntax for inspection by the programmer.
306</p>
307
Rob Pike136c04f2011-12-08 16:39:05 -0800308<h3 id="iteration">Iterating in maps</h3>
309
310<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100311The old language specification did not define the order of iteration for maps,
312and in practice it differed across hardware platforms.
313This caused tests that iterated over maps to be fragile and non-portable, with the
314unpleasant property that a test might always pass on one machine but break on another.
315</p>
316
317<p>
Rob Pike136c04f2011-12-08 16:39:05 -0800318In Go 1, the order in which elements are visited when iterating
319over a map using a <code>for</code> <code>range</code> statement
320is defined to be unpredictable, even if the same loop is run multiple
321times with the same map.
322Code should not assume that the elements are visited in any particular order.
323</p>
324
Rob Pike68c7e8a2012-02-27 07:31:34 +1100325<p>
326This change means that code that depends on iteration order is very likely to break early and be fixed long before it becomes a problem.
327Just as important, it allows the map implementation to ensure better map balancing even when programs are using range loops to select an element from a map.
328</p>
329
Rob Pike136c04f2011-12-08 16:39:05 -0800330<pre><!--{{code "progs/go1.go" `/Sunday/` `/^ }/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100331--> m := map[string]int{&#34;Sunday&#34;: 0, &#34;Monday&#34;: 1}
Rob Pike136c04f2011-12-08 16:39:05 -0800332 for name, value := range m {
333 // This loop should not assume Sunday will be visited first.
334 f(name, value)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100335 }</pre>
Rob Pike136c04f2011-12-08 16:39:05 -0800336
337<p>
338<em>Updating</em>:
339This is one change where tools cannot help. Most existing code
340will be unaffected, but some programs may break or misbehave; we
341recommend manual checking of all range statements over maps to
342verify they do not depend on iteration order. There were a few such
343examples in the standard repository; they have been fixed.
344Note that it was already incorrect to depend on the iteration order, which
345was unspecified. This change codifies the unpredictability.
346</p>
Rob Pikebab4dec2011-12-07 14:33:37 -0800347
348<h3 id="multiple_assignment">Multiple assignment</h3>
349
Rob Pike136c04f2011-12-08 16:39:05 -0800350<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100351The language specification guarantees that in assignments
352the right-hand-side expressions are all evaluated before any left-hand-side expressions are assigned.
353To guarantee predictable behavior,
354Go 1 refines the specification further.
355</p>
356
357<p>
358If the left-hand side of the assignment
Rob Pike136c04f2011-12-08 16:39:05 -0800359statement contains expressions that require evaluation, such as
360function calls or array indexing operations, these will all be done
361using the usual left-to-right rule before any variables are assigned
362their value. Once everything is evaluated, the actual assignments
363proceed in left-to-right order.
364</p>
365
366<p>
367These examples illustrate the behavior.
368</p>
369
370<pre><!--{{code "progs/go1.go" `/sa :=/` `/then sc.0. = 2/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100371--> sa := []int{1, 2, 3}
Rob Pike136c04f2011-12-08 16:39:05 -0800372 i := 0
373 i, sa[i] = 1, 2 // sets i = 1, sa[0] = 2
374
375 sb := []int{1, 2, 3}
376 j := 0
377 sb[j], j = 2, 1 // sets sb[0] = 2, j = 1
378
379 sc := []int{1, 2, 3}
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100380 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 -0800381
Rob Pike2e338fa2011-12-09 08:31:57 -0800382<p>
Rob Pike136c04f2011-12-08 16:39:05 -0800383<em>Updating</em>:
384This is one change where tools cannot help, but breakage is unlikely.
385No code in the standard repository was broken by this change, and code
386that depended on the previous unspecified behavior was already incorrect.
387</p>
388
Rob Pikebab4dec2011-12-07 14:33:37 -0800389<h3 id="shadowing">Returns and shadowed variables</h3>
390
Rob Pike136c04f2011-12-08 16:39:05 -0800391<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100392A common mistake is to use <code>return</code> (without arguments) after an assignment to a variable that has the same name as a result variable but is not the same variable.
393This situation is called <em>shadowing</em>: the result variable has been shadowed by another variable with the same name declared in an inner scope.
394</p>
395
396<p>
Rob Pike136c04f2011-12-08 16:39:05 -0800397In functions with named return values,
398the Go 1 compilers disallow return statements without arguments if any of the named return values is shadowed at the point of the return statement.
399(It isn't part of the specification, because this is one area we are still exploring;
400the situation is analogous to the compilers rejecting functions that do not end with an explicit return statement.)
401</p>
402
403<p>
404This function implicitly returns a shadowed return value and will be rejected by the compiler:
405</p>
406
407<pre>
408 func Bug() (i, j, k int) {
Robert Hencke7c9ee5f2012-01-25 21:09:46 -0800409 for i = 0; i &lt; 5; i++ {
410 for j := 0; j &lt; 5; j++ { // Redeclares j.
Rob Pike136c04f2011-12-08 16:39:05 -0800411 k += i*j
412 if k > 100 {
413 return // Rejected: j is shadowed here.
414 }
415 }
416 }
417 return // OK: j is not shadowed here.
418 }
419</pre>
420
421<p>
422<em>Updating</em>:
423Code that shadows return values in this way will be rejected by the compiler and will need to be fixed by hand.
424The few cases that arose in the standard repository were mostly bugs.
425</p>
426
427<h3 id="unexported">Copying structs with unexported fields</h3>
428
Rob Pike2e338fa2011-12-09 08:31:57 -0800429<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100430The old language did not allow a package to make a copy of a struct value containing unexported fields belonging to a different package.
431There was, however, a required exception for a method receiver;
432also, the implementations of <code>copy</code> and <code>append</code> have never honored the restriction.
433</p>
434
435<p>
436Go 1 will allow packages to copy struct values containing unexported fields from other packages.
437Besides resolving the inconsistency,
438this change admits a new kind of API: a package can return an opaque value without resorting to a pointer or interface.
439The new implementations of <code>time.Time</code> and
440<code>reflect.Value</code> are examples of types taking advantage of this new property.
Rob Pike2e338fa2011-12-09 08:31:57 -0800441</p>
442
443<p>
444As an example, if package <code>p</code> includes the definitions,
445</p>
446
447<pre>
448 type Struct struct {
449 Public int
450 secret int
451 }
452 func NewStruct(a int) Struct { // Note: not a pointer.
453 return Struct{a, f(a)}
454 }
455 func (s Struct) String() string {
456 return fmt.Sprintf("{%d (secret %d)}", s.Public, s.secret)
457 }
458</pre>
459
460<p>
461a package that imports <code>p</code> can assign and copy values of type
462<code>p.Struct</code> at will.
463Behind the scenes the unexported fields will be assigned and copied just
464as if they were exported,
465but the client code will never be aware of them. The code
466</p>
467
468<pre>
469 import "p"
470
471 myStruct := p.NewStruct(23)
472 copyOfMyStruct := myStruct
473 fmt.Println(myStruct, copyOfMyStruct)
474</pre>
475
476<p>
477will show that the secret field of the struct has been copied to the new value.
478</p>
479
480<p>
481<em>Updating</em>:
482This is a new feature, so existing code needs no changes.
483</p>
484
Rob Pike68c7e8a2012-02-27 07:31:34 +1100485<h3 id="equality">Equality</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800486
Rob Pike136c04f2011-12-08 16:39:05 -0800487<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100488Before Go 1, the language did not define equality on struct and array values.
489This meant,
490among other things, that structs and arrays could not be used as map keys.
491On the other hand, Go did define equality on function and map values.
492Function equality was problematic in the presence of closures
493(when are two closures equal?)
494while map equality compared pointers, not the maps' content, which was usually
495not what the user would want.
Rob Pike136c04f2011-12-08 16:39:05 -0800496</p>
497
498<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100499Go 1 addressed these issues.
500First, structs and arrays can be compared for equality and inequality
501(<code>==</code> and <code>!=</code>),
502and therefore be used as map keys,
503provided they are composed from elements for which equality is also defined,
504using element-wise comparison.
Rob Pike136c04f2011-12-08 16:39:05 -0800505</p>
506
507<pre><!--{{code "progs/go1.go" `/type Day struct/` `/Printf/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100508--> type Day struct {
Rob Pike5fa18e12011-12-12 21:08:03 -0800509 long string
510 short string
511 }
512 Christmas := Day{&#34;Christmas&#34;, &#34;XMas&#34;}
513 Thanksgiving := Day{&#34;Thanksgiving&#34;, &#34;Turkey&#34;}
514 holiday := map[Day]bool{
515 Christmas: true,
516 Thanksgiving: true,
517 }
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100518 fmt.Printf(&#34;Christmas is a holiday: %t\n&#34;, holiday[Christmas])</pre>
Rob Pike136c04f2011-12-08 16:39:05 -0800519
520<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100521Second, Go 1 removes the definition of equality for function values,
522except for comparison with <code>nil</code>.
523Finally, Map equality is gone too, also except for comparison with <code>nil</code>,
524although it may return one day in a more intuitive form.
525</p>
526
527<p>
Rob Pike136c04f2011-12-08 16:39:05 -0800528Note that equality is still undefined for slices, for which the
529calculation is in general infeasible. Also note that the ordered
530comparison operators (<code>&lt;</code> <code>&lt;=</code>
531<code>&gt;</code> <code>&gt;=</code>) are still undefined for
532structs and arrays.
533
534<p>
535<em>Updating</em>:
Rob Pike68c7e8a2012-02-27 07:31:34 +1100536Struct and array equality is a new feature, so existing code needs no changes.
Rob Pike136c04f2011-12-08 16:39:05 -0800537Existing code that depends on function or map equality will be
538rejected by the compiler and will need to be fixed by hand.
539Few programs will be affected, but the fix may require some
540redesign.
541</p>
542
Rob Pike0a1376a2012-01-20 14:28:48 -0800543<h2 id="packages">The package hierarchy</h2>
544
545<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100546Go 1 addresses many deficiencies in the old standard library and
547cleans up a number of packages, making them more internally consistent
548and portable.
549</p>
550
551<p>
Rob Pike0a1376a2012-01-20 14:28:48 -0800552This section describes how the packages have been rearranged in Go 1.
553Some have moved, some have been renamed, some have been deleted.
554New packages are described in later sections.
555</p>
Rob Pikebab4dec2011-12-07 14:33:37 -0800556
Rob Pike9d59c402011-12-08 11:35:28 -0800557<h3 id="hierarchy">The package hierarchy</h3>
558
559<p>
560Go 1 has a rearranged package hierarchy that groups related items
561into subdirectories. For instance, <code>utf8</code> and
562<code>utf16</code> now occupy subdirectories of <code>unicode</code>.
563Also, <a href="#subrepo">some packages</a> have moved into
564subrepositories of
565<a href="http://code.google.com/p/go"><code>code.google.com/p/go</code></a>
566while <a href="#deleted">others</a> have been deleted outright.
567</p>
568
569<table class="codetable" frame="border" summary="Moved packages">
570<colgroup align="left" width="60%"></colgroup>
571<colgroup align="left" width="40%"></colgroup>
572<tr>
573<th align="left">Old path</th>
574<th align="left">New path</th>
575</tr>
Rob Pike71ccf732011-12-09 14:12:51 -0800576<tr>
577<td colspan="2"><hr></td>
578</tr>
Rob Pike136c04f2011-12-08 16:39:05 -0800579<tr><td>asn1</td> <td>encoding/asn1</td></tr>
Rob Pike9d59c402011-12-08 11:35:28 -0800580<tr><td>csv</td> <td>encoding/csv</td></tr>
581<tr><td>gob</td> <td>encoding/gob</td></tr>
582<tr><td>json</td> <td>encoding/json</td></tr>
583<tr><td>xml</td> <td>encoding/xml</td></tr>
584<tr>
585<td colspan="2"><hr></td>
586</tr>
587<tr><td>exp/template/html</td> <td>html/template</td></tr>
588<tr>
589<td colspan="2"><hr></td>
590</tr>
591<tr><td>big</td> <td>math/big</td></tr>
592<tr><td>cmath</td> <td>math/cmplx</td></tr>
593<tr><td>rand</td> <td>math/rand</td></tr>
594<tr>
595<td colspan="2"><hr></td>
596</tr>
597<tr><td>http</td> <td>net/http</td></tr>
598<tr><td>http/cgi</td> <td>net/http/cgi</td></tr>
599<tr><td>http/fcgi</td> <td>net/http/fcgi</td></tr>
600<tr><td>http/httptest</td> <td>net/http/httptest</td></tr>
601<tr><td>http/pprof</td> <td>net/http/pprof</td></tr>
602<tr><td>mail</td> <td>net/mail</td></tr>
603<tr><td>rpc</td> <td>net/rpc</td></tr>
604<tr><td>rpc/jsonrpc</td> <td>net/rpc/jsonrpc</td></tr>
605<tr><td>smtp</td> <td>net/smtp</td></tr>
606<tr><td>url</td> <td>net/url</td></tr>
607<tr>
608<td colspan="2"><hr></td>
609</tr>
610<tr><td>exec</td> <td>os/exec</td></tr>
611<tr>
612<td colspan="2"><hr></td>
613</tr>
614<tr><td>scanner</td> <td>text/scanner</td></tr>
615<tr><td>tabwriter</td> <td>text/tabwriter</td></tr>
616<tr><td>template</td> <td>text/template</td></tr>
617<tr><td>template/parse</td> <td>text/template/parse</td></tr>
618<tr>
619<td colspan="2"><hr></td>
620</tr>
621<tr><td>utf8</td> <td>unicode/utf8</td></tr>
622<tr><td>utf16</td> <td>unicode/utf16</td></tr>
623</table>
624
625<p>
626Note that the package names for the old <code>cmath</code> and
627<code>exp/template/html</code> packages have changed to <code>cmplx</code>
628and <code>template</code>.
629</p>
630
631<p>
632<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +1100633Running <code>go</code> <code>fix</code> will update all imports and package renames for packages that
Rob Pike9d59c402011-12-08 11:35:28 -0800634remain inside the standard repository. Programs that import packages
635that are no longer in the standard repository will need to be edited
636by hand.
Rob Pike9d59c402011-12-08 11:35:28 -0800637</p>
Rob Pikebab4dec2011-12-07 14:33:37 -0800638
Rob Pike0a1376a2012-01-20 14:28:48 -0800639<h3 id="exp">The package tree exp</h3>
640
641<p>
642Because they are not standardized, the packages under the <code>exp</code> directory will not be available in the
643standard Go 1 release distributions, although they will be available in source code form
644in <a href="http://code.google.com/p/go/">the repository</a> for
645developers who wish to use them.
646</p>
647
648<p>
649Several packages have moved under <code>exp</code> at the time of Go 1's release:
650</p>
651
652<ul>
653<li><code>ebnf</code></li>
654<li><code>go/types</code></li>
David Symondsb68d9472012-02-02 09:08:50 +1100655<li><code>os/signal</code></li>
Rob Pike0a1376a2012-01-20 14:28:48 -0800656</ul>
657
658<p>
Rob Pike531ded92012-01-20 15:38:03 -0800659All these packages are available under the same names, with the prefix <code>exp/</code>: <code>exp/ebnf</code> etc.
660</p>
661
662<p>
Rob Pike0a1376a2012-01-20 14:28:48 -0800663Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>.
664</p>
665
666<p>
Rob Pike531ded92012-01-20 15:38:03 -0800667Finally, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while
Rob Pike27836912012-02-04 07:49:51 +1100668<code>ebnflint</code> is now in <code>exp/ebnflint</code>.
669If they are installed, they now reside in <code>$GOROOT/bin/tool</code>.
Rob Pike0a1376a2012-01-20 14:28:48 -0800670</p>
671
672<p>
673<em>Updating</em>:
674Code that uses packages in <code>exp</code> will need to be updated by hand,
675or else compiled from an installation that has <code>exp</code> available.
Rob Pike5cff0292012-02-24 13:08:11 +1100676The <code>go</code> <code>fix</code> tool or the compiler will complain about such uses.
Rob Pike0a1376a2012-01-20 14:28:48 -0800677</p>
678
679<h3 id="old">The package tree old</h3>
680
681<p>
682Because they are deprecated, the packages under the <code>old</code> directory will not be available in the
683standard Go 1 release distributions, although they will be available in source code form for
684developers who wish to use them.
685</p>
686
687<p>
688The packages in their new locations are:
689</p>
690
691<ul>
692<li><code>old/netchan</code></li>
693<li><code>old/regexp</code></li>
694<li><code>old/template</code></li>
695</ul>
696
697<p>
698<em>Updating</em>:
699Code that uses packages now in <code>old</code> will need to be updated by hand,
700or else compiled from an installation that has <code>old</code> available.
Rob Pike5cff0292012-02-24 13:08:11 +1100701The <code>go</code> <code>fix</code> tool will warn about such uses.
Rob Pike0a1376a2012-01-20 14:28:48 -0800702</p>
703
704<h3 id="deleted">Deleted packages</h3>
705
706<p>
707Go 1 deletes several packages outright:
708</p>
709
710<ul>
711<li><code>container/vector</code></li>
712<li><code>exp/datafmt</code></li>
713<li><code>go/typechecker</code></li>
714<li><code>try</code></li>
715</ul>
716
717<p>
718and also the command <code>gotry</code>.
719</p>
720
721<p>
722<em>Updating</em>:
723Code that uses <code>container/vector</code> should be updated to use
724slices directly. See
725<a href="http://code.google.com/p/go-wiki/wiki/SliceTricks">the Go
726Language Community Wiki</a> for some suggestions.
727Code that uses the other packages (there should be almost zero) will need to be rethought.
Rob Pike0a1376a2012-01-20 14:28:48 -0800728</p>
729
730<h3 id="subrepo">Packages moving to subrepositories</h3>
731
Rob Pike7eaad5e2012-01-25 13:29:25 -0800732<p>
733Go 1 has moved a number of packages into sub-repositories of
734<a href="http://code.google.com/p/go/">the main Go repository</a>.
735This table lists the old and new import paths:
736
737<table class="codetable" frame="border" summary="Sub-repositories">
738<colgroup align="left" width="40%"></colgroup>
739<colgroup align="left" width="60%"></colgroup>
740<tr>
741<th align="left">Old</th>
742<th align="left">New</th>
743</tr>
744<tr>
745<td colspan="2"><hr></td>
746</tr>
747<tr><td>crypto/bcrypt</td> <td>code.google.com/p/go.crypto/bcrypt</tr>
748<tr><td>crypto/blowfish</td> <td>code.google.com/p/go.crypto/blowfish</tr>
749<tr><td>crypto/cast5</td> <td>code.google.com/p/go.crypto/cast5</tr>
750<tr><td>crypto/md4</td> <td>code.google.com/p/go.crypto/md4</tr>
751<tr><td>crypto/ocsp</td> <td>code.google.com/p/go.crypto/ocsp</tr>
752<tr><td>crypto/openpgp</td> <td>code.google.com/p/go.crypto/openpgp</tr>
753<tr><td>crypto/openpgp/armor</td> <td>code.google.com/p/go.crypto/openpgp/armor</tr>
754<tr><td>crypto/openpgp/elgamal</td> <td>code.google.com/p/go.crypto/openpgp/elgamal</tr>
755<tr><td>crypto/openpgp/errors</td> <td>code.google.com/p/go.crypto/openpgp/errors</tr>
756<tr><td>crypto/openpgp/packet</td> <td>code.google.com/p/go.crypto/openpgp/packet</tr>
757<tr><td>crypto/openpgp/s2k</td> <td>code.google.com/p/go.crypto/openpgp/s2k</tr>
758<tr><td>crypto/ripemd160</td> <td>code.google.com/p/go.crypto/ripemd160</tr>
759<tr><td>crypto/twofish</td> <td>code.google.com/p/go.crypto/twofish</tr>
760<tr><td>crypto/xtea</td> <td>code.google.com/p/go.crypto/xtea</tr>
761<tr><td>exp/ssh</td> <td>code.google.com/p/go.crypto/ssh</tr>
762<tr>
763<td colspan="2"><hr></td>
764</tr>
Nigel Taoceb1ca62012-01-31 12:29:00 +1100765<tr><td>image/bmp</td> <td>code.google.com/p/go.image/bmp</tr>
766<tr><td>image/tiff</td> <td>code.google.com/p/go.image/tiff</tr>
767<tr>
768<td colspan="2"><hr></td>
769</tr>
Rob Pike7eaad5e2012-01-25 13:29:25 -0800770<tr><td>net/dict</td> <td>code.google.com/p/go.net/dict</tr>
771<tr><td>net/websocket</td> <td>code.google.com/p/go.net/websocket</tr>
772<tr><td>exp/spdy</td> <td>code.google.com/p/go.net/spdy</tr>
773<tr>
774<td colspan="2"><hr></td>
775</tr>
776<tr><td>encoding/git85</td> <td>code.google.com/p/go.codereview/git85</tr>
777<tr><td>patch</td> <td>code.google.com/p/go.codereview/patch</tr>
778</table>
779
780<p>
781<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +1100782Running <code>go</code> <code>fix</code> will update imports of these packages to use the new import paths.
Rob Pike7eaad5e2012-01-25 13:29:25 -0800783Installations that depend on these packages will need to install them using
784a <code>go install</code> command.
785</p>
Rob Pike0a1376a2012-01-20 14:28:48 -0800786
787<h2 id="major">Major changes to the library</h2>
788
789<p>
790This section describes significant changes to the core libraries, the ones that
791affect the most programs.
792</p>
793
Rob Pike71ccf732011-12-09 14:12:51 -0800794<h3 id="errors">The error type and errors package</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800795
Rob Pikeebdcbf12011-12-12 12:26:56 -0800796<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100797The placement of <code>os.Error</code> in package <code>os</code> is mostly historical: errors first came up when implementing package <code>os</code>, and they seemed system-related at the time.
798Since then it has become clear that errors are more fundamental than the operating system. For example, it would be nice to use <code>Errors</code> in packages that <code>os</code> depends on, like <code>syscall</code>.
799Also, having <code>Error</code> in <code>os</code> introduces many dependencies on <code>os</code> that would otherwise not exist.
800</p>
801
802<p>
803Go 1 solves these problems by introducing a built-in <code>error</code> interface type and a separate <code>errors</code> package (analogous to <code>bytes</code> and <code>strings</code>) that contains utility functions.
804It replaces <code>os.NewError</code> with
805<a href="/pkg/errors/#New"><code>errors.New</code></a>,
806giving errors a more central place in the environment.
807</p>
808
809<p>
Rob Pikeebdcbf12011-12-12 12:26:56 -0800810So the widely-used <code>String</code> method does not cause accidental satisfaction
811of the <code>error</code> interface, the <code>error</code> interface uses instead
812the name <code>Error</code> for that method:
813</p>
814
815<pre>
816 type error interface {
817 Error() string
818 }
819</pre>
820
821<p>
822The <code>fmt</code> library automatically invokes <code>Error</code>, as it already
823does for <code>String</code>, for easy printing of error values.
824</p>
825
826<pre><!--{{code "progs/go1.go" `/START ERROR EXAMPLE/` `/END ERROR EXAMPLE/`}}
827-->type SyntaxError struct {
828 File string
829 Line int
830 Message string
831}
832
833func (se *SyntaxError) Error() string {
834 return fmt.Sprintf(&#34;%s:%d: %s&#34;, se.File, se.Line, se.Message)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100835}</pre>
Rob Pikeebdcbf12011-12-12 12:26:56 -0800836
837<p>
838All standard packages have been updated to use the new interface; the old <code>os.Error</code> is gone.
839</p>
840
841<p>
842A new package, <a href="/pkg/errors/"><code>errors</code></a>, contains the function
843</p>
844
845<pre>
846func New(text string) error
847</pre>
848
849<p>
850to turn a string into an error. It replaces the old <code>os.NewError</code>.
851</p>
852
853<pre><!--{{code "progs/go1.go" `/ErrSyntax/`}}
Andrew Gerrand468e6922012-01-09 20:05:34 +1100854--> var ErrSyntax = errors.New(&#34;syntax error&#34;)</pre>
Rob Pikeebdcbf12011-12-12 12:26:56 -0800855
856<p>
857<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +1100858Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
Rob Pikeebdcbf12011-12-12 12:26:56 -0800859Code that defines error types with a <code>String</code> method will need to be updated
860by hand to rename the methods to <code>Error</code>.
861</p>
862
Rob Pike9d59c402011-12-08 11:35:28 -0800863<h3 id="errno">System call errors</h3>
Rob Pikebab4dec2011-12-07 14:33:37 -0800864
Rob Pike71ccf732011-12-09 14:12:51 -0800865<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100866The old <code>syscall</code> package, which predated <code>os.Error</code>
867(and just about everything else),
868returned errors as <code>int</code> values.
869In turn, the <code>os</code> package forwarded many of these errors, such
870as <code>EINVAL</code>, but using a different set of errors on each platform.
871This behavior was unpleasant and unportable.
872</p>
873
874<p>
Rob Pike71ccf732011-12-09 14:12:51 -0800875In Go 1, the
Rob Pikeebdcbf12011-12-12 12:26:56 -0800876<a href="/pkg/syscall/"><code>syscall</code></a>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100877package instead returns an <code>error</code> for system call errors.
Rob Pike71ccf732011-12-09 14:12:51 -0800878On Unix, the implementation is done by a
Rob Pikeebdcbf12011-12-12 12:26:56 -0800879<a href="/pkg/syscall/#Errno"><code>syscall.Errno</code></a> type
Rob Pike71ccf732011-12-09 14:12:51 -0800880that satisfies <code>error</code> and replaces the old <code>os.Errno</code>.
881</p>
882
883<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100884The changes affecting <code>os.EINVAL</code> and relatives are
885described <a href="#os">elsewhere</a>.
886
887<p>
Rob Pike71ccf732011-12-09 14:12:51 -0800888<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +1100889Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
Rob Pike71ccf732011-12-09 14:12:51 -0800890Regardless, most code should use the <code>os</code> package
891rather than <code>syscall</code> and so will be unaffected.
892</p>
893
Rob Pikebab4dec2011-12-07 14:33:37 -0800894<h3 id="time">Time</h3>
895
Rob Pike5fa18e12011-12-12 21:08:03 -0800896<p>
Rob Pike68c7e8a2012-02-27 07:31:34 +1100897Time is always a challenge to support well in a programming language.
898The old Go <code>time</code> package had <code>int64</code> units, no
899real type safety,
900and no distinction between absolute times and durations.
901</p>
902
903<p>
904One of the most sweeping changes in the Go 1 library is therefore a
Rob Pike5fa18e12011-12-12 21:08:03 -0800905complete redesign of the
906<a href="/pkg/time/"><code>time</code></a> package.
907Instead of an integer number of nanoseconds as an <code>int64</code>,
908and a separate <code>*time.Time</code> type to deal with human
909units such as hours and years,
910there are now two fundamental types:
911<a href="/pkg/time/#Time"><code>time.Time</code></a>
912(a value, so the <code>*</code> is gone), which represents a moment in time;
913and <a href="/pkg/time/#Duration"><code>time.Duration</code></a>,
914which represents an interval.
915Both have nanosecond resolution.
916A <code>Time</code> can represent any time into the ancient
917past and remote future, while a <code>Duration</code> can
918span plus or minus only about 290 years.
919There are methods on these types, plus a number of helpful
920predefined constant durations such as <code>time.Second</code>.
921</p>
922
923<p>
924Among the new methods are things like
925<a href="/pkg/time/#Time.Add"><code>Time.Add</code></a>,
926which adds a <code>Duration</code> to a <code>Time</code>, and
927<a href="/pkg/time/#Time.Sub"><code>Time.Sub</code></a>,
928which subtracts two <code>Times</code> to yield a <code>Duration</code>.
929</p>
930
931<p>
932The most important semantic change is that the Unix epoch (Jan 1, 1970) is now
933relevant only for those functions and methods that mention Unix:
934<a href="/pkg/time/#Unix"><code>time.Unix</code></a>
935and the <a href="/pkg/time/#Time.Unix"><code>Unix</code></a>
936and <a href="/pkg/time/#Time.UnixNano"><code>UnixNano</code></a> methods
937of the <code>Time</code> type.
938In particular,
939<a href="/pkg/time/#Now"><code>time.Now</code></a>
940returns a <code>time.Time</code> value rather than, in the old
941API, an integer nanosecond count since the Unix epoch.
942</p>
943
944<pre><!--{{code "progs/go1.go" `/sleepUntil/` `/^}/`}}
945-->// sleepUntil sleeps until the specified time. It returns immediately if it&#39;s too late.
946func sleepUntil(wakeup time.Time) {
947 now := time.Now() // A Time.
948 if !wakeup.After(now) {
949 return
950 }
951 delta := wakeup.Sub(now) // A Duration.
952 log.Printf(&#34;Sleeping for %.3fs&#34;, delta.Seconds())
953 time.Sleep(delta)
Andrew Gerrand5353e1e2012-01-06 09:20:31 +1100954}</pre>
Rob Pike5fa18e12011-12-12 21:08:03 -0800955
956<p>
957The new types, methods, and constants have been propagated through
958all the standard packages that use time, such as <code>os</code> and
959its representation of file time stamps.
960</p>
961
962<p>
963<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +1100964The <code>go</code> <code>fix</code> tool will update many uses of the old <code>time</code> package to use the new
Rob Pike5fa18e12011-12-12 21:08:03 -0800965types and methods, although it does not replace values such as <code>1e9</code>
966representing nanoseconds per second.
967Also, because of type changes in some of the values that arise,
Rob Pike27836912012-02-04 07:49:51 +1100968some of the expressions rewritten by the fix tool may require
Rob Pike5fa18e12011-12-12 21:08:03 -0800969further hand editing; in such cases the rewrite will include
970the correct function or method for the old functionality, but
971may have the wrong type or require further analysis.
972</p>
973
Rob Pike0a1376a2012-01-20 14:28:48 -0800974<h2 id="minor">Minor changes to the library</h2>
975
976<p>
977This section describes smaller changes, such as those to less commonly
978used packages or that affect
Rob Pike5cff0292012-02-24 13:08:11 +1100979few programs beyond the need to run <code>go</code> <code>fix</code>.
Rob Pike0a1376a2012-01-20 14:28:48 -0800980This category includes packages that are new in Go 1.
Rob Pike68c7e8a2012-02-27 07:31:34 +1100981Collectively they improve portability, regularize behavior, and
982make the interfaces more modern and Go-like.
Rob Pike0a1376a2012-01-20 14:28:48 -0800983</p>
984
Andrew Gerrand04868b22012-02-14 10:47:48 +1100985<h3 id="archive_zip">The archive/zip package</h3>
986
987<p>
988In Go 1, <a href="/pkg/archive/zip/#Writer"><code>*zip.Writer</code></a> no
989longer has a <code>Write</code> method. Its presence was a mistake.
990</p>
991
992<p>
Nigel Taoda8f0372012-02-15 14:41:47 +1100993<em>Updating</em>:
994What little code is affected will be caught by the compiler and must be updated by hand.
Andrew Gerrand04868b22012-02-14 10:47:48 +1100995</p>
996
Adam Langleyc2e58dc2012-02-14 07:13:57 -0500997<h3 id="bufio">The bufio package</h3>
998
999<p>
1000In Go 1, <a href="/pkg/bufio/#NewReaderSize"><code>bufio.NewReaderSize</code></a>
1001and
1002<a href="/pkg/bufio/#NewWriterSize"><code>bufio.NewWriterSize</code></a>
1003functions no longer return an error for invalid sizes.
1004If the argument size is too small or invalid, it is adjusted.
1005</p>
1006
1007<p>
1008<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001009Running <code>go</code> <code>fix</code> will update calls that assign the error to _.
Nigel Taoda8f0372012-02-15 14:41:47 +11001010Calls that aren't fixed will be caught by the compiler and must be updated by hand.
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001011</p>
1012
Rob Pikec3ef1982012-02-19 14:15:26 +11001013<h3 id="compress">The compress/flate, compress/gzip and compress/zlib packages</h3>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001014
1015<p>
1016In Go 1, the <code>NewWriterXxx</code> functions in
1017<a href="/pkg/compress/flate"><code>compress/flate</code></a>,
1018<a href="/pkg/compress/gzip"><code>compress/gzip</code></a> and
1019<a href="/pkg/compress/zlib"><code>compress/zlib</code></a>
1020all return <code>(*Writer, error)</code> if they take a compression level,
1021and <code>*Writer</code> otherwise. Package <code>gzip</code>'s
1022<code>Compressor</code> and <code>Decompressor</code> types have been renamed
1023to <code>Writer</code> and <code>Reader</code>. Package <code>flate</code>'s
1024<code>WrongValueError</code> type has been removed.
1025</p>
1026
1027<p>
1028<em>Updating</em>
Rob Pike5cff0292012-02-24 13:08:11 +11001029Running <code>go</code> <code>fix</code> will update old names and calls that assign the error to _.
Nigel Taoda8f0372012-02-15 14:41:47 +11001030Calls that aren't fixed will be caught by the compiler and must be updated by hand.
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001031</p>
1032
Adam Langleycdd7e022012-02-13 12:38:45 -05001033<h3 id="crypto_aes_des">The crypto/aes and crypto/des packages</h3>
Rob Pikebb7b1a12012-02-08 13:07:13 +11001034
1035<p>
Adam Langleycdd7e022012-02-13 12:38:45 -05001036In Go 1, the <code>Reset</code> method has been removed. Go does not guarantee
1037that memory is not copied and therefore this method was misleading.
1038</p>
1039
1040<p>
1041The cipher-specific types <code>*aes.Cipher</code>, <code>*des.Cipher</code>,
1042and <code>*des.TripleDESCipher</code> have been removed in favor of
1043<code>cipher.Block</code>.
Rob Pikebb7b1a12012-02-08 13:07:13 +11001044</p>
1045
1046<p>
1047<em>Updating</em>:
Adam Langleycdd7e022012-02-13 12:38:45 -05001048Remove the calls to Reset. Replace uses of the specific cipher types with
1049cipher.Block.
Nigel Taocc9ed442012-02-10 18:49:19 +11001050</p>
1051
Rob Pike0a1376a2012-01-20 14:28:48 -08001052<h3 id="crypto_elliptic">The crypto/elliptic package</h3>
1053
1054<p>
1055In Go 1, <a href="/pkg/crypto/elliptic/#Curve"><code>elliptic.Curve</code></a>
1056has been made an interface to permit alternative implementations. The curve
1057parameters have been moved to the
1058<a href="/pkg/crypto/elliptic/#CurveParams"><code>elliptic.CurveParams</code></a>
1059structure.
1060</p>
1061
1062<p>
1063<em>Updating</em>:
1064Existing users of <code>*elliptic.Curve</code> will need to change to
1065simply <code>elliptic.Curve</code>. Calls to <code>Marshal</code>,
1066<code>Unmarshal</code> and <code>GenerateKey</code> are now functions
1067in <code>crypto/elliptic</code> that take an <code>elliptic.Curve</code>
1068as their first argument.
1069</p>
1070
Rob Pike27836912012-02-04 07:49:51 +11001071<h3 id="crypto_hmac">The crypto/hmac package</h3>
Adam Langley68aff952012-01-27 10:12:27 -08001072
1073<p>
1074In Go 1, the hash-specific functions, such as <code>hmac.NewMD5</code>, have
1075been removed from <code>crypto/hmac</code>. Instead, <code>hmac.New</code> takes
1076a function that returns a <code>hash.Hash</code>, such as <code>md5.New</code>.
1077</p>
1078
1079<p>
1080<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001081Running <code>go</code> <code>fix</code> will perform the needed changes.
Adam Langley68aff952012-01-27 10:12:27 -08001082</p>
1083
Rob Pike0a1376a2012-01-20 14:28:48 -08001084<h3 id="crypto_x509">The crypto/x509 package</h3>
1085
1086<p>
1087In Go 1, the
1088<a href="/pkg/crypto/x509/#CreateCertificate"><code>CreateCertificate</code></a>
1089and
1090<a href="/pkg/crypto/x509/#CreateCRL"><code>CreateCRL</code></a>
1091functions in <code>crypto/x509</code> have been altered to take an
1092<code>interface{}</code> where they previously took a <code>*rsa.PublicKey</code>
1093or <code>*rsa.PrivateKey</code>. This will allow other public key algorithms
1094to be implemented in the future.
1095</p>
1096
1097<p>
1098<em>Updating</em>:
1099No changes will be needed.
1100</p>
1101
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001102<h3 id="encoding_binary">The encoding/binary package</h3>
1103
1104<p>
1105In Go 1, the <code>binary.TotalSize</code> function has been replaced by
1106<a href="/pkg/encoding/binary/#Size"><code>Size</code></a>,
1107which takes an <code>interface{}</code> argument rather than
1108a <code>reflect.Value</code>.
1109</p>
1110
1111<p>
1112<em>Updating</em>:
1113What little code is affected will be caught by the compiler and must be updated by hand.
1114</p>
1115
1116<h3 id="encoding_xml">The encoding/xml package</h3>
1117
1118<p>
1119In Go 1, the <a href="/pkg/encoding/xml/"><code>xml</code></a> package
1120has been brought closer in design to the other marshaling packages such
1121as <a href="/pkg/encoding/gob/"><code>encoding/gob</code></a>.
1122</p>
1123
1124<p>
1125The old <code>Parser</code> type is renamed
1126<a href="/pkg/encoding/xml/#Decoder"><code>Decoder</code></a> and has a new
1127<a href="/pkg/encoding/xml/#Decoder.Decode"><code>Decode</code></a> method. An
Rob Pike68c7e8a2012-02-27 07:31:34 +11001128<a href="/pkg/encoding/xml/#Encoder"><code>Encoder</code></a> type was also introduced.
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001129</p>
1130
1131<p>
1132The functions <a href="/pkg/encoding/xml/#Marshal"><code>Marshal</code></a>
1133and <a href="/pkg/encoding/xml/#Unmarshal"><code>Unmarshal</code></a>
1134work with <code>[]byte</code> values now. To work with streams,
1135use the new <a href="/pkg/encoding/xml/#Encoder"><code>Encoder</code></a>
1136and <a href="/pkg/encoding/xml/#Decoder"><code>Decoder</code></a> types.
1137</p>
1138
1139<p>
1140When marshaling or unmarshaling values, the format of supported flags in
1141field tags has changed to be closer to the
1142<a href="/pkg/encoding/json"><code>json</code></a> package
1143(<code>`xml:"name,flag"`</code>). The matching done between field tags, field
1144names, and the XML attribute and element names is now case-sensitive.
1145The <code>XMLName</code> field tag, if present, must also match the name
1146of the XML element being marshaled.
1147</p>
1148
1149<p>
1150<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001151Running <code>go</code> <code>fix</code> will update most uses of the package except for some calls to
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001152<code>Unmarshal</code>. Special care must be taken with field tags,
1153since the fix tool will not update them and if not fixed by hand they will
1154misbehave silently in some cases. For example, the old
1155<code>"attr"</code> is now written <code>",attr"</code> while plain
1156<code>"attr"</code> remains valid but with a different meaning.
1157</p>
1158
David Symonds715588f2012-02-04 14:32:05 +11001159<h3 id="expvar">The expvar package</h3>
1160
1161<p>
1162In Go 1, the <code>RemoveAll</code> function has been removed.
1163The <code>Iter</code> function and Iter method on <code>*Map</code> have
1164been replaced by
1165<a href="/pkg/expvar/#Do"><code>Do</code></a>
1166and
1167<a href="/pkg/expvar/#Map.Do"><code>(*Map).Do</code></a>.
1168</p>
1169
1170<p>
1171<em>Updating</em>:
1172Most code using <code>expvar</code> will not need changing. The rare code that used
David Symonds2943ca6b2012-02-04 21:55:38 +11001173<code>Iter</code> can be updated to pass a closure to <code>Do</code> to achieve the same effect.
David Symonds715588f2012-02-04 14:32:05 +11001174</p>
1175
Rob Pike531ded92012-01-20 15:38:03 -08001176<h3 id="flag">The flag package</h3>
1177
1178<p>
1179In Go 1, the interface <a href="/pkg/flag/#Value"><code>flag.Value</code></a> has changed slightly.
1180The <code>Set</code> method now returns an <code>error</code> instead of
1181a <code>bool</code> to indicate success or failure.
1182</p>
1183
1184<p>
1185There is also a new kind of flag, <code>Duration</code>, to support argument
1186values specifying time intervals.
1187Values for such flags must be given units, just as <code>time.Duration</code>
1188formats them: <code>10s</code>, <code>1h30m</code>, etc.
1189</p>
1190
1191<pre><!--{{code "progs/go1.go" `/timeout/`}}
1192-->var timeout = flag.Duration(&#34;timeout&#34;, 30*time.Second, &#34;how long to wait for completion&#34;)</pre>
1193
1194<p>
1195<em>Updating</em>:
1196Programs that implement their own flags will need minor manual fixes to update their
1197<code>Set</code> methods.
1198The <code>Duration</code> flag is new and affects no existing code.
1199</p>
1200
1201
Rob Pike0a1376a2012-01-20 14:28:48 -08001202<h3 id="go">The go/* packages</h3>
1203
1204<p>
1205Several packages under <code>go</code> have slightly revised APIs.
1206</p>
1207
1208<p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001209A concrete <code>Mode</code> type was introduced for configuration mode flags
1210in the packages
1211<a href="/pkg/go/scanner/"><code>go/scanner</code></a>,
1212<a href="/pkg/go/parser/"><code>go/parser</code></a>,
1213<a href="/pkg/go/printer/"><code>go/printer</code></a>, and
1214<a href="/pkg/go/doc/"><code>go/doc</code></a>.
1215</p>
1216
1217<p>
Rob Pike0a1376a2012-01-20 14:28:48 -08001218The modes <code>AllowIllegalChars</code> and <code>InsertSemis</code> have been removed
1219from the <a href="/pkg/go/scanner/"><code>go/scanner</code></a> package. They were mostly
1220useful for scanning text other then Go source files. Instead, the
1221<a href="/pkg/text/scanner/"><code>text/scanner</code></a> package should be used
1222for that purpose.
1223</p>
1224
1225<p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001226The <a href="/pkg/go/scanner/#ErrorHandler"><code>ErrorHandler</code></a> provided
1227to the scanner's <a href="/pkg/go/scanner/#Scanner.Init"><code>Init</code></a> method is
1228now simply a function rather than an interface. The <code>ErrorVector</code> type has
1229been removed in favor of the (existing) <a href="/pkg/go/scanner/#ErrorList"><code>ErrorList</code></a>
1230type, and the <code>ErrorVector</code> methods have been migrated. Instead of embedding
1231an <code>ErrorVector</code> in a client of the scanner, now a client should maintain
1232an <code>ErrorList</code>.
1233</p>
1234
1235<p>
Rob Pike0a1376a2012-01-20 14:28:48 -08001236The set of parse functions provided by the <a href="/pkg/go/parser/"><code>go/parser</code></a>
1237package has been reduced to the primary parse function
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -02001238<a href="/pkg/go/parser/#ParseFile"><code>ParseFile</code></a>, and a couple of
1239convenience functions <a href="/pkg/go/parser/#ParseDir"><code>ParseDir</code></a>
1240and <a href="/pkg/go/parser/#ParseExpr"><code>ParseExpr</code></a>.
Rob Pike0a1376a2012-01-20 14:28:48 -08001241</p>
1242
1243<p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001244The <a href="/pkg/go/printer/"><code>go/printer</code></a> package supports an additional
1245configuration mode <a href="/pkg/go/printer/#Mode"><code>SourcePos</code></a>;
1246if set, the printer will emit <code>//line</code> comments such that the generated
1247output contains the original source code position information. The new type
1248<a href="/pkg/go/printer/#CommentedNode"><code>CommentedNode</code></a> can be
1249used to provide comments associated with an arbitrary
1250<a href="/pkg/go/ast/#Node"><code>ast.Node</code></a> (until now only
1251<a href="/pkg/go/ast/#File"><code>ast.File</code></a> carried comment information).
1252</p>
1253
1254<p>
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -02001255The type names of the <a href="/pkg/go/doc/"><code>go/doc</code></a> package have been
Rob Pike0a1376a2012-01-20 14:28:48 -08001256streamlined by removing the <code>Doc</code> suffix: <code>PackageDoc</code>
1257is now <code>Package</code>, <code>ValueDoc</code> is <code>Value</code>, etc.
1258Also, all types now consistently have a <code>Name</code> field (or <code>Names</code>,
Robert Griesemerd571c5c2012-01-25 16:48:06 -08001259in the case of type <code>Value</code>) and <code>Type.Factories</code> has become
1260<code>Type.Funcs</code>.
Rob Pike0a1376a2012-01-20 14:28:48 -08001261Instead of calling <code>doc.NewPackageDoc(pkg, importpath)</code>,
1262documentation for a package is created with:
1263</p>
1264
1265<pre>
1266 doc.New(pkg, importpath, mode)
1267</pre>
1268
1269<p>
1270where the new <code>mode</code> parameter specifies the operation mode:
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -02001271if set to <a href="/pkg/go/doc/#AllDecls"><code>AllDecls</code></a>, all declarations
Rob Pike0a1376a2012-01-20 14:28:48 -08001272(not just exported ones) are considered.
1273The function <code>NewFileDoc</code> was removed, and the function
1274<code>CommentText</code> has become the method
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -02001275<a href="/pkg/go/ast/#Text"><code>Text</code></a> of
1276<a href="/pkg/go/ast/#CommentGroup"><code>ast.CommentGroup</code></a>.
Rob Pike0a1376a2012-01-20 14:28:48 -08001277</p>
1278
1279<p>
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -02001280In package <a href="/pkg/go/token/"><code>go/token</code></a>, the
1281<a href="/pkg/go/token/#FileSet"><code>token.FileSet</code></a> method <code>Files</code>
Rob Pike0a1376a2012-01-20 14:28:48 -08001282(which originally returned a channel of <code>*token.File</code>s) has been replaced
Gustavo Niemeyer75e9d242012-01-25 23:42:36 -02001283with the iterator <a href="/pkg/go/token/#FileSet.Iterate"><code>Iterate</code></a> that
Rob Pike0a1376a2012-01-20 14:28:48 -08001284accepts a function argument instead.
1285</p>
1286
1287<p>
1288<em>Updating</em>:
1289Code that uses packages in <code>go</code> will have to be updated by hand; the
Rob Pike68c7e8a2012-02-27 07:31:34 +11001290compiler will reject incorrect uses. Templates used in conjunction with any of the
Rob Pike0a1376a2012-01-20 14:28:48 -08001291<code>go/doc</code> types may need manual fixes; the renamed fields will lead
1292to run-time errors.
1293</p>
1294
Russ Cox1f1c9ba2012-01-18 10:36:43 -05001295<h3 id="hash">The hash package</h3>
1296
1297<p>
1298In Go 1, the definition of <a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> includes
1299a new method, <code>BlockSize</code>. This new method is used primarily in the
1300cryptographic libraries.
1301</p>
1302
1303<p>
Rob Pike03ea8b12012-01-24 16:36:40 -08001304The <code>Sum</code> method of the
1305<a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> interface now takes a
1306<code>[]byte</code> argument, to which the hash value will be appended.
1307The previous behavior can be recreated by adding a <code>nil</code> argument to the call.
1308</p>
1309
1310<p>
Russ Cox1f1c9ba2012-01-18 10:36:43 -05001311<em>Updating</em>:
1312Existing implementations of <code>hash.Hash</code> will need to add a
1313<code>BlockSize</code> method. Hashes that process the input one byte at
1314a time can implement <code>BlockSize</code> to return 1.
Rob Pike5cff0292012-02-24 13:08:11 +11001315Running <code>go</code> <code>fix</code> will update calls to the <code>Sum</code> methods of the various
Rob Pike03ea8b12012-01-24 16:36:40 -08001316implementations of <code>hash.Hash</code>.
Rob Pikef76bd4f2011-12-12 19:25:25 -08001317</p>
1318
1319<p>
1320<em>Updating</em>:
1321Since the package's functionality is new, no updating is necessary.
1322</p>
1323
Rob Pikebab4dec2011-12-07 14:33:37 -08001324<h3 id="http">The http package</h3>
1325
Rob Pikef76bd4f2011-12-12 19:25:25 -08001326<p>
Jongmin Kim343098e2012-01-17 09:47:34 -08001327In Go 1 the <a href="/pkg/net/http/"><code>http</code></a> package is refactored,
Rob Pikef76bd4f2011-12-12 19:25:25 -08001328putting some of the utilities into a
Jongmin Kim343098e2012-01-17 09:47:34 -08001329<a href="/pkg/net/httputil/"><code>httputil</code></a> subdirectory.
Rob Pikef76bd4f2011-12-12 19:25:25 -08001330These pieces are only rarely needed by HTTP clients.
1331The affected items are:
1332</p>
1333
1334<ul>
1335<li>ClientConn</li>
1336<li>DumpRequest</li>
1337<li>DumpRequest</li>
1338<li>DumpRequestOut</li>
1339<li>DumpResponse</li>
1340<li>NewChunkedReader</li>
1341<li>NewChunkedWriter</li>
1342<li>NewClientConn</li>
1343<li>NewProxyClientConn</li>
1344<li>NewServerConn</li>
1345<li>NewSingleHostReverseProxy</li>
1346<li>ReverseProxy</li>
1347<li>ServerConn</li>
1348</ul>
1349
1350<p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001351The <code>Request.RawURL</code> field has been removed; it was a
Rob Pikef76bd4f2011-12-12 19:25:25 -08001352historical artifact.
1353</p>
1354
1355<p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001356The <code>Handle</code> and <code>HandleFunc</code>
1357functions, and the similarly-named methods of <code>ServeMux</code>,
1358now panic if an attempt is made to register the same pattern twice.
1359</p>
1360
1361<p>
Rob Pikef76bd4f2011-12-12 19:25:25 -08001362<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001363Running <code>go</code> <code>fix</code> will update the few programs that are affected except for
Rob Pikef76bd4f2011-12-12 19:25:25 -08001364uses of <code>RawURL</code>, which must be fixed by hand.
1365</p>
1366
Rob Pike2257e762012-01-23 16:11:49 -08001367<h3 id="image">The image package</h3>
1368
1369<p>
1370The <a href="/pkg/image/"><code>image</code></a> package has had a number of
1371minor changes, rearrangements and renamings.
1372</p>
1373
1374<p>
1375Most of the color handling code has been moved into its own package,
1376<a href="/pkg/image/color/"><code>image/color</code></a>.
1377For the elements that moved, a symmetry arises; for instance,
1378each pixel of an
1379<a href="/pkg/image/#RGBA"><code>image.RGBA</code></a>
1380is a
1381<a href="/pkg/image/color/#RGBA"><code>color.RGBA</code></a>.
1382</p>
1383
1384<p>
1385The old <code>image/ycbcr</code> package has been folded, with some
1386renamings, into the
1387<a href="/pkg/image/"><code>image</code></a>
1388and
1389<a href="/pkg/image/color/"><code>image/color</code></a>
1390packages.
1391</p>
1392
1393<p>
1394The old <code>image.ColorImage</code> type is still in the <code>image</code>
1395package but has been renamed
1396<a href="/pkg/image/#Uniform"><code>image.Uniform</code></a>,
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001397while <code>image.Tiled</code> has been removed.
Rob Pike2257e762012-01-23 16:11:49 -08001398</p>
1399
1400<p>
1401This table lists the renamings.
1402</p>
1403
1404<table class="codetable" frame="border" summary="image renames">
1405<colgroup align="left" width="50%"></colgroup>
1406<colgroup align="left" width="50%"></colgroup>
1407<tr>
1408<th align="left">Old</th>
1409<th align="left">New</th>
1410</tr>
1411<tr>
1412<td colspan="2"><hr></td>
1413</tr>
1414<tr><td>image.Color</td> <td>color.Color</td></tr>
1415<tr><td>image.ColorModel</td> <td>color.Model</td></tr>
1416<tr><td>image.ColorModelFunc</td> <td>color.ModelFunc</td></tr>
1417<tr><td>image.PalettedColorModel</td> <td>color.Palette</td></tr>
1418<tr>
1419<td colspan="2"><hr></td>
1420</tr>
1421<tr><td>image.RGBAColor</td> <td>color.RGBA</td></tr>
1422<tr><td>image.RGBA64Color</td> <td>color.RGBA64</td></tr>
1423<tr><td>image.NRGBAColor</td> <td>color.NRGBA</td></tr>
1424<tr><td>image.NRGBA64Color</td> <td>color.NRGBA64</td></tr>
1425<tr><td>image.AlphaColor</td> <td>color.Alpha</td></tr>
1426<tr><td>image.Alpha16Color</td> <td>color.Alpha16</td></tr>
1427<tr><td>image.GrayColor</td> <td>color.Gray</td></tr>
1428<tr><td>image.Gray16Color</td> <td>color.Gray16</td></tr>
1429<tr>
1430<td colspan="2"><hr></td>
1431</tr>
1432<tr><td>image.RGBAColorModel</td> <td>color.RGBAModel</td></tr>
1433<tr><td>image.RGBA64ColorModel</td> <td>color.RGBA64Model</td></tr>
1434<tr><td>image.NRGBAColorModel</td> <td>color.NRGBAModel</td></tr>
1435<tr><td>image.NRGBA64ColorModel</td> <td>color.NRGBA64Model</td></tr>
1436<tr><td>image.AlphaColorModel</td> <td>color.AlphaModel</td></tr>
1437<tr><td>image.Alpha16ColorModel</td> <td>color.Alpha16Model</td></tr>
1438<tr><td>image.GrayColorModel</td> <td>color.GrayModel</td></tr>
1439<tr><td>image.Gray16ColorModel</td> <td>color.Gray16Model</td></tr>
1440<tr>
1441<td colspan="2"><hr></td>
1442</tr>
1443<tr><td>ycbcr.RGBToYCbCr</td> <td>color.RGBToYCbCr</td></tr>
1444<tr><td>ycbcr.YCbCrToRGB</td> <td>color.YCbCrToRGB</td></tr>
1445<tr><td>ycbcr.YCbCrColorModel</td> <td>color.YCbCrModel</td></tr>
1446<tr><td>ycbcr.YCbCrColor</td> <td>color.YCbCr</td></tr>
1447<tr><td>ycbcr.YCbCr</td> <td>image.YCbCr</td></tr>
1448<tr>
1449<td colspan="2"><hr></td>
1450</tr>
1451<tr><td>ycbcr.SubsampleRatio444</td> <td>image.YCbCrSubsampleRatio444</td></tr>
1452<tr><td>ycbcr.SubsampleRatio422</td> <td>image.YCbCrSubsampleRatio422</td></tr>
1453<tr><td>ycbcr.SubsampleRatio420</td> <td>image.YCbCrSubsampleRatio420</td></tr>
1454<tr>
1455<td colspan="2"><hr></td>
1456</tr>
1457<tr><td>image.ColorImage</td> <td>image.Uniform</td></tr>
Rob Pike2257e762012-01-23 16:11:49 -08001458</table>
1459
1460<p>
1461The image package's <code>New</code> functions
1462(<a href="/pkg/image/#NewRGBA"><code>NewRGBA</code></a>,
1463<a href="/pkg/image/#NewRGBA64"><code>NewRGBA64</code></a>, etc.)
1464take an <a href="/pkg/image/#Rectangle"><code>image.Rectangle</code></a> as an argument
1465instead of four integers.
1466</p>
1467
1468<p>
1469Finally, there are new predefined <code>color.Color</code> variables
1470<a href="/pkg/image/color/#Black"><code>color.Black</code></a>,
1471<a href="/pkg/image/color/#White"><code>color.White</code></a>,
1472<a href="/pkg/image/color/#Opaque"><code>color.Opaque</code></a>
1473and
1474<a href="/pkg/image/color/#Transparent"><code>color.Transparent</code></a>.
1475</p>
1476
1477<p>
1478<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001479Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
Rob Pike2257e762012-01-23 16:11:49 -08001480</p>
1481
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001482<h3 id="log_syslog">The log/syslog package</h3>
1483
1484<p>
1485In Go 1, the <a href="/pkg/log/syslog/#NewLogger"><code>syslog.NewLogger</code></a>
1486function returns an error as well as a <code>log.Logger</code>.
1487</p>
1488
1489<p>
1490<em>Updating</em>:
1491What little code is affected will be caught by the compiler and must be updated by hand.
1492</p>
1493
Rob Pikedd442a52012-01-24 17:02:06 -08001494<h3 id="mime">The mime package</h3>
1495
1496<p>
1497In Go 1, the <a href="/pkg/mime/#FormatMediaType"><code>FormatMediaType</code></a> function
1498of the <code>mime</code> package has been simplified to make it
1499consistent with
1500<a href="/pkg/mime/#ParseMediaType"><code>ParseMediaType</code></a>.
1501It now takes <code>"text/html"</code> rather than <code>"text"</code> and <code>"html"</code>.
1502</p>
1503
1504<p>
1505<em>Updating</em>:
1506What little code is affected will be caught by the compiler and must be updated by hand.
1507</p>
1508
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001509<h3 id="net">The net package</h3>
1510
Rob Pike03ea8b12012-01-24 16:36:40 -08001511<p>
1512In Go 1, the various <code>SetTimeout</code>,
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001513<code>SetReadTimeout</code>, and <code>SetWriteTimeout</code> methods
Rob Pike03ea8b12012-01-24 16:36:40 -08001514have been replaced with
1515<a href="/pkg/net/#IPConn.SetDeadline"><code>SetDeadline</code></a>,
1516<a href="/pkg/net/#IPConn.SetReadDeadline"><code>SetReadDeadline</code></a>, and
1517<a href="/pkg/net/#IPConn.SetWriteDeadline"><code>SetWriteDeadline</code></a>,
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001518respectively. Rather than taking a timeout value in nanoseconds that
1519apply to any activity on the connection, the new methods set an
1520absolute deadline (as a <code>time.Time</code> value) after which
Rob Pike03ea8b12012-01-24 16:36:40 -08001521reads and writes will time out and no longer block.
1522</p>
1523
1524<p>
Mikio Hara2f63afd2012-02-01 01:53:26 +09001525There are also new functions
1526<a href="/pkg/net/#DialTimeout"><code>net.DialTimeout</code></a>
1527to simplify timing out dialing a network address and
1528<a href="/pkg/net/#ListenMulticastUDP"><code>net.ListenMulticastUDP</code></a>
1529to allow multicast UDP to listen concurrently across multiple listeners.
1530The <code>net.ListenMulticastUDP</code> function replaces the old
1531<code>JoinGroup</code> and <code>LeaveGroup</code> methods.
Rob Pike03ea8b12012-01-24 16:36:40 -08001532</p>
1533
1534<p>
1535<em>Updating</em>:
1536Code that uses the old methods will fail to compile and must be updated by hand.
Rob Pike27836912012-02-04 07:49:51 +11001537The semantic change makes it difficult for the fix tool to update automatically.
Rob Pike03ea8b12012-01-24 16:36:40 -08001538</p>
Brad Fitzpatrickb71883e2012-01-18 16:24:06 -08001539
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001540<h3 id="os">The os package</h3>
1541
Rob Pikec3ef1982012-02-19 14:15:26 +11001542<p>
1543The <code>Time</code> function has been removed; callers should use
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001544the <a href="/pkg/time/#Time"><code>Time</code></a> type from the
Rob Pikec3ef1982012-02-19 14:15:26 +11001545<code>time</code> package.
1546</p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001547
Rob Pikec3ef1982012-02-19 14:15:26 +11001548<p>
1549The <code>Exec</code> function has been removed; callers should use
1550<code>Exec</code> from the <code>syscall</code> package, where available.
1551</p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001552
Rob Pikec3ef1982012-02-19 14:15:26 +11001553<p>
1554The <code>ShellExpand</code> function has been renamed to <a
1555href="/pkg/os/#ExpandEnv"><code>ExpandEnv</code></a>.
1556</p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001557
Rob Pikec3ef1982012-02-19 14:15:26 +11001558<p>
1559The <a href="/pkg/os/#NewFile"><code>NewFile</code></a> function
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001560now takes a <code>uintptr</code> fd, instead of an <code>int</code>.
1561The <a href="/pkg/os/#File.Fd"><code>Fd</code></a> method on files now
Rob Pikec3ef1982012-02-19 14:15:26 +11001562also returns a <code>uintptr</code>.
1563</p>
1564
1565<p>
1566There are no longer error constants such as <code>EINVAL</code>
1567in the <code>os</code> package, since the set of values varied with
1568the underlying operating system. There are new portable functions like
1569<a href="/pkg/os/#IsPermission"><code>IsPermission</code></a>
1570to test common error properties, plus a few new error values
1571with more Go-like names, such as
1572<a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
1573and
1574<a href="/pkg/os/#ErrNoEnv"><code>ErrNoEnv</code></a>.
Brad Fitzpatrickefacb2a2012-02-18 21:18:13 -08001575</p>
Rob Pikec3ef1982012-02-19 14:15:26 +11001576
Brad Fitzpatrickefacb2a2012-02-18 21:18:13 -08001577<p>
1578The <code>Getenverror</code> function has been removed. To distinguish
1579between a non-existent environment variable and an empty string,
1580use <a href="/pkg/os/#Environ"><code>os.Environ</code></a> or
1581<a href="/pkg/syscall/#Getenv"><code>syscall.Getenv</code></a>.
1582</p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001583
Rob Pikeb5a3bd52012-02-20 15:36:08 +11001584
1585<p>
1586The <a href="/pkg/os/#Process.Wait"><code>Process.Wait</code></a> method has
1587dropped its option argument and the associated constants are gone
1588from the package.
1589Also, the function <code>Wait</code> is gone; only the method of
1590the <code>Process</code> type persists.
1591</p>
1592
Nigel Taoda8f0372012-02-15 14:41:47 +11001593<p>
Rob Pike5cff0292012-02-24 13:08:11 +11001594The <code>Waitmsg</code> type returned by
1595<a href="/pkg/os/#Process.Wait"><code>Process.Wait</code></a>
1596has been replaced with a more portable
1597<a href="/pkg/os/#ProcessState"><code>ProcessState</code></a>
1598type with accessor methods to recover information about the
1599process.
1600Because of changes to <code>Wait</code>, the <code>ProcessState</code>
1601value always describes an exited process.
1602Portability concerns simplified the interface in other ways, but the values returned by the
1603<a href="/pkg/os/#ProcessState.Sys"><code>ProcessState.Sys</code></a> and
1604<a href="/pkg/os/#ProcessState.SysUsage"><code>ProcessState.SysUsage</code></a>
1605methods can be type-asserted to underlying system-specific data structures such as
1606<a href="/pkg/syscall/#WaitStatus"><code>syscall.WaitStatus</code></a> and
1607<a href="/pkg/syscall/#Rusage"><code>syscall.Rusage</code></a> on Unix.
1608</p>
1609
1610<p>
Nigel Taoda8f0372012-02-15 14:41:47 +11001611<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001612Running <code>go</code> <code>fix</code> will drop a zero argument to <code>Process.Wait</code>.
1613All other changes will be caught by the compiler and must be updated by hand.
Nigel Taoda8f0372012-02-15 14:41:47 +11001614</p>
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001615
1616<h4 id="os_fileinfo">The os.FileInfo type</h4>
Rob Pike0a1376a2012-01-20 14:28:48 -08001617
1618<p>
1619Go 1 redefines the <a href="/pkg/os/#FileInfo"><code>os.FileInfo</code></a> type,
1620changing it from a struct to an interface:
1621</p>
1622
1623<pre>
1624 type FileInfo interface {
1625 Name() string // base name of the file
1626 Size() int64 // length in bytes
1627 Mode() FileMode // file mode bits
1628 ModTime() time.Time // modification time
1629 IsDir() bool // abbreviation for Mode().IsDir()
Rob Pike46dc76f2012-02-12 09:17:57 +11001630 Sys() interface{} // underlying data source (can return nil)
Rob Pike0a1376a2012-01-20 14:28:48 -08001631 }
1632</pre>
1633
1634<p>
1635The file mode information has been moved into a subtype called
1636<a href="/pkg/os/#FileMode"><code>os.FileMode</code></a>,
1637a simple integer type with <code>IsDir</code>, <code>Perm</code>, and <code>String</code>
1638methods.
1639</p>
1640
1641<p>
1642The system-specific details of file modes and properties such as (on Unix)
1643i-number have been removed from <code>FileInfo</code> altogether.
1644Instead, each operating system's <code>os</code> package provides an
Rob Pike6ba77ec2012-02-21 08:03:03 +11001645implementation of the <code>FileInfo</code> interface, which
1646has a <code>Sys</code> method that returns the
Rob Pike0a1376a2012-01-20 14:28:48 -08001647system-specific representation of file metadata.
1648For instance, to discover the i-number of a file on a Unix system, unpack
1649the <code>FileInfo</code> like this:
1650</p>
1651
1652<pre>
1653 fi, err := os.Stat("hello.go")
1654 if err != nil {
1655 log.Fatal(err)
1656 }
Rob Pike46dc76f2012-02-12 09:17:57 +11001657 // Check that it's a Unix file.
1658 unixStat, ok := fi.Sys().(*syscall.Stat_t)
Rob Pike0a1376a2012-01-20 14:28:48 -08001659 if !ok {
1660 log.Fatal("hello.go: not a Unix file")
1661 }
1662 fmt.Printf("file i-number: %d\n", unixStat.Ino)
1663</pre>
1664
1665<p>
1666Assuming (which is unwise) that <code>"hello.go"</code> is a Unix file,
1667the i-number expression could be contracted to
1668</p>
1669
1670<pre>
Rob Pikeaee1c382012-02-13 08:05:53 +11001671 fi.Sys().(*syscall.Stat_t).Ino
Rob Pike0a1376a2012-01-20 14:28:48 -08001672</pre>
1673
1674<p>
1675The vast majority of uses of <code>FileInfo</code> need only the methods
1676of the standard interface.
1677</p>
Rob Pike56069f02012-02-17 10:04:29 +11001678
1679<p>
1680The <code>os</code> package no longer contains wrappers for the POSIX errors
1681such as <code>ENOENT</code>.
1682For the few programs that need to verify particular error conditions, there are
1683now the boolean functions
1684<a href="/pkg/os/#IsExist"><code>IsExist</code></a>,
1685<a href="/pkg/os/#IsNotExist"><code>IsNotExist</code></a>
1686and
1687<a href="/pkg/os/#IsPermission"><code>IsPermission</code></a>.
1688</p>
1689
1690<pre><!--{{code "progs/go1.go" `/os\.Open/` `/}/`}}
1691--> f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
1692 if os.IsExist(err) {
1693 log.Printf(&#34;%s already exists&#34;, name)
1694 }</pre>
1695
Rob Pike0a1376a2012-01-20 14:28:48 -08001696<p>
1697<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001698Running <code>go</code> <code>fix</code> will update code that uses the old equivalent of the current <code>os.FileInfo</code>
Rob Pike0a1376a2012-01-20 14:28:48 -08001699and <code>os.FileMode</code> API.
1700Code that needs system-specific file details will need to be updated by hand.
Rob Pike56069f02012-02-17 10:04:29 +11001701Code that uses the old POSIX error values from the <code>os</code> package
1702will fail to compile and will also need to be updated by hand.
Rob Pike0a1376a2012-01-20 14:28:48 -08001703</p>
1704
Rob Pikedd442a52012-01-24 17:02:06 -08001705<h3 id="path_filepath">The path/filepath package</h3>
1706
1707<p>
1708In Go 1, the <a href="/pkg/path/filepath/#Walk"><code>Walk</code></a> function of the
1709<code>path/filepath</code> package
1710has been changed to take a function value of type
1711<a href="/pkg/path/filepath/#WalkFunc"><code>WalkFunc</code></a>
1712instead of a <code>Visitor</code> interface value.
1713<code>WalkFunc</code> unifies the handling of both files and directories.
1714</p>
1715
1716<pre>
Mike Rosset4af3dda2012-02-24 22:17:21 -08001717 type WalkFunc func(path string, info os.FileInfo, err error) error
Rob Pikedd442a52012-01-24 17:02:06 -08001718</pre>
1719
1720<p>
1721The <code>WalkFunc</code> function will be called even for files or directories that could not be opened;
1722in such cases the error argument will describe the failure.
1723If a directory's contents are to be skipped,
Mike Rosset91672682012-02-17 12:45:55 +11001724the function should return the value <a href="/pkg/path/filepath/#variables"><code>filepath.SkipDir</code></a>
Rob Pikedd442a52012-01-24 17:02:06 -08001725</p>
1726
Mike Rosset91672682012-02-17 12:45:55 +11001727<pre><!--{{code "progs/go1.go" `/STARTWALK/` `/ENDWALK/`}}
1728--> markFn := func(path string, info os.FileInfo, err error) error {
1729 if path == &#34;pictures&#34; { // Will skip walking of directory pictures and its contents.
1730 return filepath.SkipDir
1731 }
1732 if err != nil {
1733 return err
1734 }
1735 log.Println(path)
1736 return nil
1737 }
1738 err := filepath.Walk(&#34;.&#34;, markFn)
1739 if err != nil {
1740 log.Fatal(err)
1741 }</pre>
Rob Pikedd442a52012-01-24 17:02:06 -08001742
1743<p>
1744<em>Updating</em>:
1745The change simplifies most code but has subtle consequences, so affected programs
1746will need to be updated by hand.
1747The compiler will catch code using the old interface.
1748</p>
1749
Rob Pikec3ef1982012-02-19 14:15:26 +11001750<h3 id="os_signal">The os/signal package</h3>
Russ Cox35586f72012-02-13 13:52:37 -05001751
1752<p>
1753The <code>os/signal</code> package in Go 1 replaces the
1754<code>Incoming</code> function, which returned a channel
1755that received all incoming signals,
1756with the selective <code>Notify</code> function, which asks
1757for delivery of specific signals on an existing channel.
1758</p>
1759
1760<p>
1761<em>Updating</em>:
1762Code must be updated by hand.
1763A literal translation of
1764</p>
1765<pre>
1766c := signal.Incoming()
1767</pre>
1768<p>
1769is
1770</p>
1771<pre>
1772c := make(chan os.Signal)
1773signal.Notify(c) // ask for all signals
1774</pre>
1775<p>
1776but most code should list the specific signals it wants to handle instead:
1777</p>
1778<pre>
1779c := make(chan os.Signal)
1780signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT)
1781</pre>
1782
Rob Pike531ded92012-01-20 15:38:03 -08001783<h3 id="runtime">The runtime package</h3>
1784
1785<p>
Russ Cox72f5a912012-02-19 18:04:38 -05001786In Go 1, much of the API exported by package
1787<code>runtime</code> has been removed in favor of
1788functionality provided by other packages.
1789Code using the <code>runtime.Type</code> interface
1790or its specific concrete type implementations should
1791now use package <a href="/pkg/reflect/"><code>reflect</code></a>.
1792Code using <code>runtime.Semacquire</code> or <code>runtime.Semrelease</code>
1793should use channels or the abstractions in package <a href="/pkg/sync/"><code>sync</code></a>.
1794The <code>runtime.Alloc</code>, <code>runtime.Free</code>,
1795and <code>runtime.Lookup</code> functions, an unsafe API created for
1796debugging the memory allocator, have no replacement.
1797</p>
1798
1799<p>
1800Before, <code>runtime.MemStats</code> was a global variable holding
1801statistics about memory allocation, and calls to <code>runtime.UpdateMemStats</code>
1802ensured that it was up to date.
1803In Go 1, <code>runtime.MemStats</code> is a struct type, and code should use
1804<a href="/pkg/runtime/#ReadMemStats"><code>runtime.ReadMemStats</code></a>
1805to obtain the current statistics.
1806</p>
1807
1808<p>
1809The package adds a new function,
Rob Pike531ded92012-01-20 15:38:03 -08001810<a href="/pkg/runtime/#NumCPU"><code>runtime.NumCPU</code></a>, that returns the number of CPUs available
1811for parallel execution, as reported by the operating system kernel.
1812Its value can inform the setting of <code>GOMAXPROCS</code>.
Russ Cox72f5a912012-02-19 18:04:38 -05001813The <code>runtime.Cgocalls</code> and <code>runtime.Goroutines</code> functions
1814have been renamed to <code>runtime.NumCgoCall</code> and <code>runtime.NumGoroutine</code>.
Rob Pike531ded92012-01-20 15:38:03 -08001815</p>
1816
1817<p>
1818<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001819Running <code>go</code> <code>fix</code> will update code for the function renamings.
Russ Cox72f5a912012-02-19 18:04:38 -05001820Other code will need to be updated by hand.
Rob Pike531ded92012-01-20 15:38:03 -08001821</p>
1822
Rob Pikebab4dec2011-12-07 14:33:37 -08001823<h3 id="strconv">The strconv package</h3>
1824
Rob Pike71ccf732011-12-09 14:12:51 -08001825<p>
1826In Go 1, the
Rob Pikeebdcbf12011-12-12 12:26:56 -08001827<a href="/pkg/strconv/"><code>strconv</code></a>
Rob Pike71ccf732011-12-09 14:12:51 -08001828package has been significantly reworked to make it more Go-like and less C-like,
1829although <code>Atoi</code> lives on (it's similar to
1830<code>int(ParseInt(x, 10, 0))</code>, as does
1831<code>Itoa(x)</code> (<code>FormatInt(int64(x), 10)</code>).
1832There are also new variants of some of the functions that append to byte slices rather than
1833return strings, to allow control over allocation.
1834</p>
1835
1836<p>
1837This table summarizes the renamings; see the
Rob Pikeebdcbf12011-12-12 12:26:56 -08001838<a href="/pkg/strconv/">package documentation</a>
Rob Pike71ccf732011-12-09 14:12:51 -08001839for full details.
1840</p>
1841
1842<table class="codetable" frame="border" summary="strconv renames">
1843<colgroup align="left" width="50%"></colgroup>
1844<colgroup align="left" width="50%"></colgroup>
1845<tr>
1846<th align="left">Old call</th>
1847<th align="left">New call</th>
1848</tr>
1849<tr>
1850<td colspan="2"><hr></td>
1851</tr>
1852<tr><td>Atob(x)</td> <td>ParseBool(x)</td></tr>
1853<tr>
1854<td colspan="2"><hr></td>
1855</tr>
1856<tr><td>Atof32(x)</td> <td>ParseFloat(x, 32)ยง</td></tr>
1857<tr><td>Atof64(x)</td> <td>ParseFloat(x, 64)</td></tr>
1858<tr><td>AtofN(x, n)</td> <td>ParseFloat(x, n)</td></tr>
1859<tr>
1860<td colspan="2"><hr></td>
1861</tr>
1862<tr><td>Atoi(x)</td> <td>Atoi(x)</td></tr>
1863<tr><td>Atoi(x)</td> <td>ParseInt(x, 10, 0)ยง</td></tr>
1864<tr><td>Atoi64(x)</td> <td>ParseInt(x, 10, 64)</td></tr>
1865<tr>
1866<td colspan="2"><hr></td>
1867</tr>
1868<tr><td>Atoui(x)</td> <td>ParseUint(x, 10, 0)ยง</td></tr>
1869<tr><td>Atoi64(x)</td> <td>ParseInt(x, 10, 64)</td></tr>
1870<tr>
1871<td colspan="2"><hr></td>
1872</tr>
1873<tr><td>Btoi64(x, b)</td> <td>ParseInt(x, b, 64)</td></tr>
1874<tr><td>Btoui64(x, b)</td> <td>ParseUint(x, b, 64)</td></tr>
1875<tr>
1876<td colspan="2"><hr></td>
1877</tr>
1878<tr><td>Btoa(x)</td> <td>FormatBool(x)</td></tr>
1879<tr>
1880<td colspan="2"><hr></td>
1881</tr>
Marcel van Lohuizen72fb81e2012-02-19 19:26:05 +01001882<tr><td>Ftoa32(x, f, p)</td> <td>FormatFloat(float64(x), f, p, 32)</td></tr>
Rob Pike71ccf732011-12-09 14:12:51 -08001883<tr><td>Ftoa64(x, f, p)</td> <td>FormatFloat(x, f, p, 64)</td></tr>
1884<tr><td>FtoaN(x, f, p, n)</td> <td>FormatFloat(x, f, p, n)</td></tr>
1885<tr>
1886<td colspan="2"><hr></td>
1887</tr>
1888<tr><td>Itoa(x)</td> <td>Itoa(x)</td></tr>
1889<tr><td>Itoa(x)</td> <td>FormatInt(int64(x), 10)</td></tr>
1890<tr><td>Itoa64(x)</td> <td>FormatInt(x, 10)</td></tr>
1891<tr>
1892<td colspan="2"><hr></td>
1893</tr>
1894<tr><td>Itob(x, b)</td> <td>FormatInt(int64(x), b)</td></tr>
1895<tr><td>Itob64(x, b)</td> <td>FormatInt(x, b)</td></tr>
1896<tr>
1897<td colspan="2"><hr></td>
1898</tr>
1899<tr><td>Uitoa(x)</td> <td>FormatUint(uint64(x), 10)</td></tr>
1900<tr><td>Uitoa64(x)</td> <td>FormatUint(x, 10)</td></tr>
1901<tr>
1902<td colspan="2"><hr></td>
1903</tr>
1904<tr><td>Uitob(x, b)</td> <td>FormatUint(uint64(x), b)</td></tr>
1905<tr><td>Uitob64(x, b)</td> <td>FormatUint(x, b)</td></tr>
1906</table>
1907
1908<p>
1909<em>Updating</em>:
Rob Pike5cff0292012-02-24 13:08:11 +11001910Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
Rob Pike71ccf732011-12-09 14:12:51 -08001911<br>
1912ยง <code>Atoi</code> persists but <code>Atoui</code> and <code>Atof32</code> do not, so
1913they may require
Rob Pike5cff0292012-02-24 13:08:11 +11001914a cast that must be added by hand; the <code>go</code> <code>fix</code> tool will warn about it.
Rob Pike71ccf732011-12-09 14:12:51 -08001915</p>
1916
Rob Pikebab4dec2011-12-07 14:33:37 -08001917
Rob Pike531ded92012-01-20 15:38:03 -08001918<h3 id="testing">The testing package</h3>
1919
1920<p>
1921The testing package has a type, <code>B</code>, passed as an argument to benchmark functions.
1922In Go 1, <code>B</code> has new methods, analogous to those of <code>T</code>, enabling
1923logging and failure reporting.
1924</p>
1925
1926<pre><!--{{code "progs/go1.go" `/func.*Benchmark/` `/^}/`}}
1927-->func BenchmarkSprintf(b *testing.B) {
1928 // Verify correctness before running benchmark.
1929 b.StopTimer()
1930 got := fmt.Sprintf(&#34;%x&#34;, 23)
1931 const expect = &#34;17&#34;
1932 if expect != got {
1933 b.Fatalf(&#34;expected %q; got %q&#34;, expect, got)
1934 }
1935 b.StartTimer()
1936 for i := 0; i &lt; b.N; i++ {
1937 fmt.Sprintf(&#34;%x&#34;, 23)
1938 }
1939}</pre>
1940
1941<p>
1942<em>Updating</em>:
1943Existing code is unaffected, although benchmarks that use <code>println</code>
Rob Pike2257e762012-01-23 16:11:49 -08001944or <code>panic</code> should be updated to use the new methods.
Rob Pike531ded92012-01-20 15:38:03 -08001945</p>
1946
Adam Langleyc2e58dc2012-02-14 07:13:57 -05001947<h3 id="testing_script">The testing/script package</h3>
1948
1949<p>
1950The testing/script package has been deleted. It was a dreg.
1951</p>
1952
1953<p>
1954<em>Updating</em>:
1955No code is likely to be affected.
1956</p>
1957
Russ Cox72f5a912012-02-19 18:04:38 -05001958<h3 id="unsafe">The unsafe package</h3>
1959
1960<p>
1961In Go 1, the functions
1962<code>unsafe.Typeof</code>, <code>unsafe.Reflect</code>,
1963<code>unsafe.Unreflect</code>, <code>unsafe.New</code>, and
1964<code>unsafe.NewArray</code> have been removed;
1965they duplicated safer functionality provided by
1966package <a href="/pkg/reflect/"><code>reflect</code></a>.
1967</p>
1968
1969<p>
1970<em>Updating</em>:
1971Code using these functions must be rewritten to use
1972package <a href="/pkg/reflect/"><code>reflect</code></a>.
1973The changes to <a href="http://code.google.com/p/go/source/detail?r=2646dc956207">encoding/gob</a> and the <a href="http://code.google.com/p/goprotobuf/source/detail?r=5340ad310031">protocol buffer library</a>
1974may be helpful as examples.
1975</p>
1976
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02001977<h3 id="url">The url package</h3>
1978
1979<p>
Gustavo Niemeyer7b504852012-01-26 00:59:50 -02001980In Go 1 several fields from the <a href="/pkg/net/url/#URL"><code>url.URL</code></a> type
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02001981were removed or replaced.
1982</p>
1983
1984<p>
Gustavo Niemeyer7b504852012-01-26 00:59:50 -02001985The <a href="/pkg/net/url/#URL.String"><code>String</code></a> method now
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02001986predictably rebuilds an encoded URL string using all of <code>URL</code>'s
1987fields as necessary. The resulting string will also no longer have
1988passwords escaped.
1989</p>
1990
1991<p>
1992The <code>Raw</code> field has been removed. In most cases the <code>String</code>
1993method may be used in its place.
1994</p>
1995
1996<p>
1997The old <code>RawUserinfo</code> field is replaced by the <code>User</code>
Gustavo Niemeyer7b504852012-01-26 00:59:50 -02001998field, of type <a href="/pkg/net/url/#Userinfo"><code>*net.Userinfo</code></a>.
1999Values of this type may be created using the new <a href="/pkg/net/url/#User"><code>net.User</code></a>
2000and <a href="/pkg/net/url/#UserPassword"><code>net.UserPassword</code></a>
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02002001functions. The <code>EscapeUserinfo</code> and <code>UnescapeUserinfo</code>
2002functions are also gone.
2003</p>
2004
2005<p>
2006The <code>RawAuthority</code> field has been removed. The same information is
2007available in the <code>Host</code> and <code>User</code> fields.
2008</p>
2009
2010<p>
2011The <code>RawPath</code> field and the <code>EncodedPath</code> method have
2012been removed. The path information in rooted URLs (with a slash following the
2013schema) is now available only in decoded form in the <code>Path</code> field.
2014Occasionally, the encoded data may be required to obtain information that
2015was lost in the decoding process. These cases must be handled by accessing
2016the data the URL was built from.
2017</p>
2018
2019<p>
2020URLs with non-rooted paths, such as <code>"mailto:dev@golang.org?subject=Hi"</code>,
2021are also handled differently. The <code>OpaquePath</code> boolean field has been
2022removed and a new <code>Opaque</code> string field introduced to hold the encoded
2023path for such URLs. In Go 1, the cited URL parses as:
2024</p>
2025
2026<pre>
2027 URL{
2028 Scheme: "mailto",
2029 Opaque: "dev@golang.org",
2030 RawQuery: "subject=Hi",
2031 }
2032</pre>
2033
2034<p>
Gustavo Niemeyer7b504852012-01-26 00:59:50 -02002035A new <a href="/pkg/net/url/#URL.RequestURI"><code>RequestURI</code></a> method was
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02002036added to <code>URL</code>.
2037</p>
2038
2039<p>
David Symonds83427932012-02-16 15:56:03 +11002040The <code>ParseWithReference</code> function has been renamed to <code>ParseWithFragment</code>.
2041</p>
2042
2043<p>
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02002044<em>Updating</em>:
2045Code that uses the old fields will fail to compile and must be updated by hand.
Rob Pike27836912012-02-04 07:49:51 +11002046The semantic changes make it difficult for the fix tool to update automatically.
Gustavo Niemeyer805d6202012-01-25 23:11:25 -02002047</p>
2048
Russ Cox1a0c8fe2012-02-19 13:32:55 -05002049<h2 id="cmd_go">The go command</h2>
2050
2051<p>
2052TODO: Write this.
2053</p>
2054
2055<h2 id="cmd_cgo">The cgo command</h2>
2056
2057<p>
2058In Go 1, the <a href="/cmd/cgo">cgo command</a>
2059uses a different <code>_cgo_export.h</code>
2060file, which is generated for packages containing <code>//export</code> lines.
2061The <code>_cgo_export.h</code> file now begins with the C preamble comment,
2062so that exported function definitions can use types defined there.
2063This has the effect of compiling the preamble multiple times, so a
2064package using <code>//export</code> must not put function definitions
2065or variable initializations in the C preamble.
Rob Pikeb5a3bd52012-02-20 15:36:08 +11002066</p>
Rob Pikebab4dec2011-12-07 14:33:37 -08002067
2068<h2 id="releases">Packaged releases</h2>
2069