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