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