go1.2.html 34 KB

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