You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2237 lines
76 KiB

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