github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/doc/go1.html (about)

     1  <!--{
     2  	"Title": "Go 1 Release Notes",
     3  	"Template": true
     4  }-->
     5  
     6  <h2 id="introduction">Introduction to Go 1</h2>
     7  
     8  <p>
     9  Go version 1, Go 1 for short, defines a language and a set of core libraries
    10  that provide a stable foundation for creating reliable products, projects, and
    11  publications.
    12  </p>
    13  
    14  <p>
    15  The driving motivation for Go 1 is stability for its users. People should be able to
    16  write Go programs and expect that they will continue to compile and run without
    17  change, on a time scale of years, including in production environments such as
    18  Google App Engine. Similarly, people should be able to write books about Go, be
    19  able to say which version of Go the book is describing, and have that version
    20  number still be meaningful much later.
    21  </p>
    22  
    23  <p>
    24  Code that compiles in Go 1 should, with few exceptions, continue to compile and
    25  run throughout the lifetime of that version, even as we issue updates and bug
    26  fixes such as Go version 1.1, 1.2, and so on. Other than critical fixes, changes
    27  made to the language and library for subsequent releases of Go 1 may
    28  add functionality but will not break existing Go 1 programs.
    29  <a href="go1compat.html">The Go 1 compatibility document</a>
    30  explains the compatibility guidelines in more detail.
    31  </p>
    32  
    33  <p>
    34  Go 1 is a representation of Go as it used today, not a wholesale rethinking of
    35  the language. We avoided designing new features and instead focused on cleaning
    36  up problems and inconsistencies and improving portability. There are a number
    37  changes to the Go language and packages that we had considered for some time and
    38  prototyped but not released primarily because they are significant and
    39  backwards-incompatible. Go 1 was an opportunity to get them out, which is
    40  helpful for the long term, but also means that Go 1 introduces incompatibilities
    41  for old programs. Fortunately, the <code>go</code> <code>fix</code> tool can
    42  automate much of the work needed to bring programs up to the Go 1 standard.
    43  </p>
    44  
    45  <p>
    46  This document outlines the major changes in Go 1 that will affect programmers
    47  updating existing code; its reference point is the prior release, r60 (tagged as
    48  r60.3). It also explains how to update code from r60 to run under Go 1.
    49  </p>
    50  
    51  <h2 id="language">Changes to the language</h2>
    52  
    53  <h3 id="append">Append</h3>
    54  
    55  <p>
    56  The <code>append</code> predeclared variadic function makes it easy to grow a slice
    57  by adding elements to the end.
    58  A common use is to add bytes to the end of a byte slice when generating output.
    59  However, <code>append</code> did not provide a way to append a string to a <code>[]byte</code>,
    60  which is another common case.
    61  </p>
    62  
    63  {{code "/doc/progs/go1.go" `/greeting := ..byte/` `/append.*hello/`}}
    64  
    65  <p>
    66  By analogy with the similar property of <code>copy</code>, Go 1
    67  permits a string to be appended (byte-wise) directly to a byte
    68  slice, reducing the friction between strings and byte slices.
    69  The conversion is no longer necessary:
    70  </p>
    71  
    72  {{code "/doc/progs/go1.go" `/append.*world/`}}
    73  
    74  <p>
    75  <em>Updating</em>:
    76  This is a new feature, so existing code needs no changes.
    77  </p>
    78  
    79  <h3 id="close">Close</h3>
    80  
    81  <p>
    82  The <code>close</code> predeclared function provides a mechanism
    83  for a sender to signal that no more values will be sent.
    84  It is important to the implementation of <code>for</code> <code>range</code>
    85  loops over channels and is helpful in other situations.
    86  Partly by design and partly because of race conditions that can occur otherwise,
    87  it is intended for use only by the goroutine sending on the channel,
    88  not by the goroutine receiving data.
    89  However, before Go 1 there was no compile-time checking that <code>close</code>
    90  was being used correctly.
    91  </p>
    92  
    93  <p>
    94  To close this gap, at least in part, Go 1 disallows <code>close</code> on receive-only channels.
    95  Attempting to close such a channel is a compile-time error.
    96  </p>
    97  
    98  <pre>
    99      var c chan int
   100      var csend chan&lt;- int = c
   101      var crecv &lt;-chan int = c
   102      close(c)     // legal
   103      close(csend) // legal
   104      close(crecv) // illegal
   105  </pre>
   106  
   107  <p>
   108  <em>Updating</em>:
   109  Existing code that attempts to close a receive-only channel was
   110  erroneous even before Go 1 and should be fixed.  The compiler will
   111  now reject such code.
   112  </p>
   113  
   114  <h3 id="literals">Composite literals</h3>
   115  
   116  <p>
   117  In Go 1, a composite literal of array, slice, or map type can elide the
   118  type specification for the elements' initializers if they are of pointer type.
   119  All four of the initializations in this example are legal; the last one was illegal before Go 1.
   120  </p>
   121  
   122  {{code "/doc/progs/go1.go" `/type Date struct/` `/STOP/`}}
   123  
   124  <p>
   125  <em>Updating</em>:
   126  This change has no effect on existing code, but the command
   127  <code>gofmt</code> <code>-s</code> applied to existing source
   128  will, among other things, elide explicit element types wherever permitted.
   129  </p>
   130  
   131  
   132  <h3 id="init">Goroutines during init</h3>
   133  
   134  <p>
   135  The 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.
   136  This introduced clumsiness in many places and, in effect, limited the utility
   137  of the <code>init</code> construct:
   138  if it was possible for another package to use the library during initialization, the library
   139  was forced to avoid goroutines.
   140  This design was done for reasons of simplicity and safety but,
   141  as our confidence in the language grew, it seemed unnecessary.
   142  Running goroutines during initialization is no more complex or unsafe than running them during normal execution.
   143  </p>
   144  
   145  <p>
   146  In Go 1, code that uses goroutines can be called from
   147  <code>init</code> routines and global initialization expressions
   148  without introducing a deadlock.
   149  </p>
   150  
   151  {{code "/doc/progs/go1.go" `/PackageGlobal/` `/^}/`}}
   152  
   153  <p>
   154  <em>Updating</em>:
   155  This is a new feature, so existing code needs no changes,
   156  although it's possible that code that depends on goroutines not starting before <code>main</code> will break.
   157  There was no such code in the standard repository.
   158  </p>
   159  
   160  <h3 id="rune">The rune type</h3>
   161  
   162  <p>
   163  The 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.
   164  It would be preferable to have <code>int</code> be 64 bits on 64-bit platforms.
   165  (There are important consequences for indexing large slices.)
   166  However, this change would waste space when processing Unicode characters with
   167  the 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.
   168  </p>
   169  
   170  <p>
   171  To make changing to 64-bit <code>int</code> feasible,
   172  Go 1 introduces a new basic type, <code>rune</code>, to represent
   173  individual Unicode code points.
   174  It is an alias for <code>int32</code>, analogous to <code>byte</code>
   175  as an alias for <code>uint8</code>.
   176  </p>
   177  
   178  <p>
   179  Character literals such as <code>'a'</code>, <code>'語'</code>, and <code>'\u0345'</code>
   180  now have default type <code>rune</code>,
   181  analogous to <code>1.0</code> having default type <code>float64</code>.
   182  A variable initialized to a character constant will therefore
   183  have type <code>rune</code> unless otherwise specified.
   184  </p>
   185  
   186  <p>
   187  Libraries have been updated to use <code>rune</code> rather than <code>int</code>
   188  when appropriate. For instance, the functions <code>unicode.ToLower</code> and
   189  relatives now take and return a <code>rune</code>.
   190  </p>
   191  
   192  {{code "/doc/progs/go1.go" `/STARTRUNE/` `/ENDRUNE/`}}
   193  
   194  <p>
   195  <em>Updating</em>:
   196  Most source code will be unaffected by this because the type inference from
   197  <code>:=</code> initializers introduces the new type silently, and it propagates
   198  from there.
   199  Some code may get type errors that a trivial conversion will resolve.
   200  </p>
   201  
   202  <h3 id="error">The error type</h3>
   203  
   204  <p>
   205  Go 1 introduces a new built-in type, <code>error</code>, which has the following definition:
   206  </p>
   207  
   208  <pre>
   209      type error interface {
   210          Error() string
   211      }
   212  </pre>
   213  
   214  <p>
   215  Since the consequences of this type are all in the package library,
   216  it is discussed <a href="#errors">below</a>.
   217  </p>
   218  
   219  <h3 id="delete">Deleting from maps</h3>
   220  
   221  <p>
   222  In the old language, to delete the entry with key <code>k</code> from map <code>m</code>, one wrote the statement,
   223  </p>
   224  
   225  <pre>
   226      m[k] = value, false
   227  </pre>
   228  
   229  <p>
   230  This syntax was a peculiar special case, the only two-to-one assignment.
   231  It required passing a value (usually ignored) that is evaluated but discarded,
   232  plus a boolean that was nearly always the constant <code>false</code>.
   233  It did the job but was odd and a point of contention.
   234  </p>
   235  
   236  <p>
   237  In Go 1, that syntax has gone; instead there is a new built-in
   238  function, <code>delete</code>.  The call
   239  </p>
   240  
   241  {{code "/doc/progs/go1.go" `/delete\(m, k\)/`}}
   242  
   243  <p>
   244  will delete the map entry retrieved by the expression <code>m[k]</code>.
   245  There is no return value. Deleting a non-existent entry is a no-op.
   246  </p>
   247  
   248  <p>
   249  <em>Updating</em>:
   250  Running <code>go</code> <code>fix</code> will convert expressions of the form <code>m[k] = value,
   251  false</code> into <code>delete(m, k)</code> when it is clear that
   252  the ignored value can be safely discarded from the program and
   253  <code>false</code> refers to the predefined boolean constant.
   254  The fix tool
   255  will flag other uses of the syntax for inspection by the programmer.
   256  </p>
   257  
   258  <h3 id="iteration">Iterating in maps</h3>
   259  
   260  <p>
   261  The old language specification did not define the order of iteration for maps,
   262  and in practice it differed across hardware platforms.
   263  This caused tests that iterated over maps to be fragile and non-portable, with the
   264  unpleasant property that a test might always pass on one machine but break on another.
   265  </p>
   266  
   267  <p>
   268  In Go 1, the order in which elements are visited when iterating
   269  over a map using a <code>for</code> <code>range</code> statement
   270  is defined to be unpredictable, even if the same loop is run multiple
   271  times with the same map.
   272  Code should not assume that the elements are visited in any particular order.
   273  </p>
   274  
   275  <p>
   276  This change means that code that depends on iteration order is very likely to break early and be fixed long before it becomes a problem.
   277  Just 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.
   278  </p>
   279  
   280  {{code "/doc/progs/go1.go" `/Sunday/` `/^	}/`}}
   281  
   282  <p>
   283  <em>Updating</em>:
   284  This is one change where tools cannot help.  Most existing code
   285  will be unaffected, but some programs may break or misbehave; we
   286  recommend manual checking of all range statements over maps to
   287  verify they do not depend on iteration order. There were a few such
   288  examples in the standard repository; they have been fixed.
   289  Note that it was already incorrect to depend on the iteration order, which
   290  was unspecified. This change codifies the unpredictability.
   291  </p>
   292  
   293  <h3 id="multiple_assignment">Multiple assignment</h3>
   294  
   295  <p>
   296  The language specification has long guaranteed that in assignments
   297  the right-hand-side expressions are all evaluated before any left-hand-side expressions are assigned.
   298  To guarantee predictable behavior,
   299  Go 1 refines the specification further.
   300  </p>
   301  
   302  <p>
   303  If the left-hand side of the assignment
   304  statement contains expressions that require evaluation, such as
   305  function calls or array indexing operations, these will all be done
   306  using the usual left-to-right rule before any variables are assigned
   307  their value.  Once everything is evaluated, the actual assignments
   308  proceed in left-to-right order.
   309  </p>
   310  
   311  <p>
   312  These examples illustrate the behavior.
   313  </p>
   314  
   315  {{code "/doc/progs/go1.go" `/sa :=/` `/then sc.0. = 2/`}}
   316  
   317  <p>
   318  <em>Updating</em>:
   319  This is one change where tools cannot help, but breakage is unlikely.
   320  No code in the standard repository was broken by this change, and code
   321  that depended on the previous unspecified behavior was already incorrect.
   322  </p>
   323  
   324  <h3 id="shadowing">Returns and shadowed variables</h3>
   325  
   326  <p>
   327  A 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.
   328  This situation is called <em>shadowing</em>: the result variable has been shadowed by another variable with the same name declared in an inner scope.
   329  </p>
   330  
   331  <p>
   332  In functions with named return values,
   333  the Go 1 compilers disallow return statements without arguments if any of the named return values is shadowed at the point of the return statement.
   334  (It isn't part of the specification, because this is one area we are still exploring;
   335  the situation is analogous to the compilers rejecting functions that do not end with an explicit return statement.)
   336  </p>
   337  
   338  <p>
   339  This function implicitly returns a shadowed return value and will be rejected by the compiler:
   340  </p>
   341  
   342  <pre>
   343      func Bug() (i, j, k int) {
   344          for i = 0; i &lt; 5; i++ {
   345              for j := 0; j &lt; 5; j++ { // Redeclares j.
   346                  k += i*j
   347                  if k > 100 {
   348                      return // Rejected: j is shadowed here.
   349                  }
   350              }
   351          }
   352          return // OK: j is not shadowed here.
   353      }
   354  </pre>
   355  
   356  <p>
   357  <em>Updating</em>:
   358  Code that shadows return values in this way will be rejected by the compiler and will need to be fixed by hand.
   359  The few cases that arose in the standard repository were mostly bugs.
   360  </p>
   361  
   362  <h3 id="unexported">Copying structs with unexported fields</h3>
   363  
   364  <p>
   365  The old language did not allow a package to make a copy of a struct value containing unexported fields belonging to a different package.
   366  There was, however, a required exception for a method receiver;
   367  also, the implementations of <code>copy</code> and <code>append</code> have never honored the restriction.
   368  </p>
   369  
   370  <p>
   371  Go 1 will allow packages to copy struct values containing unexported fields from other packages.
   372  Besides resolving the inconsistency,
   373  this change admits a new kind of API: a package can return an opaque value without resorting to a pointer or interface.
   374  The new implementations of <code>time.Time</code> and
   375  <code>reflect.Value</code> are examples of types taking advantage of this new property.
   376  </p>
   377  
   378  <p>
   379  As an example, if package <code>p</code> includes the definitions,
   380  </p>
   381  
   382  <pre>
   383      type Struct struct {
   384          Public int
   385          secret int
   386      }
   387      func NewStruct(a int) Struct {  // Note: not a pointer.
   388          return Struct{a, f(a)}
   389      }
   390      func (s Struct) String() string {
   391          return fmt.Sprintf("{%d (secret %d)}", s.Public, s.secret)
   392      }
   393  </pre>
   394  
   395  <p>
   396  a package that imports <code>p</code> can assign and copy values of type
   397  <code>p.Struct</code> at will.
   398  Behind the scenes the unexported fields will be assigned and copied just
   399  as if they were exported,
   400  but the client code will never be aware of them. The code
   401  </p>
   402  
   403  <pre>
   404      import "p"
   405  
   406      myStruct := p.NewStruct(23)
   407      copyOfMyStruct := myStruct
   408      fmt.Println(myStruct, copyOfMyStruct)
   409  </pre>
   410  
   411  <p>
   412  will show that the secret field of the struct has been copied to the new value.
   413  </p>
   414  
   415  <p>
   416  <em>Updating</em>:
   417  This is a new feature, so existing code needs no changes.
   418  </p>
   419  
   420  <h3 id="equality">Equality</h3>
   421  
   422  <p>
   423  Before Go 1, the language did not define equality on struct and array values.
   424  This meant,
   425  among other things, that structs and arrays could not be used as map keys.
   426  On the other hand, Go did define equality on function and map values.
   427  Function equality was problematic in the presence of closures
   428  (when are two closures equal?)
   429  while map equality compared pointers, not the maps' content, which was usually
   430  not what the user would want.
   431  </p>
   432  
   433  <p>
   434  Go 1 addressed these issues.
   435  First, structs and arrays can be compared for equality and inequality
   436  (<code>==</code> and <code>!=</code>),
   437  and therefore be used as map keys,
   438  provided they are composed from elements for which equality is also defined,
   439  using element-wise comparison.
   440  </p>
   441  
   442  {{code "/doc/progs/go1.go" `/type Day struct/` `/Printf/`}}
   443  
   444  <p>
   445  Second, Go 1 removes the definition of equality for function values,
   446  except for comparison with <code>nil</code>.
   447  Finally, map equality is gone too, also except for comparison with <code>nil</code>.
   448  </p>
   449  
   450  <p>
   451  Note that equality is still undefined for slices, for which the
   452  calculation is in general infeasible.  Also note that the ordered
   453  comparison operators (<code>&lt;</code> <code>&lt;=</code>
   454  <code>&gt;</code> <code>&gt;=</code>) are still undefined for
   455  structs and arrays.
   456  
   457  <p>
   458  <em>Updating</em>:
   459  Struct and array equality is a new feature, so existing code needs no changes.
   460  Existing code that depends on function or map equality will be
   461  rejected by the compiler and will need to be fixed by hand.
   462  Few programs will be affected, but the fix may require some
   463  redesign.
   464  </p>
   465  
   466  <h2 id="packages">The package hierarchy</h2>
   467  
   468  <p>
   469  Go 1 addresses many deficiencies in the old standard library and
   470  cleans up a number of packages, making them more internally consistent
   471  and portable.
   472  </p>
   473  
   474  <p>
   475  This section describes how the packages have been rearranged in Go 1.
   476  Some have moved, some have been renamed, some have been deleted.
   477  New packages are described in later sections.
   478  </p>
   479  
   480  <h3 id="hierarchy">The package hierarchy</h3>
   481  
   482  <p>
   483  Go 1 has a rearranged package hierarchy that groups related items
   484  into subdirectories. For instance, <code>utf8</code> and
   485  <code>utf16</code> now occupy subdirectories of <code>unicode</code>.
   486  Also, <a href="#subrepo">some packages</a> have moved into
   487  subrepositories of
   488  <a href="http://code.google.com/p/go"><code>code.google.com/p/go</code></a>
   489  while <a href="#deleted">others</a> have been deleted outright.
   490  </p>
   491  
   492  <table class="codetable" frame="border" summary="Moved packages">
   493  <colgroup align="left" width="60%"></colgroup>
   494  <colgroup align="left" width="40%"></colgroup>
   495  <tr>
   496  <th align="left">Old path</th>
   497  <th align="left">New path</th>
   498  </tr>
   499  <tr>
   500  <td colspan="2"><hr></td>
   501  </tr>
   502  <tr><td>asn1</td> <td>encoding/asn1</td></tr>
   503  <tr><td>csv</td> <td>encoding/csv</td></tr>
   504  <tr><td>gob</td> <td>encoding/gob</td></tr>
   505  <tr><td>json</td> <td>encoding/json</td></tr>
   506  <tr><td>xml</td> <td>encoding/xml</td></tr>
   507  <tr>
   508  <td colspan="2"><hr></td>
   509  </tr>
   510  <tr><td>exp/template/html</td> <td>html/template</td></tr>
   511  <tr>
   512  <td colspan="2"><hr></td>
   513  </tr>
   514  <tr><td>big</td> <td>math/big</td></tr>
   515  <tr><td>cmath</td> <td>math/cmplx</td></tr>
   516  <tr><td>rand</td> <td>math/rand</td></tr>
   517  <tr>
   518  <td colspan="2"><hr></td>
   519  </tr>
   520  <tr><td>http</td> <td>net/http</td></tr>
   521  <tr><td>http/cgi</td> <td>net/http/cgi</td></tr>
   522  <tr><td>http/fcgi</td> <td>net/http/fcgi</td></tr>
   523  <tr><td>http/httptest</td> <td>net/http/httptest</td></tr>
   524  <tr><td>http/pprof</td> <td>net/http/pprof</td></tr>
   525  <tr><td>mail</td> <td>net/mail</td></tr>
   526  <tr><td>rpc</td> <td>net/rpc</td></tr>
   527  <tr><td>rpc/jsonrpc</td> <td>net/rpc/jsonrpc</td></tr>
   528  <tr><td>smtp</td> <td>net/smtp</td></tr>
   529  <tr><td>url</td> <td>net/url</td></tr>
   530  <tr>
   531  <td colspan="2"><hr></td>
   532  </tr>
   533  <tr><td>exec</td> <td>os/exec</td></tr>
   534  <tr>
   535  <td colspan="2"><hr></td>
   536  </tr>
   537  <tr><td>scanner</td> <td>text/scanner</td></tr>
   538  <tr><td>tabwriter</td> <td>text/tabwriter</td></tr>
   539  <tr><td>template</td> <td>text/template</td></tr>
   540  <tr><td>template/parse</td> <td>text/template/parse</td></tr>
   541  <tr>
   542  <td colspan="2"><hr></td>
   543  </tr>
   544  <tr><td>utf8</td> <td>unicode/utf8</td></tr>
   545  <tr><td>utf16</td> <td>unicode/utf16</td></tr>
   546  </table>
   547  
   548  <p>
   549  Note that the package names for the old <code>cmath</code> and
   550  <code>exp/template/html</code> packages have changed to <code>cmplx</code>
   551  and <code>template</code>.
   552  </p>
   553  
   554  <p>
   555  <em>Updating</em>:
   556  Running <code>go</code> <code>fix</code> will update all imports and package renames for packages that
   557  remain inside the standard repository.  Programs that import packages
   558  that are no longer in the standard repository will need to be edited
   559  by hand.
   560  </p>
   561  
   562  <h3 id="exp">The package tree exp</h3>
   563  
   564  <p>
   565  Because they are not standardized, the packages under the <code>exp</code> directory will not be available in the
   566  standard Go 1 release distributions, although they will be available in source code form
   567  in <a href="http://code.google.com/p/go/">the repository</a> for
   568  developers who wish to use them.
   569  </p>
   570  
   571  <p>
   572  Several packages have moved under <code>exp</code> at the time of Go 1's release:
   573  </p>
   574  
   575  <ul>
   576  <li><code>ebnf</code></li>
   577  <li><code>html</code><sup>&#8224;</sup></li>
   578  <li><code>go/types</code></li>
   579  </ul>
   580  
   581  <p>
   582  (<sup>&#8224;</sup>The <code>EscapeString</code> and <code>UnescapeString</code> types remain
   583  in package <code>html</code>.)
   584  </p>
   585  
   586  <p>
   587  All these packages are available under the same names, with the prefix <code>exp/</code>: <code>exp/ebnf</code> etc.
   588  </p>
   589  
   590  <p>
   591  Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>.
   592  </p>
   593  
   594  <p>
   595  Finally, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while
   596  <code>ebnflint</code> is now in <code>exp/ebnflint</code>.
   597  If they are installed, they now reside in <code>$GOROOT/bin/tool</code>.
   598  </p>
   599  
   600  <p>
   601  <em>Updating</em>:
   602  Code that uses packages in <code>exp</code> will need to be updated by hand,
   603  or else compiled from an installation that has <code>exp</code> available.
   604  The <code>go</code> <code>fix</code> tool or the compiler will complain about such uses.
   605  </p>
   606  
   607  <h3 id="old">The package tree old</h3>
   608  
   609  <p>
   610  Because they are deprecated, the packages under the <code>old</code> directory will not be available in the
   611  standard Go 1 release distributions, although they will be available in source code form for
   612  developers who wish to use them.
   613  </p>
   614  
   615  <p>
   616  The packages in their new locations are:
   617  </p>
   618  
   619  <ul>
   620  <li><code>old/netchan</code></li>
   621  </ul>
   622  
   623  <p>
   624  <em>Updating</em>:
   625  Code that uses packages now in <code>old</code> will need to be updated by hand,
   626  or else compiled from an installation that has <code>old</code> available.
   627  The <code>go</code> <code>fix</code> tool will warn about such uses.
   628  </p>
   629  
   630  <h3 id="deleted">Deleted packages</h3>
   631  
   632  <p>
   633  Go 1 deletes several packages outright:
   634  </p>
   635  
   636  <ul>
   637  <li><code>container/vector</code></li>
   638  <li><code>exp/datafmt</code></li>
   639  <li><code>go/typechecker</code></li>
   640  <li><code>old/regexp</code></li>
   641  <li><code>old/template</code></li>
   642  <li><code>try</code></li>
   643  </ul>
   644  
   645  <p>
   646  and also the command <code>gotry</code>.
   647  </p>
   648  
   649  <p>
   650  <em>Updating</em>:
   651  Code that uses <code>container/vector</code> should be updated to use
   652  slices directly.  See
   653  <a href="http://code.google.com/p/go-wiki/wiki/SliceTricks">the Go
   654  Language Community Wiki</a> for some suggestions.
   655  Code that uses the other packages (there should be almost zero) will need to be rethought.
   656  </p>
   657  
   658  <h3 id="subrepo">Packages moving to subrepositories</h3>
   659  
   660  <p>
   661  Go 1 has moved a number of packages into other repositories, usually sub-repositories of
   662  <a href="http://code.google.com/p/go/">the main Go repository</a>.
   663  This table lists the old and new import paths:
   664  
   665  <table class="codetable" frame="border" summary="Sub-repositories">
   666  <colgroup align="left" width="40%"></colgroup>
   667  <colgroup align="left" width="60%"></colgroup>
   668  <tr>
   669  <th align="left">Old</th>
   670  <th align="left">New</th>
   671  </tr>
   672  <tr>
   673  <td colspan="2"><hr></td>
   674  </tr>
   675  <tr><td>crypto/bcrypt</td> <td>code.google.com/p/go.crypto/bcrypt</tr>
   676  <tr><td>crypto/blowfish</td> <td>code.google.com/p/go.crypto/blowfish</tr>
   677  <tr><td>crypto/cast5</td> <td>code.google.com/p/go.crypto/cast5</tr>
   678  <tr><td>crypto/md4</td> <td>code.google.com/p/go.crypto/md4</tr>
   679  <tr><td>crypto/ocsp</td> <td>code.google.com/p/go.crypto/ocsp</tr>
   680  <tr><td>crypto/openpgp</td> <td>code.google.com/p/go.crypto/openpgp</tr>
   681  <tr><td>crypto/openpgp/armor</td> <td>code.google.com/p/go.crypto/openpgp/armor</tr>
   682  <tr><td>crypto/openpgp/elgamal</td> <td>code.google.com/p/go.crypto/openpgp/elgamal</tr>
   683  <tr><td>crypto/openpgp/errors</td> <td>code.google.com/p/go.crypto/openpgp/errors</tr>
   684  <tr><td>crypto/openpgp/packet</td> <td>code.google.com/p/go.crypto/openpgp/packet</tr>
   685  <tr><td>crypto/openpgp/s2k</td> <td>code.google.com/p/go.crypto/openpgp/s2k</tr>
   686  <tr><td>crypto/ripemd160</td> <td>code.google.com/p/go.crypto/ripemd160</tr>
   687  <tr><td>crypto/twofish</td> <td>code.google.com/p/go.crypto/twofish</tr>
   688  <tr><td>crypto/xtea</td> <td>code.google.com/p/go.crypto/xtea</tr>
   689  <tr><td>exp/ssh</td> <td>code.google.com/p/go.crypto/ssh</tr>
   690  <tr>
   691  <td colspan="2"><hr></td>
   692  </tr>
   693  <tr><td>image/bmp</td> <td>code.google.com/p/go.image/bmp</tr>
   694  <tr><td>image/tiff</td> <td>code.google.com/p/go.image/tiff</tr>
   695  <tr>
   696  <td colspan="2"><hr></td>
   697  </tr>
   698  <tr><td>net/dict</td> <td>code.google.com/p/go.net/dict</tr>
   699  <tr><td>net/websocket</td> <td>code.google.com/p/go.net/websocket</tr>
   700  <tr><td>exp/spdy</td> <td>code.google.com/p/go.net/spdy</tr>
   701  <tr>
   702  <td colspan="2"><hr></td>
   703  </tr>
   704  <tr><td>encoding/git85</td> <td>code.google.com/p/go.codereview/git85</tr>
   705  <tr><td>patch</td> <td>code.google.com/p/go.codereview/patch</tr>
   706  <tr>
   707  <td colspan="2"><hr></td>
   708  </tr>
   709  <tr><td>exp/wingui</td> <td>code.google.com/p/gowingui</tr>
   710  </table>
   711  
   712  <p>
   713  <em>Updating</em>:
   714  Running <code>go</code> <code>fix</code> will update imports of these packages to use the new import paths.
   715  Installations that depend on these packages will need to install them using
   716  a <code>go get</code> command.
   717  </p>
   718  
   719  <h2 id="major">Major changes to the library</h2>
   720  
   721  <p>
   722  This section describes significant changes to the core libraries, the ones that
   723  affect the most programs.
   724  </p>
   725  
   726  <h3 id="errors">The error type and errors package</h3>
   727  
   728  <p>
   729  The 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.
   730  Since 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>.
   731  Also, having <code>Error</code> in <code>os</code> introduces many dependencies on <code>os</code> that would otherwise not exist.
   732  </p>
   733  
   734  <p>
   735  Go 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.
   736  It replaces <code>os.NewError</code> with
   737  <a href="/pkg/errors/#New"><code>errors.New</code></a>,
   738  giving errors a more central place in the environment.
   739  </p>
   740  
   741  <p>
   742  So the widely-used <code>String</code> method does not cause accidental satisfaction
   743  of the <code>error</code> interface, the <code>error</code> interface uses instead
   744  the name <code>Error</code> for that method:
   745  </p>
   746  
   747  <pre>
   748      type error interface {
   749          Error() string
   750      }
   751  </pre>
   752  
   753  <p>
   754  The <code>fmt</code> library automatically invokes <code>Error</code>, as it already
   755  does for <code>String</code>, for easy printing of error values.
   756  </p>
   757  
   758  {{code "/doc/progs/go1.go" `/START ERROR EXAMPLE/` `/END ERROR EXAMPLE/`}}
   759  
   760  <p>
   761  All standard packages have been updated to use the new interface; the old <code>os.Error</code> is gone.
   762  </p>
   763  
   764  <p>
   765  A new package, <a href="/pkg/errors/"><code>errors</code></a>, contains the function
   766  </p>
   767  
   768  <pre>
   769  func New(text string) error
   770  </pre>
   771  
   772  <p>
   773  to turn a string into an error. It replaces the old <code>os.NewError</code>.
   774  </p>
   775  
   776  {{code "/doc/progs/go1.go" `/ErrSyntax/`}}
   777  		
   778  <p>
   779  <em>Updating</em>:
   780  Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
   781  Code that defines error types with a <code>String</code> method will need to be updated
   782  by hand to rename the methods to <code>Error</code>.
   783  </p>
   784  
   785  <h3 id="errno">System call errors</h3>
   786  
   787  <p>
   788  The old <code>syscall</code> package, which predated <code>os.Error</code>
   789  (and just about everything else),
   790  returned errors as <code>int</code> values.
   791  In turn, the <code>os</code> package forwarded many of these errors, such
   792  as <code>EINVAL</code>, but using a different set of errors on each platform.
   793  This behavior was unpleasant and unportable.
   794  </p>
   795  
   796  <p>
   797  In Go 1, the
   798  <a href="/pkg/syscall/"><code>syscall</code></a>
   799  package instead returns an <code>error</code> for system call errors.
   800  On Unix, the implementation is done by a
   801  <a href="/pkg/syscall/#Errno"><code>syscall.Errno</code></a> type
   802  that satisfies <code>error</code> and replaces the old <code>os.Errno</code>.
   803  </p>
   804  
   805  <p>
   806  The changes affecting <code>os.EINVAL</code> and relatives are
   807  described <a href="#os">elsewhere</a>.
   808  
   809  <p>
   810  <em>Updating</em>:
   811  Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
   812  Regardless, most code should use the <code>os</code> package
   813  rather than <code>syscall</code> and so will be unaffected.
   814  </p>
   815  
   816  <h3 id="time">Time</h3>
   817  
   818  <p>
   819  Time is always a challenge to support well in a programming language.
   820  The old Go <code>time</code> package had <code>int64</code> units, no
   821  real type safety,
   822  and no distinction between absolute times and durations.
   823  </p>
   824  
   825  <p>
   826  One of the most sweeping changes in the Go 1 library is therefore a
   827  complete redesign of the
   828  <a href="/pkg/time/"><code>time</code></a> package.
   829  Instead of an integer number of nanoseconds as an <code>int64</code>,
   830  and a separate <code>*time.Time</code> type to deal with human
   831  units such as hours and years,
   832  there are now two fundamental types:
   833  <a href="/pkg/time/#Time"><code>time.Time</code></a>
   834  (a value, so the <code>*</code> is gone), which represents a moment in time;
   835  and <a href="/pkg/time/#Duration"><code>time.Duration</code></a>,
   836  which represents an interval.
   837  Both have nanosecond resolution.
   838  A <code>Time</code> can represent any time into the ancient
   839  past and remote future, while a <code>Duration</code> can
   840  span plus or minus only about 290 years.
   841  There are methods on these types, plus a number of helpful
   842  predefined constant durations such as <code>time.Second</code>.
   843  </p>
   844  
   845  <p>
   846  Among the new methods are things like
   847  <a href="/pkg/time/#Time.Add"><code>Time.Add</code></a>,
   848  which adds a <code>Duration</code> to a <code>Time</code>, and
   849  <a href="/pkg/time/#Time.Sub"><code>Time.Sub</code></a>,
   850  which subtracts two <code>Times</code> to yield a <code>Duration</code>.
   851  </p>
   852  
   853  <p>
   854  The most important semantic change is that the Unix epoch (Jan 1, 1970) is now
   855  relevant only for those functions and methods that mention Unix:
   856  <a href="/pkg/time/#Unix"><code>time.Unix</code></a>
   857  and the <a href="/pkg/time/#Time.Unix"><code>Unix</code></a>
   858  and <a href="/pkg/time/#Time.UnixNano"><code>UnixNano</code></a> methods
   859  of the <code>Time</code> type.
   860  In particular,
   861  <a href="/pkg/time/#Now"><code>time.Now</code></a>
   862  returns a <code>time.Time</code> value rather than, in the old
   863  API, an integer nanosecond count since the Unix epoch.
   864  </p>
   865  
   866  {{code "/doc/progs/go1.go" `/sleepUntil/` `/^}/`}}
   867  
   868  <p>
   869  The new types, methods, and constants have been propagated through
   870  all the standard packages that use time, such as <code>os</code> and
   871  its representation of file time stamps.
   872  </p>
   873  
   874  <p>
   875  <em>Updating</em>:
   876  The <code>go</code> <code>fix</code> tool will update many uses of the old <code>time</code> package to use the new
   877  types and methods, although it does not replace values such as <code>1e9</code>
   878  representing nanoseconds per second.
   879  Also, because of type changes in some of the values that arise,
   880  some of the expressions rewritten by the fix tool may require
   881  further hand editing; in such cases the rewrite will include
   882  the correct function or method for the old functionality, but
   883  may have the wrong type or require further analysis.
   884  </p>
   885  
   886  <h2 id="minor">Minor changes to the library</h2>
   887  
   888  <p>
   889  This section describes smaller changes, such as those to less commonly
   890  used packages or that affect
   891  few programs beyond the need to run <code>go</code> <code>fix</code>.
   892  This category includes packages that are new in Go 1.
   893  Collectively they improve portability, regularize behavior, and
   894  make the interfaces more modern and Go-like.
   895  </p>
   896  
   897  <h3 id="archive_zip">The archive/zip package</h3>
   898  
   899  <p>
   900  In Go 1, <a href="/pkg/archive/zip/#Writer"><code>*zip.Writer</code></a> no
   901  longer has a <code>Write</code> method. Its presence was a mistake.
   902  </p>
   903  
   904  <p>
   905  <em>Updating</em>:
   906  What little code is affected will be caught by the compiler and must be updated by hand.
   907  </p>
   908  
   909  <h3 id="bufio">The bufio package</h3>
   910  
   911  <p>
   912  In Go 1, <a href="/pkg/bufio/#NewReaderSize"><code>bufio.NewReaderSize</code></a>
   913  and
   914  <a href="/pkg/bufio/#NewWriterSize"><code>bufio.NewWriterSize</code></a>
   915  functions no longer return an error for invalid sizes.
   916  If the argument size is too small or invalid, it is adjusted.
   917  </p>
   918  
   919  <p>
   920  <em>Updating</em>:
   921  Running <code>go</code> <code>fix</code> will update calls that assign the error to _.
   922  Calls that aren't fixed will be caught by the compiler and must be updated by hand.
   923  </p>
   924  
   925  <h3 id="compress">The compress/flate, compress/gzip and compress/zlib packages</h3>
   926  
   927  <p>
   928  In Go 1, the <code>NewWriterXxx</code> functions in
   929  <a href="/pkg/compress/flate"><code>compress/flate</code></a>,
   930  <a href="/pkg/compress/gzip"><code>compress/gzip</code></a> and
   931  <a href="/pkg/compress/zlib"><code>compress/zlib</code></a>
   932  all return <code>(*Writer, error)</code> if they take a compression level,
   933  and <code>*Writer</code> otherwise. Package <code>gzip</code>'s
   934  <code>Compressor</code> and <code>Decompressor</code> types have been renamed
   935  to <code>Writer</code> and <code>Reader</code>. Package <code>flate</code>'s
   936  <code>WrongValueError</code> type has been removed.
   937  </p>
   938  
   939  <p>
   940  <em>Updating</em>
   941  Running <code>go</code> <code>fix</code> will update old names and calls that assign the error to _.
   942  Calls that aren't fixed will be caught by the compiler and must be updated by hand.
   943  </p>
   944  
   945  <h3 id="crypto_aes_des">The crypto/aes and crypto/des packages</h3>
   946  
   947  <p>
   948  In Go 1, the <code>Reset</code> method has been removed. Go does not guarantee
   949  that memory is not copied and therefore this method was misleading.
   950  </p>
   951  
   952  <p>
   953  The cipher-specific types <code>*aes.Cipher</code>, <code>*des.Cipher</code>,
   954  and <code>*des.TripleDESCipher</code> have been removed in favor of
   955  <code>cipher.Block</code>.
   956  </p>
   957  
   958  <p>
   959  <em>Updating</em>:
   960  Remove the calls to Reset. Replace uses of the specific cipher types with
   961  cipher.Block.
   962  </p>
   963  
   964  <h3 id="crypto_elliptic">The crypto/elliptic package</h3>
   965  
   966  <p>
   967  In Go 1, <a href="/pkg/crypto/elliptic/#Curve"><code>elliptic.Curve</code></a>
   968  has been made an interface to permit alternative implementations. The curve
   969  parameters have been moved to the
   970  <a href="/pkg/crypto/elliptic/#CurveParams"><code>elliptic.CurveParams</code></a>
   971  structure.
   972  </p>
   973  
   974  <p>
   975  <em>Updating</em>:
   976  Existing users of <code>*elliptic.Curve</code> will need to change to
   977  simply <code>elliptic.Curve</code>. Calls to <code>Marshal</code>,
   978  <code>Unmarshal</code> and <code>GenerateKey</code> are now functions
   979  in <code>crypto/elliptic</code> that take an <code>elliptic.Curve</code>
   980  as their first argument.
   981  </p>
   982  
   983  <h3 id="crypto_hmac">The crypto/hmac package</h3>
   984  
   985  <p>
   986  In Go 1, the hash-specific functions, such as <code>hmac.NewMD5</code>, have
   987  been removed from <code>crypto/hmac</code>. Instead, <code>hmac.New</code> takes
   988  a function that returns a <code>hash.Hash</code>, such as <code>md5.New</code>.
   989  </p>
   990  
   991  <p>
   992  <em>Updating</em>:
   993  Running <code>go</code> <code>fix</code> will perform the needed changes.
   994  </p>
   995  
   996  <h3 id="crypto_x509">The crypto/x509 package</h3>
   997  
   998  <p>
   999  In Go 1, the
  1000  <a href="/pkg/crypto/x509/#CreateCertificate"><code>CreateCertificate</code></a>
  1001  function and
  1002  <a href="/pkg/crypto/x509/#Certificate.CreateCRL"><code>CreateCRL</code></a>
  1003  method in <code>crypto/x509</code> have been altered to take an
  1004  <code>interface{}</code> where they previously took a <code>*rsa.PublicKey</code>
  1005  or <code>*rsa.PrivateKey</code>. This will allow other public key algorithms
  1006  to be implemented in the future.
  1007  </p>
  1008  
  1009  <p>
  1010  <em>Updating</em>:
  1011  No changes will be needed.
  1012  </p>
  1013  
  1014  <h3 id="encoding_binary">The encoding/binary package</h3>
  1015  
  1016  <p>
  1017  In Go 1, the <code>binary.TotalSize</code> function has been replaced by
  1018  <a href="/pkg/encoding/binary/#Size"><code>Size</code></a>,
  1019  which takes an <code>interface{}</code> argument rather than
  1020  a <code>reflect.Value</code>.
  1021  </p>
  1022  
  1023  <p>
  1024  <em>Updating</em>:
  1025  What little code is affected will be caught by the compiler and must be updated by hand.
  1026  </p>
  1027  
  1028  <h3 id="encoding_xml">The encoding/xml package</h3>
  1029  
  1030  <p>
  1031  In Go 1, the <a href="/pkg/encoding/xml/"><code>xml</code></a> package
  1032  has been brought closer in design to the other marshaling packages such
  1033  as <a href="/pkg/encoding/gob/"><code>encoding/gob</code></a>.
  1034  </p>
  1035  
  1036  <p>
  1037  The old <code>Parser</code> type is renamed
  1038  <a href="/pkg/encoding/xml/#Decoder"><code>Decoder</code></a> and has a new
  1039  <a href="/pkg/encoding/xml/#Decoder.Decode"><code>Decode</code></a> method. An
  1040  <a href="/pkg/encoding/xml/#Encoder"><code>Encoder</code></a> type was also introduced.
  1041  </p>
  1042  
  1043  <p>
  1044  The functions <a href="/pkg/encoding/xml/#Marshal"><code>Marshal</code></a>
  1045  and <a href="/pkg/encoding/xml/#Unmarshal"><code>Unmarshal</code></a>
  1046  work with <code>[]byte</code> values now. To work with streams,
  1047  use the new <a href="/pkg/encoding/xml/#Encoder"><code>Encoder</code></a>
  1048  and <a href="/pkg/encoding/xml/#Decoder"><code>Decoder</code></a> types.
  1049  </p>
  1050  
  1051  <p>
  1052  When marshaling or unmarshaling values, the format of supported flags in
  1053  field tags has changed to be closer to the
  1054  <a href="/pkg/encoding/json"><code>json</code></a> package
  1055  (<code>`xml:"name,flag"`</code>). The matching done between field tags, field
  1056  names, and the XML attribute and element names is now case-sensitive.
  1057  The <code>XMLName</code> field tag, if present, must also match the name
  1058  of the XML element being marshaled.
  1059  </p>
  1060  
  1061  <p>
  1062  <em>Updating</em>:
  1063  Running <code>go</code> <code>fix</code> will update most uses of the package except for some calls to
  1064  <code>Unmarshal</code>. Special care must be taken with field tags,
  1065  since the fix tool will not update them and if not fixed by hand they will
  1066  misbehave silently in some cases. For example, the old
  1067  <code>"attr"</code> is now written <code>",attr"</code> while plain
  1068  <code>"attr"</code> remains valid but with a different meaning.
  1069  </p>
  1070  
  1071  <h3 id="expvar">The expvar package</h3>
  1072  
  1073  <p>
  1074  In Go 1, the <code>RemoveAll</code> function has been removed.
  1075  The <code>Iter</code> function and Iter method on <code>*Map</code> have
  1076  been replaced by
  1077  <a href="/pkg/expvar/#Do"><code>Do</code></a>
  1078  and
  1079  <a href="/pkg/expvar/#Map.Do"><code>(*Map).Do</code></a>.
  1080  </p>
  1081  
  1082  <p>
  1083  <em>Updating</em>:
  1084  Most code using <code>expvar</code> will not need changing. The rare code that used
  1085  <code>Iter</code> can be updated to pass a closure to <code>Do</code> to achieve the same effect.
  1086  </p>
  1087  
  1088  <h3 id="flag">The flag package</h3>
  1089  
  1090  <p>
  1091  In Go 1, the interface <a href="/pkg/flag/#Value"><code>flag.Value</code></a> has changed slightly.
  1092  The <code>Set</code> method now returns an <code>error</code> instead of
  1093  a <code>bool</code> to indicate success or failure.
  1094  </p>
  1095  
  1096  <p>
  1097  There is also a new kind of flag, <code>Duration</code>, to support argument
  1098  values specifying time intervals.
  1099  Values for such flags must be given units, just as <code>time.Duration</code>
  1100  formats them: <code>10s</code>, <code>1h30m</code>, etc.
  1101  </p>
  1102  
  1103  {{code "/doc/progs/go1.go" `/timeout/`}}
  1104  
  1105  <p>
  1106  <em>Updating</em>:
  1107  Programs that implement their own flags will need minor manual fixes to update their
  1108  <code>Set</code> methods.
  1109  The <code>Duration</code> flag is new and affects no existing code.
  1110  </p>
  1111  
  1112  
  1113  <h3 id="go">The go/* packages</h3>
  1114  
  1115  <p>
  1116  Several packages under <code>go</code> have slightly revised APIs.
  1117  </p>
  1118  
  1119  <p>
  1120  A concrete <code>Mode</code> type was introduced for configuration mode flags
  1121  in the packages
  1122  <a href="/pkg/go/scanner/"><code>go/scanner</code></a>,
  1123  <a href="/pkg/go/parser/"><code>go/parser</code></a>,
  1124  <a href="/pkg/go/printer/"><code>go/printer</code></a>, and
  1125  <a href="/pkg/go/doc/"><code>go/doc</code></a>.
  1126  </p>
  1127  
  1128  <p>
  1129  The modes <code>AllowIllegalChars</code> and <code>InsertSemis</code> have been removed
  1130  from the <a href="/pkg/go/scanner/"><code>go/scanner</code></a> package. They were mostly
  1131  useful for scanning text other then Go source files. Instead, the
  1132  <a href="/pkg/text/scanner/"><code>text/scanner</code></a> package should be used
  1133  for that purpose.
  1134  </p>
  1135  
  1136  <p>
  1137  The <a href="/pkg/go/scanner/#ErrorHandler"><code>ErrorHandler</code></a> provided
  1138  to the scanner's <a href="/pkg/go/scanner/#Scanner.Init"><code>Init</code></a> method is
  1139  now simply a function rather than an interface. The <code>ErrorVector</code> type has
  1140  been removed in favor of the (existing) <a href="/pkg/go/scanner/#ErrorList"><code>ErrorList</code></a>
  1141  type, and the <code>ErrorVector</code> methods have been migrated. Instead of embedding
  1142  an <code>ErrorVector</code> in a client of the scanner, now a client should maintain
  1143  an <code>ErrorList</code>.
  1144  </p>
  1145  
  1146  <p>
  1147  The set of parse functions provided by the <a href="/pkg/go/parser/"><code>go/parser</code></a>
  1148  package has been reduced to the primary parse function
  1149  <a href="/pkg/go/parser/#ParseFile"><code>ParseFile</code></a>, and a couple of
  1150  convenience functions <a href="/pkg/go/parser/#ParseDir"><code>ParseDir</code></a>
  1151  and <a href="/pkg/go/parser/#ParseExpr"><code>ParseExpr</code></a>.
  1152  </p>
  1153  
  1154  <p>
  1155  The <a href="/pkg/go/printer/"><code>go/printer</code></a> package supports an additional
  1156  configuration mode <a href="/pkg/go/printer/#Mode"><code>SourcePos</code></a>;
  1157  if set, the printer will emit <code>//line</code> comments such that the generated
  1158  output contains the original source code position information. The new type
  1159  <a href="/pkg/go/printer/#CommentedNode"><code>CommentedNode</code></a> can be
  1160  used to provide comments associated with an arbitrary
  1161  <a href="/pkg/go/ast/#Node"><code>ast.Node</code></a> (until now only
  1162  <a href="/pkg/go/ast/#File"><code>ast.File</code></a> carried comment information).
  1163  </p>
  1164  
  1165  <p>
  1166  The type names of the <a href="/pkg/go/doc/"><code>go/doc</code></a> package have been
  1167  streamlined by removing the <code>Doc</code> suffix: <code>PackageDoc</code>
  1168  is now <code>Package</code>, <code>ValueDoc</code> is <code>Value</code>, etc.
  1169  Also, all types now consistently have a <code>Name</code> field (or <code>Names</code>,
  1170  in the case of type <code>Value</code>) and <code>Type.Factories</code> has become
  1171  <code>Type.Funcs</code>.
  1172  Instead of calling <code>doc.NewPackageDoc(pkg, importpath)</code>,
  1173  documentation for a package is created with:
  1174  </p>
  1175  
  1176  <pre>
  1177      doc.New(pkg, importpath, mode)
  1178  </pre>
  1179  
  1180  <p>
  1181  where the new <code>mode</code> parameter specifies the operation mode:
  1182  if set to <a href="/pkg/go/doc/#AllDecls"><code>AllDecls</code></a>, all declarations
  1183  (not just exported ones) are considered.
  1184  The function <code>NewFileDoc</code> was removed, and the function
  1185  <code>CommentText</code> has become the method
  1186  <a href="/pkg/go/ast/#CommentGroup.Text"><code>Text</code></a> of
  1187  <a href="/pkg/go/ast/#CommentGroup"><code>ast.CommentGroup</code></a>.
  1188  </p>
  1189  
  1190  <p>
  1191  In package <a href="/pkg/go/token/"><code>go/token</code></a>, the
  1192  <a href="/pkg/go/token/#FileSet"><code>token.FileSet</code></a> method <code>Files</code>
  1193  (which originally returned a channel of <code>*token.File</code>s) has been replaced
  1194  with the iterator <a href="/pkg/go/token/#FileSet.Iterate"><code>Iterate</code></a> that
  1195  accepts a function argument instead.
  1196  </p>
  1197  
  1198  <p>
  1199  In package <a href="/pkg/go/build/"><code>go/build</code></a>, the API
  1200  has been nearly completely replaced.
  1201  The package still computes Go package information
  1202  but it does not run the build: the <code>Cmd</code> and <code>Script</code>
  1203  types are gone.
  1204  (To build code, use the new
  1205  <a href="/cmd/go/"><code>go</code></a> command instead.)
  1206  The <code>DirInfo</code> type is now named
  1207  <a href="/pkg/go/build/#Package"><code>Package</code></a>.
  1208  <code>FindTree</code> and <code>ScanDir</code> are replaced by
  1209  <a href="/pkg/go/build/#Import"><code>Import</code></a>
  1210  and
  1211  <a href="/pkg/go/build/#ImportDir"><code>ImportDir</code></a>.
  1212  </p>
  1213  
  1214  <p>
  1215  <em>Updating</em>:
  1216  Code that uses packages in <code>go</code> will have to be updated by hand; the
  1217  compiler will reject incorrect uses. Templates used in conjunction with any of the
  1218  <code>go/doc</code> types may need manual fixes; the renamed fields will lead
  1219  to run-time errors.
  1220  </p>
  1221  
  1222  <h3 id="hash">The hash package</h3>
  1223  
  1224  <p>
  1225  In Go 1, the definition of <a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> includes
  1226  a new method, <code>BlockSize</code>.  This new method is used primarily in the
  1227  cryptographic libraries.
  1228  </p>
  1229  
  1230  <p>
  1231  The <code>Sum</code> method of the
  1232  <a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> interface now takes a
  1233  <code>[]byte</code> argument, to which the hash value will be appended.
  1234  The previous behavior can be recreated by adding a <code>nil</code> argument to the call.
  1235  </p>
  1236  
  1237  <p>
  1238  <em>Updating</em>:
  1239  Existing implementations of <code>hash.Hash</code> will need to add a
  1240  <code>BlockSize</code> method.  Hashes that process the input one byte at
  1241  a time can implement <code>BlockSize</code> to return 1.
  1242  Running <code>go</code> <code>fix</code> will update calls to the <code>Sum</code> methods of the various
  1243  implementations of <code>hash.Hash</code>.
  1244  </p>
  1245  
  1246  <p>
  1247  <em>Updating</em>:
  1248  Since the package's functionality is new, no updating is necessary.
  1249  </p>
  1250  
  1251  <h3 id="http">The http package</h3>
  1252  
  1253  <p>
  1254  In Go 1 the <a href="/pkg/net/http/"><code>http</code></a> package is refactored,
  1255  putting some of the utilities into a
  1256  <a href="/pkg/net/http/httputil/"><code>httputil</code></a> subdirectory.
  1257  These pieces are only rarely needed by HTTP clients.
  1258  The affected items are:
  1259  </p>
  1260  
  1261  <ul>
  1262  <li>ClientConn</li>
  1263  <li>DumpRequest</li>
  1264  <li>DumpRequestOut</li>
  1265  <li>DumpResponse</li>
  1266  <li>NewChunkedReader</li>
  1267  <li>NewChunkedWriter</li>
  1268  <li>NewClientConn</li>
  1269  <li>NewProxyClientConn</li>
  1270  <li>NewServerConn</li>
  1271  <li>NewSingleHostReverseProxy</li>
  1272  <li>ReverseProxy</li>
  1273  <li>ServerConn</li>
  1274  </ul>
  1275  
  1276  <p>
  1277  The <code>Request.RawURL</code> field has been removed; it was a
  1278  historical artifact.
  1279  </p>
  1280  
  1281  <p>
  1282  The <code>Handle</code> and <code>HandleFunc</code>
  1283  functions, and the similarly-named methods of <code>ServeMux</code>,
  1284  now panic if an attempt is made to register the same pattern twice.
  1285  </p>
  1286  
  1287  <p>
  1288  <em>Updating</em>:
  1289  Running <code>go</code> <code>fix</code> will update the few programs that are affected except for
  1290  uses of <code>RawURL</code>, which must be fixed by hand.
  1291  </p>
  1292  
  1293  <h3 id="image">The image package</h3>
  1294  
  1295  <p>
  1296  The <a href="/pkg/image/"><code>image</code></a> package has had a number of
  1297  minor changes, rearrangements and renamings.
  1298  </p>
  1299  
  1300  <p>
  1301  Most of the color handling code has been moved into its own package,
  1302  <a href="/pkg/image/color/"><code>image/color</code></a>.
  1303  For the elements that moved, a symmetry arises; for instance,
  1304  each pixel of an
  1305  <a href="/pkg/image/#RGBA"><code>image.RGBA</code></a>
  1306  is a
  1307  <a href="/pkg/image/color/#RGBA"><code>color.RGBA</code></a>.
  1308  </p>
  1309  
  1310  <p>
  1311  The old <code>image/ycbcr</code> package has been folded, with some
  1312  renamings, into the
  1313  <a href="/pkg/image/"><code>image</code></a>
  1314  and
  1315  <a href="/pkg/image/color/"><code>image/color</code></a>
  1316  packages.
  1317  </p>
  1318  
  1319  <p>
  1320  The old <code>image.ColorImage</code> type is still in the <code>image</code>
  1321  package but has been renamed
  1322  <a href="/pkg/image/#Uniform"><code>image.Uniform</code></a>,
  1323  while <code>image.Tiled</code> has been removed.
  1324  </p>
  1325  
  1326  <p>
  1327  This table lists the renamings.
  1328  </p>
  1329  
  1330  <table class="codetable" frame="border" summary="image renames">
  1331  <colgroup align="left" width="50%"></colgroup>
  1332  <colgroup align="left" width="50%"></colgroup>
  1333  <tr>
  1334  <th align="left">Old</th>
  1335  <th align="left">New</th>
  1336  </tr>
  1337  <tr>
  1338  <td colspan="2"><hr></td>
  1339  </tr>
  1340  <tr><td>image.Color</td> <td>color.Color</td></tr>
  1341  <tr><td>image.ColorModel</td> <td>color.Model</td></tr>
  1342  <tr><td>image.ColorModelFunc</td> <td>color.ModelFunc</td></tr>
  1343  <tr><td>image.PalettedColorModel</td> <td>color.Palette</td></tr>
  1344  <tr>
  1345  <td colspan="2"><hr></td>
  1346  </tr>
  1347  <tr><td>image.RGBAColor</td> <td>color.RGBA</td></tr>
  1348  <tr><td>image.RGBA64Color</td> <td>color.RGBA64</td></tr>
  1349  <tr><td>image.NRGBAColor</td> <td>color.NRGBA</td></tr>
  1350  <tr><td>image.NRGBA64Color</td> <td>color.NRGBA64</td></tr>
  1351  <tr><td>image.AlphaColor</td> <td>color.Alpha</td></tr>
  1352  <tr><td>image.Alpha16Color</td> <td>color.Alpha16</td></tr>
  1353  <tr><td>image.GrayColor</td> <td>color.Gray</td></tr>
  1354  <tr><td>image.Gray16Color</td> <td>color.Gray16</td></tr>
  1355  <tr>
  1356  <td colspan="2"><hr></td>
  1357  </tr>
  1358  <tr><td>image.RGBAColorModel</td> <td>color.RGBAModel</td></tr>
  1359  <tr><td>image.RGBA64ColorModel</td> <td>color.RGBA64Model</td></tr>
  1360  <tr><td>image.NRGBAColorModel</td> <td>color.NRGBAModel</td></tr>
  1361  <tr><td>image.NRGBA64ColorModel</td> <td>color.NRGBA64Model</td></tr>
  1362  <tr><td>image.AlphaColorModel</td> <td>color.AlphaModel</td></tr>
  1363  <tr><td>image.Alpha16ColorModel</td> <td>color.Alpha16Model</td></tr>
  1364  <tr><td>image.GrayColorModel</td> <td>color.GrayModel</td></tr>
  1365  <tr><td>image.Gray16ColorModel</td> <td>color.Gray16Model</td></tr>
  1366  <tr>
  1367  <td colspan="2"><hr></td>
  1368  </tr>
  1369  <tr><td>ycbcr.RGBToYCbCr</td> <td>color.RGBToYCbCr</td></tr>
  1370  <tr><td>ycbcr.YCbCrToRGB</td> <td>color.YCbCrToRGB</td></tr>
  1371  <tr><td>ycbcr.YCbCrColorModel</td> <td>color.YCbCrModel</td></tr>
  1372  <tr><td>ycbcr.YCbCrColor</td> <td>color.YCbCr</td></tr>
  1373  <tr><td>ycbcr.YCbCr</td> <td>image.YCbCr</td></tr>
  1374  <tr>
  1375  <td colspan="2"><hr></td>
  1376  </tr>
  1377  <tr><td>ycbcr.SubsampleRatio444</td> <td>image.YCbCrSubsampleRatio444</td></tr>
  1378  <tr><td>ycbcr.SubsampleRatio422</td> <td>image.YCbCrSubsampleRatio422</td></tr>
  1379  <tr><td>ycbcr.SubsampleRatio420</td> <td>image.YCbCrSubsampleRatio420</td></tr>
  1380  <tr>
  1381  <td colspan="2"><hr></td>
  1382  </tr>
  1383  <tr><td>image.ColorImage</td> <td>image.Uniform</td></tr>
  1384  </table>
  1385  
  1386  <p>
  1387  The image package's <code>New</code> functions
  1388  (<a href="/pkg/image/#NewRGBA"><code>NewRGBA</code></a>,
  1389  <a href="/pkg/image/#NewRGBA64"><code>NewRGBA64</code></a>, etc.)
  1390  take an <a href="/pkg/image/#Rectangle"><code>image.Rectangle</code></a> as an argument
  1391  instead of four integers.
  1392  </p>
  1393  
  1394  <p>
  1395  Finally, there are new predefined <code>color.Color</code> variables
  1396  <a href="/pkg/image/color/#Black"><code>color.Black</code></a>,
  1397  <a href="/pkg/image/color/#White"><code>color.White</code></a>,
  1398  <a href="/pkg/image/color/#Opaque"><code>color.Opaque</code></a>
  1399  and
  1400  <a href="/pkg/image/color/#Transparent"><code>color.Transparent</code></a>.
  1401  </p>
  1402  
  1403  <p>
  1404  <em>Updating</em>:
  1405  Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
  1406  </p>
  1407  
  1408  <h3 id="log_syslog">The log/syslog package</h3>
  1409  
  1410  <p>
  1411  In Go 1, the <a href="/pkg/log/syslog/#NewLogger"><code>syslog.NewLogger</code></a>
  1412  function returns an error as well as a <code>log.Logger</code>.
  1413  </p>
  1414  
  1415  <p>
  1416  <em>Updating</em>:
  1417  What little code is affected will be caught by the compiler and must be updated by hand.
  1418  </p>
  1419  
  1420  <h3 id="mime">The mime package</h3>
  1421  
  1422  <p>
  1423  In Go 1, the <a href="/pkg/mime/#FormatMediaType"><code>FormatMediaType</code></a> function
  1424  of the <code>mime</code> package has  been simplified to make it
  1425  consistent with
  1426  <a href="/pkg/mime/#ParseMediaType"><code>ParseMediaType</code></a>.
  1427  It now takes <code>"text/html"</code> rather than <code>"text"</code> and <code>"html"</code>.
  1428  </p>
  1429  
  1430  <p>
  1431  <em>Updating</em>:
  1432  What little code is affected will be caught by the compiler and must be updated by hand.
  1433  </p>
  1434  
  1435  <h3 id="net">The net package</h3>
  1436  
  1437  <p>
  1438  In Go 1, the various <code>SetTimeout</code>,
  1439  <code>SetReadTimeout</code>, and <code>SetWriteTimeout</code> methods
  1440  have been replaced with
  1441  <a href="/pkg/net/#IPConn.SetDeadline"><code>SetDeadline</code></a>,
  1442  <a href="/pkg/net/#IPConn.SetReadDeadline"><code>SetReadDeadline</code></a>, and
  1443  <a href="/pkg/net/#IPConn.SetWriteDeadline"><code>SetWriteDeadline</code></a>,
  1444  respectively.  Rather than taking a timeout value in nanoseconds that
  1445  apply to any activity on the connection, the new methods set an
  1446  absolute deadline (as a <code>time.Time</code> value) after which
  1447  reads and writes will time out and no longer block.
  1448  </p>
  1449  
  1450  <p>
  1451  There are also new functions
  1452  <a href="/pkg/net/#DialTimeout"><code>net.DialTimeout</code></a>
  1453  to simplify timing out dialing a network address and
  1454  <a href="/pkg/net/#ListenMulticastUDP"><code>net.ListenMulticastUDP</code></a>
  1455  to allow multicast UDP to listen concurrently across multiple listeners.
  1456  The <code>net.ListenMulticastUDP</code> function replaces the old
  1457  <code>JoinGroup</code> and <code>LeaveGroup</code> methods.
  1458  </p>
  1459  
  1460  <p>
  1461  <em>Updating</em>:
  1462  Code that uses the old methods will fail to compile and must be updated by hand.
  1463  The semantic change makes it difficult for the fix tool to update automatically.
  1464  </p>
  1465  
  1466  <h3 id="os">The os package</h3>
  1467  
  1468  <p>
  1469  The <code>Time</code> function has been removed; callers should use
  1470  the <a href="/pkg/time/#Time"><code>Time</code></a> type from the
  1471  <code>time</code> package.
  1472  </p>
  1473  
  1474  <p>
  1475  The <code>Exec</code> function has been removed; callers should use
  1476  <code>Exec</code> from the <code>syscall</code> package, where available.
  1477  </p>
  1478  
  1479  <p>
  1480  The <code>ShellExpand</code> function has been renamed to <a
  1481  href="/pkg/os/#ExpandEnv"><code>ExpandEnv</code></a>.
  1482  </p>
  1483  
  1484  <p>
  1485  The <a href="/pkg/os/#NewFile"><code>NewFile</code></a> function
  1486  now takes a <code>uintptr</code> fd, instead of an <code>int</code>.
  1487  The <a href="/pkg/os/#File.Fd"><code>Fd</code></a> method on files now
  1488  also returns a <code>uintptr</code>.
  1489  </p>
  1490  
  1491  <p>
  1492  There are no longer error constants such as <code>EINVAL</code>
  1493  in the <code>os</code> package, since the set of values varied with
  1494  the underlying operating system. There are new portable functions like
  1495  <a href="/pkg/os/#IsPermission"><code>IsPermission</code></a>
  1496  to test common error properties, plus a few new error values
  1497  with more Go-like names, such as
  1498  <a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
  1499  and
  1500  <a href="/pkg/os/#ErrNotExist"><code>ErrNotExist</code></a>.
  1501  </p>
  1502  
  1503  <p>
  1504  The <code>Getenverror</code> function has been removed. To distinguish
  1505  between a non-existent environment variable and an empty string,
  1506  use <a href="/pkg/os/#Environ"><code>os.Environ</code></a> or
  1507  <a href="/pkg/syscall/#Getenv"><code>syscall.Getenv</code></a>.
  1508  </p>
  1509  
  1510  
  1511  <p>
  1512  The <a href="/pkg/os/#Process.Wait"><code>Process.Wait</code></a> method has
  1513  dropped its option argument and the associated constants are gone
  1514  from the package.
  1515  Also, the function <code>Wait</code> is gone; only the method of
  1516  the <code>Process</code> type persists.
  1517  </p>
  1518  
  1519  <p>
  1520  The <code>Waitmsg</code> type returned by
  1521  <a href="/pkg/os/#Process.Wait"><code>Process.Wait</code></a>
  1522  has been replaced with a more portable
  1523  <a href="/pkg/os/#ProcessState"><code>ProcessState</code></a>
  1524  type with accessor methods to recover information about the
  1525  process.
  1526  Because of changes to <code>Wait</code>, the <code>ProcessState</code>
  1527  value always describes an exited process.
  1528  Portability concerns simplified the interface in other ways, but the values returned by the
  1529  <a href="/pkg/os/#ProcessState.Sys"><code>ProcessState.Sys</code></a> and
  1530  <a href="/pkg/os/#ProcessState.SysUsage"><code>ProcessState.SysUsage</code></a>
  1531  methods can be type-asserted to underlying system-specific data structures such as
  1532  <a href="/pkg/syscall/#WaitStatus"><code>syscall.WaitStatus</code></a> and
  1533  <a href="/pkg/syscall/#Rusage"><code>syscall.Rusage</code></a> on Unix.
  1534  </p>
  1535  
  1536  <p>
  1537  <em>Updating</em>:
  1538  Running <code>go</code> <code>fix</code> will drop a zero argument to <code>Process.Wait</code>.
  1539  All other changes will be caught by the compiler and must be updated by hand.
  1540  </p>
  1541  
  1542  <h4 id="os_fileinfo">The os.FileInfo type</h4>
  1543  
  1544  <p>
  1545  Go 1 redefines the <a href="/pkg/os/#FileInfo"><code>os.FileInfo</code></a> type,
  1546  changing it from a struct to an interface:
  1547  </p>
  1548  
  1549  <pre>
  1550      type FileInfo interface {
  1551          Name() string       // base name of the file
  1552          Size() int64        // length in bytes
  1553          Mode() FileMode     // file mode bits
  1554          ModTime() time.Time // modification time
  1555          IsDir() bool        // abbreviation for Mode().IsDir()
  1556          Sys() interface{}   // underlying data source (can return nil)
  1557      }
  1558  </pre>
  1559  
  1560  <p>
  1561  The file mode information has been moved into a subtype called
  1562  <a href="/pkg/os/#FileMode"><code>os.FileMode</code></a>,
  1563  a simple integer type with <code>IsDir</code>, <code>Perm</code>, and <code>String</code>
  1564  methods.
  1565  </p>
  1566  
  1567  <p>
  1568  The system-specific details of file modes and properties such as (on Unix)
  1569  i-number have been removed from <code>FileInfo</code> altogether.
  1570  Instead, each operating system's <code>os</code> package provides an
  1571  implementation of the <code>FileInfo</code> interface, which
  1572  has a <code>Sys</code> method that returns the
  1573  system-specific representation of file metadata.
  1574  For instance, to discover the i-number of a file on a Unix system, unpack
  1575  the <code>FileInfo</code> like this:
  1576  </p>
  1577  
  1578  <pre>
  1579      fi, err := os.Stat("hello.go")
  1580      if err != nil {
  1581          log.Fatal(err)
  1582      }
  1583      // Check that it's a Unix file.
  1584      unixStat, ok := fi.Sys().(*syscall.Stat_t)
  1585      if !ok {
  1586          log.Fatal("hello.go: not a Unix file")
  1587      }
  1588      fmt.Printf("file i-number: %d\n", unixStat.Ino)
  1589  </pre>
  1590  
  1591  <p>
  1592  Assuming (which is unwise) that <code>"hello.go"</code> is a Unix file,
  1593  the i-number expression could be contracted to
  1594  </p>
  1595  
  1596  <pre>
  1597      fi.Sys().(*syscall.Stat_t).Ino
  1598  </pre>
  1599  
  1600  <p>
  1601  The vast majority of uses of <code>FileInfo</code> need only the methods
  1602  of the standard interface.
  1603  </p>
  1604  
  1605  <p>
  1606  The <code>os</code> package no longer contains wrappers for the POSIX errors
  1607  such as <code>ENOENT</code>.
  1608  For the few programs that need to verify particular error conditions, there are
  1609  now the boolean functions
  1610  <a href="/pkg/os/#IsExist"><code>IsExist</code></a>,
  1611  <a href="/pkg/os/#IsNotExist"><code>IsNotExist</code></a>
  1612  and
  1613  <a href="/pkg/os/#IsPermission"><code>IsPermission</code></a>.
  1614  </p>
  1615  
  1616  {{code "/doc/progs/go1.go" `/os\.Open/` `/}/`}}
  1617  
  1618  <p>
  1619  <em>Updating</em>:
  1620  Running <code>go</code> <code>fix</code> will update code that uses the old equivalent of the current <code>os.FileInfo</code>
  1621  and <code>os.FileMode</code> API.
  1622  Code that needs system-specific file details will need to be updated by hand.
  1623  Code that uses the old POSIX error values from the <code>os</code> package
  1624  will fail to compile and will also need to be updated by hand.
  1625  </p>
  1626  
  1627  <h3 id="os_signal">The os/signal package</h3>
  1628  
  1629  <p>
  1630  The <code>os/signal</code> package in Go 1 replaces the
  1631  <code>Incoming</code> function, which returned a channel
  1632  that received all incoming signals,
  1633  with the selective <code>Notify</code> function, which asks
  1634  for delivery of specific signals on an existing channel.
  1635  </p>
  1636  
  1637  <p>
  1638  <em>Updating</em>:
  1639  Code must be updated by hand.
  1640  A literal translation of
  1641  </p>
  1642  <pre>
  1643  c := signal.Incoming()
  1644  </pre>
  1645  <p>
  1646  is
  1647  </p>
  1648  <pre>
  1649  c := make(chan os.Signal)
  1650  signal.Notify(c) // ask for all signals
  1651  </pre>
  1652  <p>
  1653  but most code should list the specific signals it wants to handle instead:
  1654  </p>
  1655  <pre>
  1656  c := make(chan os.Signal)
  1657  signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT)
  1658  </pre>
  1659  
  1660  <h3 id="path_filepath">The path/filepath package</h3>
  1661  
  1662  <p>
  1663  In Go 1, the <a href="/pkg/path/filepath/#Walk"><code>Walk</code></a> function of the
  1664  <code>path/filepath</code> package
  1665  has been changed to take a function value of type
  1666  <a href="/pkg/path/filepath/#WalkFunc"><code>WalkFunc</code></a>
  1667  instead of a <code>Visitor</code> interface value.
  1668  <code>WalkFunc</code> unifies the handling of both files and directories.
  1669  </p>
  1670  
  1671  <pre>
  1672      type WalkFunc func(path string, info os.FileInfo, err error) error
  1673  </pre>
  1674  
  1675  <p>
  1676  The <code>WalkFunc</code> function will be called even for files or directories that could not be opened;
  1677  in such cases the error argument will describe the failure.
  1678  If a directory's contents are to be skipped,
  1679  the function should return the value <a href="/pkg/path/filepath/#pkg-variables"><code>filepath.SkipDir</code></a>
  1680  </p>
  1681  
  1682  {{code "/doc/progs/go1.go" `/STARTWALK/` `/ENDWALK/`}}
  1683  
  1684  <p>
  1685  <em>Updating</em>:
  1686  The change simplifies most code but has subtle consequences, so affected programs
  1687  will need to be updated by hand.
  1688  The compiler will catch code using the old interface.
  1689  </p>
  1690  
  1691  <h3 id="regexp">The regexp package</h3>
  1692  
  1693  <p>
  1694  The <a href="/pkg/regexp/"><code>regexp</code></a> package has been rewritten.
  1695  It has the same interface but the specification of the regular expressions
  1696  it supports has changed from the old "egrep" form to that of
  1697  <a href="http://code.google.com/p/re2/">RE2</a>.
  1698  </p>
  1699  
  1700  <p>
  1701  <em>Updating</em>:
  1702  Code that uses the package should have its regular expressions checked by hand.
  1703  </p>
  1704  
  1705  <h3 id="runtime">The runtime package</h3>
  1706  
  1707  <p>
  1708  In Go 1, much of the API exported by package
  1709  <code>runtime</code> has been removed in favor of
  1710  functionality provided by other packages.
  1711  Code using the <code>runtime.Type</code> interface
  1712  or its specific concrete type implementations should
  1713  now use package <a href="/pkg/reflect/"><code>reflect</code></a>.
  1714  Code using <code>runtime.Semacquire</code> or <code>runtime.Semrelease</code>
  1715  should use channels or the abstractions in package <a href="/pkg/sync/"><code>sync</code></a>.
  1716  The <code>runtime.Alloc</code>, <code>runtime.Free</code>,
  1717  and <code>runtime.Lookup</code> functions, an unsafe API created for
  1718  debugging the memory allocator, have no replacement.
  1719  </p>
  1720  
  1721  <p>
  1722  Before, <code>runtime.MemStats</code> was a global variable holding
  1723  statistics about memory allocation, and calls to <code>runtime.UpdateMemStats</code>
  1724  ensured that it was up to date.
  1725  In Go 1, <code>runtime.MemStats</code> is a struct type, and code should use
  1726  <a href="/pkg/runtime/#ReadMemStats"><code>runtime.ReadMemStats</code></a>
  1727  to obtain the current statistics.
  1728  </p>
  1729  
  1730  <p>
  1731  The package adds a new function,
  1732  <a href="/pkg/runtime/#NumCPU"><code>runtime.NumCPU</code></a>, that returns the number of CPUs available
  1733  for parallel execution, as reported by the operating system kernel.
  1734  Its value can inform the setting of <code>GOMAXPROCS</code>.
  1735  The <code>runtime.Cgocalls</code> and <code>runtime.Goroutines</code> functions
  1736  have been renamed to <code>runtime.NumCgoCall</code> and <code>runtime.NumGoroutine</code>.
  1737  </p>
  1738  
  1739  <p>
  1740  <em>Updating</em>:
  1741  Running <code>go</code> <code>fix</code> will update code for the function renamings.
  1742  Other code will need to be updated by hand.
  1743  </p>
  1744  
  1745  <h3 id="strconv">The strconv package</h3>
  1746  
  1747  <p>
  1748  In Go 1, the
  1749  <a href="/pkg/strconv/"><code>strconv</code></a>
  1750  package has been significantly reworked to make it more Go-like and less C-like,
  1751  although <code>Atoi</code> lives on (it's similar to
  1752  <code>int(ParseInt(x, 10, 0))</code>, as does
  1753  <code>Itoa(x)</code> (<code>FormatInt(int64(x), 10)</code>).
  1754  There are also new variants of some of the functions that append to byte slices rather than
  1755  return strings, to allow control over allocation.
  1756  </p>
  1757  
  1758  <p>
  1759  This table summarizes the renamings; see the
  1760  <a href="/pkg/strconv/">package documentation</a>
  1761  for full details.
  1762  </p>
  1763  
  1764  <table class="codetable" frame="border" summary="strconv renames">
  1765  <colgroup align="left" width="50%"></colgroup>
  1766  <colgroup align="left" width="50%"></colgroup>
  1767  <tr>
  1768  <th align="left">Old call</th>
  1769  <th align="left">New call</th>
  1770  </tr>
  1771  <tr>
  1772  <td colspan="2"><hr></td>
  1773  </tr>
  1774  <tr><td>Atob(x)</td> <td>ParseBool(x)</td></tr>
  1775  <tr>
  1776  <td colspan="2"><hr></td>
  1777  </tr>
  1778  <tr><td>Atof32(x)</td> <td>ParseFloat(x, 32)§</td></tr>
  1779  <tr><td>Atof64(x)</td> <td>ParseFloat(x, 64)</td></tr>
  1780  <tr><td>AtofN(x, n)</td> <td>ParseFloat(x, n)</td></tr>
  1781  <tr>
  1782  <td colspan="2"><hr></td>
  1783  </tr>
  1784  <tr><td>Atoi(x)</td> <td>Atoi(x)</td></tr>
  1785  <tr><td>Atoi(x)</td> <td>ParseInt(x, 10, 0)§</td></tr>
  1786  <tr><td>Atoi64(x)</td> <td>ParseInt(x, 10, 64)</td></tr>
  1787  <tr>
  1788  <td colspan="2"><hr></td>
  1789  </tr>
  1790  <tr><td>Atoui(x)</td> <td>ParseUint(x, 10, 0)§</td></tr>
  1791  <tr><td>Atoui64(x)</td> <td>ParseUint(x, 10, 64)</td></tr>
  1792  <tr>
  1793  <td colspan="2"><hr></td>
  1794  </tr>
  1795  <tr><td>Btoi64(x, b)</td> <td>ParseInt(x, b, 64)</td></tr>
  1796  <tr><td>Btoui64(x, b)</td> <td>ParseUint(x, b, 64)</td></tr>
  1797  <tr>
  1798  <td colspan="2"><hr></td>
  1799  </tr>
  1800  <tr><td>Btoa(x)</td> <td>FormatBool(x)</td></tr>
  1801  <tr>
  1802  <td colspan="2"><hr></td>
  1803  </tr>
  1804  <tr><td>Ftoa32(x, f, p)</td> <td>FormatFloat(float64(x), f, p, 32)</td></tr>
  1805  <tr><td>Ftoa64(x, f, p)</td> <td>FormatFloat(x, f, p, 64)</td></tr>
  1806  <tr><td>FtoaN(x, f, p, n)</td> <td>FormatFloat(x, f, p, n)</td></tr>
  1807  <tr>
  1808  <td colspan="2"><hr></td>
  1809  </tr>
  1810  <tr><td>Itoa(x)</td> <td>Itoa(x)</td></tr>
  1811  <tr><td>Itoa(x)</td> <td>FormatInt(int64(x), 10)</td></tr>
  1812  <tr><td>Itoa64(x)</td> <td>FormatInt(x, 10)</td></tr>
  1813  <tr>
  1814  <td colspan="2"><hr></td>
  1815  </tr>
  1816  <tr><td>Itob(x, b)</td> <td>FormatInt(int64(x), b)</td></tr>
  1817  <tr><td>Itob64(x, b)</td> <td>FormatInt(x, b)</td></tr>
  1818  <tr>
  1819  <td colspan="2"><hr></td>
  1820  </tr>
  1821  <tr><td>Uitoa(x)</td> <td>FormatUint(uint64(x), 10)</td></tr>
  1822  <tr><td>Uitoa64(x)</td> <td>FormatUint(x, 10)</td></tr>
  1823  <tr>
  1824  <td colspan="2"><hr></td>
  1825  </tr>
  1826  <tr><td>Uitob(x, b)</td> <td>FormatUint(uint64(x), b)</td></tr>
  1827  <tr><td>Uitob64(x, b)</td> <td>FormatUint(x, b)</td></tr>
  1828  </table>
  1829  		
  1830  <p>
  1831  <em>Updating</em>:
  1832  Running <code>go</code> <code>fix</code> will update almost all code affected by the change.
  1833  <br>
  1834  § <code>Atoi</code> persists but <code>Atoui</code> and <code>Atof32</code> do not, so
  1835  they may require
  1836  a cast that must be added by hand; the <code>go</code> <code>fix</code> tool will warn about it.
  1837  </p>
  1838  
  1839  
  1840  <h3 id="templates">The template packages</h3>
  1841  
  1842  <p>
  1843  The <code>template</code> and <code>exp/template/html</code> packages have moved to 
  1844  <a href="/pkg/text/template/"><code>text/template</code></a> and
  1845  <a href="/pkg/html/template/"><code>html/template</code></a>.
  1846  More significant, the interface to these packages has been simplified.
  1847  The template language is the same, but the concept of "template set" is gone
  1848  and the functions and methods of the packages have changed accordingly,
  1849  often by elimination.
  1850  </p>
  1851  
  1852  <p>
  1853  Instead of sets, a <code>Template</code> object
  1854  may contain multiple named template definitions,
  1855  in effect constructing
  1856  name spaces for template invocation.
  1857  A template can invoke any other template associated with it, but only those
  1858  templates associated with it.
  1859  The simplest way to associate templates is to parse them together, something
  1860  made easier with the new structure of the packages.
  1861  </p>
  1862  
  1863  <p>
  1864  <em>Updating</em>:
  1865  The imports will be updated by fix tool.
  1866  Single-template uses will be otherwise be largely unaffected.
  1867  Code that uses multiple templates in concert will need to be updated by hand.
  1868  The <a href="/pkg/text/template/#pkg-examples">examples</a> in
  1869  the documentation for <code>text/template</code> can provide guidance.
  1870  </p>
  1871  
  1872  <h3 id="testing">The testing package</h3>
  1873  
  1874  <p>
  1875  The testing package has a type, <code>B</code>, passed as an argument to benchmark functions.
  1876  In Go 1, <code>B</code> has new methods, analogous to those of <code>T</code>, enabling
  1877  logging and failure reporting.
  1878  </p>
  1879  
  1880  {{code "/doc/progs/go1.go" `/func.*Benchmark/` `/^}/`}}
  1881  
  1882  <p>
  1883  <em>Updating</em>:
  1884  Existing code is unaffected, although benchmarks that use <code>println</code>
  1885  or <code>panic</code> should be updated to use the new methods.
  1886  </p>
  1887  
  1888  <h3 id="testing_script">The testing/script package</h3>
  1889  
  1890  <p>
  1891  The testing/script package has been deleted. It was a dreg.
  1892  </p>
  1893  
  1894  <p>
  1895  <em>Updating</em>:
  1896  No code is likely to be affected.
  1897  </p>
  1898  
  1899  <h3 id="unsafe">The unsafe package</h3>
  1900  
  1901  <p>
  1902  In Go 1, the functions
  1903  <code>unsafe.Typeof</code>, <code>unsafe.Reflect</code>,
  1904  <code>unsafe.Unreflect</code>, <code>unsafe.New</code>, and
  1905  <code>unsafe.NewArray</code> have been removed;
  1906  they duplicated safer functionality provided by
  1907  package <a href="/pkg/reflect/"><code>reflect</code></a>.
  1908  </p>
  1909  
  1910  <p>
  1911  <em>Updating</em>:
  1912  Code using these functions must be rewritten to use
  1913  package <a href="/pkg/reflect/"><code>reflect</code></a>.
  1914  The 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>
  1915  may be helpful as examples.
  1916  </p>
  1917  
  1918  <h3 id="url">The url package</h3>
  1919  
  1920  <p>
  1921  In Go 1 several fields from the <a href="/pkg/net/url/#URL"><code>url.URL</code></a> type
  1922  were removed or replaced.
  1923  </p>
  1924  
  1925  <p>
  1926  The <a href="/pkg/net/url/#URL.String"><code>String</code></a> method now
  1927  predictably rebuilds an encoded URL string using all of <code>URL</code>'s
  1928  fields as necessary. The resulting string will also no longer have
  1929  passwords escaped.
  1930  </p>
  1931  
  1932  <p>
  1933  The <code>Raw</code> field has been removed. In most cases the <code>String</code>
  1934  method may be used in its place.
  1935  </p>
  1936  
  1937  <p>
  1938  The old <code>RawUserinfo</code> field is replaced by the <code>User</code>
  1939  field, of type <a href="/pkg/net/url/#Userinfo"><code>*net.Userinfo</code></a>.
  1940  Values of this type may be created using the new <a href="/pkg/net/url/#User"><code>net.User</code></a>
  1941  and <a href="/pkg/net/url/#UserPassword"><code>net.UserPassword</code></a>
  1942  functions. The <code>EscapeUserinfo</code> and <code>UnescapeUserinfo</code>
  1943  functions are also gone.
  1944  </p>
  1945  
  1946  <p>
  1947  The <code>RawAuthority</code> field has been removed. The same information is
  1948  available in the <code>Host</code> and <code>User</code> fields.
  1949  </p>
  1950  
  1951  <p>
  1952  The <code>RawPath</code> field and the <code>EncodedPath</code> method have
  1953  been removed. The path information in rooted URLs (with a slash following the
  1954  schema) is now available only in decoded form in the <code>Path</code> field.
  1955  Occasionally, the encoded data may be required to obtain information that
  1956  was lost in the decoding process. These cases must be handled by accessing
  1957  the data the URL was built from.
  1958  </p>
  1959  
  1960  <p>
  1961  URLs with non-rooted paths, such as <code>"mailto:dev@golang.org?subject=Hi"</code>,
  1962  are also handled differently. The <code>OpaquePath</code> boolean field has been
  1963  removed and a new <code>Opaque</code> string field introduced to hold the encoded
  1964  path for such URLs. In Go 1, the cited URL parses as:
  1965  </p>
  1966  
  1967  <pre>
  1968      URL{
  1969          Scheme: "mailto",
  1970          Opaque: "dev@golang.org",
  1971          RawQuery: "subject=Hi",
  1972      }
  1973  </pre>
  1974  
  1975  <p>
  1976  A new <a href="/pkg/net/url/#URL.RequestURI"><code>RequestURI</code></a> method was
  1977  added to <code>URL</code>.
  1978  </p>
  1979  
  1980  <p>
  1981  The <code>ParseWithReference</code> function has been renamed to <code>ParseWithFragment</code>.
  1982  </p>
  1983  
  1984  <p>
  1985  <em>Updating</em>:
  1986  Code that uses the old fields will fail to compile and must be updated by hand.
  1987  The semantic changes make it difficult for the fix tool to update automatically.
  1988  </p>
  1989  
  1990  <h2 id="cmd_go">The go command</h2>
  1991  
  1992  <p>
  1993  Go 1 introduces the <a href="/cmd/go/">go command</a>, a tool for fetching,
  1994  building, and installing Go packages and commands. The <code>go</code> command
  1995  does away with makefiles, instead using Go source code to find dependencies and
  1996  determine build conditions. Most existing Go programs will no longer require
  1997  makefiles to be built.
  1998  </p>
  1999  
  2000  <p>
  2001  See <a href="/doc/code.html">How to Write Go Code</a> for a primer on the
  2002  <code>go</code> command and the <a href="/cmd/go/">go command documentation</a>
  2003  for the full details.
  2004  </p>
  2005  
  2006  <p>
  2007  <em>Updating</em>:
  2008  Projects that depend on the Go project's old makefile-based build
  2009  infrastructure (<code>Make.pkg</code>, <code>Make.cmd</code>, and so on) should
  2010  switch to using the <code>go</code> command for building Go code and, if
  2011  necessary, rewrite their makefiles to perform any auxiliary build tasks.
  2012  </p>
  2013  
  2014  <h2 id="cmd_cgo">The cgo command</h2>
  2015  
  2016  <p>
  2017  In Go 1, the <a href="/cmd/cgo">cgo command</a>
  2018  uses a different <code>_cgo_export.h</code>
  2019  file, which is generated for packages containing <code>//export</code> lines.
  2020  The <code>_cgo_export.h</code> file now begins with the C preamble comment,
  2021  so that exported function definitions can use types defined there.
  2022  This has the effect of compiling the preamble multiple times, so a
  2023  package using <code>//export</code> must not put function definitions
  2024  or variable initializations in the C preamble.
  2025  </p>
  2026  
  2027  <h2 id="releases">Packaged releases</h2>
  2028  
  2029  <p>
  2030  One of the most significant changes associated with Go 1 is the availability
  2031  of prepackaged, downloadable distributions.
  2032  They are available for many combinations of architecture and operating system
  2033  (including Windows) and the list will grow.
  2034  Installation details are described on the
  2035  <a href="/doc/install">Getting Started</a> page, while
  2036  the distributions themselves are listed on the
  2037  <a href="http://code.google.com/p/go/downloads/list">downloads page</a>.