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 |
| 77 | var csend chan<- int = c |
| 78 | var crecv <-chan int = c |
| 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>: |
| 245 | Gofix will convert expressions of the form <code>m[k] = ignored, |
| 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 |
| 248 | <code>false</code> refers to the predefined boolean constant. Gofix |
| 249 | will flag other uses of the syntax for inspection by the programmer. |
| 250 | </p> |
| 251 | |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 252 | <h3 id="iteration">Iterating in maps</h3> |
| 253 | |
| 254 | <p> |
| 255 | In Go 1, the order in which elements are visited when iterating |
| 256 | over a map using a <code>for</code> <code>range</code> statement |
| 257 | is defined to be unpredictable, even if the same loop is run multiple |
| 258 | times with the same map. |
| 259 | Code should not assume that the elements are visited in any particular order. |
| 260 | </p> |
| 261 | |
| 262 | <pre><!--{{code "progs/go1.go" `/Sunday/` `/^ }/`}} |
Andrew Gerrand | 468e692 | 2012-01-09 20:05:34 +1100 | [diff] [blame] | 263 | --> m := map[string]int{"Sunday": 0, "Monday": 1} |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 264 | for name, value := range m { |
| 265 | // This loop should not assume Sunday will be visited first. |
| 266 | f(name, value) |
Andrew Gerrand | 5353e1e | 2012-01-06 09:20:31 +1100 | [diff] [blame] | 267 | }</pre> |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 268 | |
| 269 | <p> |
| 270 | <em>Updating</em>: |
| 271 | This is one change where tools cannot help. Most existing code |
| 272 | will be unaffected, but some programs may break or misbehave; we |
| 273 | recommend manual checking of all range statements over maps to |
| 274 | verify they do not depend on iteration order. There were a few such |
| 275 | examples in the standard repository; they have been fixed. |
| 276 | Note that it was already incorrect to depend on the iteration order, which |
| 277 | was unspecified. This change codifies the unpredictability. |
| 278 | </p> |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 279 | |
| 280 | <h3 id="multiple_assignment">Multiple assignment</h3> |
| 281 | |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 282 | <p> |
| 283 | Go 1 fully specifies the evaluation order in multiple assignment |
| 284 | statements. In particular, if the left-hand side of the assignment |
| 285 | statement contains expressions that require evaluation, such as |
| 286 | function calls or array indexing operations, these will all be done |
| 287 | using the usual left-to-right rule before any variables are assigned |
| 288 | their value. Once everything is evaluated, the actual assignments |
| 289 | proceed in left-to-right order. |
| 290 | </p> |
| 291 | |
| 292 | <p> |
| 293 | These examples illustrate the behavior. |
| 294 | </p> |
| 295 | |
| 296 | <pre><!--{{code "progs/go1.go" `/sa :=/` `/then sc.0. = 2/`}} |
Andrew Gerrand | 468e692 | 2012-01-09 20:05:34 +1100 | [diff] [blame] | 297 | --> sa := []int{1, 2, 3} |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 298 | i := 0 |
| 299 | i, sa[i] = 1, 2 // sets i = 1, sa[0] = 2 |
| 300 | |
| 301 | sb := []int{1, 2, 3} |
| 302 | j := 0 |
| 303 | sb[j], j = 2, 1 // sets sb[0] = 2, j = 1 |
| 304 | |
| 305 | sc := []int{1, 2, 3} |
Andrew Gerrand | 5353e1e | 2012-01-06 09:20:31 +1100 | [diff] [blame] | 306 | 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] | 307 | |
Rob Pike | 2e338fa | 2011-12-09 08:31:57 -0800 | [diff] [blame] | 308 | <p> |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 309 | <em>Updating</em>: |
| 310 | This is one change where tools cannot help, but breakage is unlikely. |
| 311 | No code in the standard repository was broken by this change, and code |
| 312 | that depended on the previous unspecified behavior was already incorrect. |
| 313 | </p> |
| 314 | |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 315 | <h3 id="shadowing">Returns and shadowed variables</h3> |
| 316 | |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 317 | <p> |
| 318 | A shadowed variable is one that has the same name as another variable in an inner scope. |
| 319 | In functions with named return values, |
| 320 | 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. |
| 321 | (It isn't part of the specification, because this is one area we are still exploring; |
| 322 | the situation is analogous to the compilers rejecting functions that do not end with an explicit return statement.) |
| 323 | </p> |
| 324 | |
| 325 | <p> |
| 326 | This function implicitly returns a shadowed return value and will be rejected by the compiler: |
| 327 | </p> |
| 328 | |
| 329 | <pre> |
| 330 | func Bug() (i, j, k int) { |
| 331 | for i = 0; i < 5; i++ { |
| 332 | for j := 0; j < 5; j++ { // Redeclares j. |
| 333 | k += i*j |
| 334 | if k > 100 { |
| 335 | return // Rejected: j is shadowed here. |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | return // OK: j is not shadowed here. |
| 340 | } |
| 341 | </pre> |
| 342 | |
| 343 | <p> |
| 344 | <em>Updating</em>: |
| 345 | Code that shadows return values in this way will be rejected by the compiler and will need to be fixed by hand. |
| 346 | The few cases that arose in the standard repository were mostly bugs. |
| 347 | </p> |
| 348 | |
| 349 | <h3 id="unexported">Copying structs with unexported fields</h3> |
| 350 | |
Rob Pike | 2e338fa | 2011-12-09 08:31:57 -0800 | [diff] [blame] | 351 | <p> |
| 352 | Go 1 relaxes the rules about accessing structs with unexported (lower-case) fields, |
| 353 | permitting a client package to assign (and therefore copy) such a struct. |
| 354 | Of course, the client package still cannot access such fields individually. |
| 355 | </p> |
| 356 | |
| 357 | <p> |
| 358 | As an example, if package <code>p</code> includes the definitions, |
| 359 | </p> |
| 360 | |
| 361 | <pre> |
| 362 | type Struct struct { |
| 363 | Public int |
| 364 | secret int |
| 365 | } |
| 366 | func NewStruct(a int) Struct { // Note: not a pointer. |
| 367 | return Struct{a, f(a)} |
| 368 | } |
| 369 | func (s Struct) String() string { |
| 370 | return fmt.Sprintf("{%d (secret %d)}", s.Public, s.secret) |
| 371 | } |
| 372 | </pre> |
| 373 | |
| 374 | <p> |
| 375 | a package that imports <code>p</code> can assign and copy values of type |
| 376 | <code>p.Struct</code> at will. |
| 377 | Behind the scenes the unexported fields will be assigned and copied just |
| 378 | as if they were exported, |
| 379 | but the client code will never be aware of them. The code |
| 380 | </p> |
| 381 | |
| 382 | <pre> |
| 383 | import "p" |
| 384 | |
| 385 | myStruct := p.NewStruct(23) |
| 386 | copyOfMyStruct := myStruct |
| 387 | fmt.Println(myStruct, copyOfMyStruct) |
| 388 | </pre> |
| 389 | |
| 390 | <p> |
| 391 | will show that the secret field of the struct has been copied to the new value. |
| 392 | </p> |
| 393 | |
| 394 | <p> |
| 395 | <em>Updating</em>: |
| 396 | This is a new feature, so existing code needs no changes. |
| 397 | </p> |
| 398 | |
Rob Pike | 9d59c40 | 2011-12-08 11:35:28 -0800 | [diff] [blame] | 399 | <h3 id="equality">Equality of structs and arrays</h3> |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 400 | |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 401 | <p> |
| 402 | Go 1 defines equality and inequality (<code>==</code> and |
| 403 | <code>!=</code>) for struct and array values, respectively, provided |
| 404 | the elements of the data structures can themselves be compared. |
| 405 | That is, if equality is defined for all the fields of a struct (or |
| 406 | an array element), then it is defined for the struct (or array). |
| 407 | </p> |
| 408 | |
| 409 | <p> |
| 410 | As a result, structs and arrays can now be used as map keys: |
| 411 | </p> |
| 412 | |
| 413 | <pre><!--{{code "progs/go1.go" `/type Day struct/` `/Printf/`}} |
Andrew Gerrand | 468e692 | 2012-01-09 20:05:34 +1100 | [diff] [blame] | 414 | --> type Day struct { |
Rob Pike | 5fa18e1 | 2011-12-12 21:08:03 -0800 | [diff] [blame] | 415 | long string |
| 416 | short string |
| 417 | } |
| 418 | Christmas := Day{"Christmas", "XMas"} |
| 419 | Thanksgiving := Day{"Thanksgiving", "Turkey"} |
| 420 | holiday := map[Day]bool{ |
| 421 | Christmas: true, |
| 422 | Thanksgiving: true, |
| 423 | } |
Andrew Gerrand | 5353e1e | 2012-01-06 09:20:31 +1100 | [diff] [blame] | 424 | fmt.Printf("Christmas is a holiday: %t\n", holiday[Christmas])</pre> |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 425 | |
| 426 | <p> |
| 427 | Note that equality is still undefined for slices, for which the |
| 428 | calculation is in general infeasible. Also note that the ordered |
| 429 | comparison operators (<code><</code> <code><=</code> |
| 430 | <code>></code> <code>>=</code>) are still undefined for |
| 431 | structs and arrays. |
| 432 | |
| 433 | <p> |
| 434 | <em>Updating</em>: |
| 435 | This is a new feature, so existing code needs no changes. |
| 436 | </p> |
| 437 | |
| 438 | <h3 id="funcs">Function and map equality</h3> |
| 439 | |
| 440 | <p> |
| 441 | Go 1 disallows checking for equality of functions and maps, |
| 442 | respectively, except to compare them directly to <code>nil</code>. |
| 443 | </p> |
| 444 | |
| 445 | <p> |
| 446 | <em>Updating</em>: |
| 447 | Existing code that depends on function or map equality will be |
| 448 | rejected by the compiler and will need to be fixed by hand. |
| 449 | Few programs will be affected, but the fix may require some |
| 450 | redesign. |
| 451 | </p> |
| 452 | |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 453 | <h2 id="packages">The package hierarchy</h2> |
| 454 | |
| 455 | <p> |
| 456 | This section describes how the packages have been rearranged in Go 1. |
| 457 | Some have moved, some have been renamed, some have been deleted. |
| 458 | New packages are described in later sections. |
| 459 | </p> |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 460 | |
Rob Pike | 9d59c40 | 2011-12-08 11:35:28 -0800 | [diff] [blame] | 461 | <h3 id="hierarchy">The package hierarchy</h3> |
| 462 | |
| 463 | <p> |
| 464 | Go 1 has a rearranged package hierarchy that groups related items |
| 465 | into subdirectories. For instance, <code>utf8</code> and |
| 466 | <code>utf16</code> now occupy subdirectories of <code>unicode</code>. |
| 467 | Also, <a href="#subrepo">some packages</a> have moved into |
| 468 | subrepositories of |
| 469 | <a href="http://code.google.com/p/go"><code>code.google.com/p/go</code></a> |
| 470 | while <a href="#deleted">others</a> have been deleted outright. |
| 471 | </p> |
| 472 | |
| 473 | <table class="codetable" frame="border" summary="Moved packages"> |
| 474 | <colgroup align="left" width="60%"></colgroup> |
| 475 | <colgroup align="left" width="40%"></colgroup> |
| 476 | <tr> |
| 477 | <th align="left">Old path</th> |
| 478 | <th align="left">New path</th> |
| 479 | </tr> |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 480 | <tr> |
| 481 | <td colspan="2"><hr></td> |
| 482 | </tr> |
Rob Pike | 136c04f | 2011-12-08 16:39:05 -0800 | [diff] [blame] | 483 | <tr><td>asn1</td> <td>encoding/asn1</td></tr> |
Rob Pike | 9d59c40 | 2011-12-08 11:35:28 -0800 | [diff] [blame] | 484 | <tr><td>csv</td> <td>encoding/csv</td></tr> |
| 485 | <tr><td>gob</td> <td>encoding/gob</td></tr> |
| 486 | <tr><td>json</td> <td>encoding/json</td></tr> |
| 487 | <tr><td>xml</td> <td>encoding/xml</td></tr> |
| 488 | <tr> |
| 489 | <td colspan="2"><hr></td> |
| 490 | </tr> |
| 491 | <tr><td>exp/template/html</td> <td>html/template</td></tr> |
| 492 | <tr> |
| 493 | <td colspan="2"><hr></td> |
| 494 | </tr> |
| 495 | <tr><td>big</td> <td>math/big</td></tr> |
| 496 | <tr><td>cmath</td> <td>math/cmplx</td></tr> |
| 497 | <tr><td>rand</td> <td>math/rand</td></tr> |
| 498 | <tr> |
| 499 | <td colspan="2"><hr></td> |
| 500 | </tr> |
| 501 | <tr><td>http</td> <td>net/http</td></tr> |
| 502 | <tr><td>http/cgi</td> <td>net/http/cgi</td></tr> |
| 503 | <tr><td>http/fcgi</td> <td>net/http/fcgi</td></tr> |
| 504 | <tr><td>http/httptest</td> <td>net/http/httptest</td></tr> |
| 505 | <tr><td>http/pprof</td> <td>net/http/pprof</td></tr> |
| 506 | <tr><td>mail</td> <td>net/mail</td></tr> |
| 507 | <tr><td>rpc</td> <td>net/rpc</td></tr> |
| 508 | <tr><td>rpc/jsonrpc</td> <td>net/rpc/jsonrpc</td></tr> |
| 509 | <tr><td>smtp</td> <td>net/smtp</td></tr> |
| 510 | <tr><td>url</td> <td>net/url</td></tr> |
| 511 | <tr> |
| 512 | <td colspan="2"><hr></td> |
| 513 | </tr> |
| 514 | <tr><td>exec</td> <td>os/exec</td></tr> |
| 515 | <tr> |
| 516 | <td colspan="2"><hr></td> |
| 517 | </tr> |
| 518 | <tr><td>scanner</td> <td>text/scanner</td></tr> |
| 519 | <tr><td>tabwriter</td> <td>text/tabwriter</td></tr> |
| 520 | <tr><td>template</td> <td>text/template</td></tr> |
| 521 | <tr><td>template/parse</td> <td>text/template/parse</td></tr> |
| 522 | <tr> |
| 523 | <td colspan="2"><hr></td> |
| 524 | </tr> |
| 525 | <tr><td>utf8</td> <td>unicode/utf8</td></tr> |
| 526 | <tr><td>utf16</td> <td>unicode/utf16</td></tr> |
| 527 | </table> |
| 528 | |
| 529 | <p> |
| 530 | Note that the package names for the old <code>cmath</code> and |
| 531 | <code>exp/template/html</code> packages have changed to <code>cmplx</code> |
| 532 | and <code>template</code>. |
| 533 | </p> |
| 534 | |
| 535 | <p> |
| 536 | <em>Updating</em>: |
| 537 | Gofix will update all imports and package renames for packages that |
| 538 | remain inside the standard repository. Programs that import packages |
| 539 | that are no longer in the standard repository will need to be edited |
| 540 | by hand. |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 541 | <br> |
| 542 | <font color="red">TODO: gofix should warn about deletions.</font> |
Rob Pike | 9d59c40 | 2011-12-08 11:35:28 -0800 | [diff] [blame] | 543 | </p> |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 544 | |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 545 | <h3 id="exp">The package tree exp</h3> |
| 546 | |
| 547 | <p> |
| 548 | Because they are not standardized, the packages under the <code>exp</code> directory will not be available in the |
| 549 | standard Go 1 release distributions, although they will be available in source code form |
| 550 | in <a href="http://code.google.com/p/go/">the repository</a> for |
| 551 | developers who wish to use them. |
| 552 | </p> |
| 553 | |
| 554 | <p> |
| 555 | Several packages have moved under <code>exp</code> at the time of Go 1's release: |
| 556 | </p> |
| 557 | |
| 558 | <ul> |
| 559 | <li><code>ebnf</code></li> |
| 560 | <li><code>go/types</code></li> |
| 561 | <li><code>http/spdy</code></li> |
| 562 | </ul> |
| 563 | |
| 564 | <p> |
Rob Pike | 531ded9 | 2012-01-20 15:38:03 -0800 | [diff] [blame] | 565 | All these packages are available under the same names, with the prefix <code>exp/</code>: <code>exp/ebnf</code> etc. |
| 566 | </p> |
| 567 | |
| 568 | <p> |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 569 | Also, the <code>utf8.String</code> type has been moved to its own package, <code>exp/utf8string</code>. |
| 570 | </p> |
| 571 | |
| 572 | <p> |
Rob Pike | 531ded9 | 2012-01-20 15:38:03 -0800 | [diff] [blame] | 573 | Finally, the <code>gotype</code> command now resides in <code>exp/gotype</code>, while |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 574 | <code>ebnflint</code> is now in <code>exp/ebnflint</code> |
| 575 | </p> |
| 576 | |
| 577 | <p> |
| 578 | <em>Updating</em>: |
| 579 | Code that uses packages in <code>exp</code> will need to be updated by hand, |
| 580 | or else compiled from an installation that has <code>exp</code> available. |
| 581 | Gofix or the compiler will complain about such uses. |
| 582 | <br> |
| 583 | <font color="red">TODO: gofix should warn about such uses.</font> |
| 584 | </p> |
| 585 | |
| 586 | <h3 id="old">The package tree old</h3> |
| 587 | |
| 588 | <p> |
| 589 | Because they are deprecated, the packages under the <code>old</code> directory will not be available in the |
| 590 | standard Go 1 release distributions, although they will be available in source code form for |
| 591 | developers who wish to use them. |
| 592 | </p> |
| 593 | |
| 594 | <p> |
| 595 | The packages in their new locations are: |
| 596 | </p> |
| 597 | |
| 598 | <ul> |
| 599 | <li><code>old/netchan</code></li> |
| 600 | <li><code>old/regexp</code></li> |
| 601 | <li><code>old/template</code></li> |
| 602 | </ul> |
| 603 | |
| 604 | <p> |
| 605 | <em>Updating</em>: |
| 606 | Code that uses packages now in <code>old</code> will need to be updated by hand, |
| 607 | or else compiled from an installation that has <code>old</code> available. |
| 608 | Gofix will warn about such uses. |
| 609 | <br> |
| 610 | <font color="red">TODO: gofix should warn about such uses.</font> |
| 611 | </p> |
| 612 | |
| 613 | <h3 id="deleted">Deleted packages</h3> |
| 614 | |
| 615 | <p> |
| 616 | Go 1 deletes several packages outright: |
| 617 | </p> |
| 618 | |
| 619 | <ul> |
| 620 | <li><code>container/vector</code></li> |
| 621 | <li><code>exp/datafmt</code></li> |
| 622 | <li><code>go/typechecker</code></li> |
| 623 | <li><code>try</code></li> |
| 624 | </ul> |
| 625 | |
| 626 | <p> |
| 627 | and also the command <code>gotry</code>. |
| 628 | </p> |
| 629 | |
| 630 | <p> |
| 631 | <em>Updating</em>: |
| 632 | Code that uses <code>container/vector</code> should be updated to use |
| 633 | slices directly. See |
| 634 | <a href="http://code.google.com/p/go-wiki/wiki/SliceTricks">the Go |
| 635 | Language Community Wiki</a> for some suggestions. |
| 636 | Code that uses the other packages (there should be almost zero) will need to be rethought. |
| 637 | <br> |
| 638 | <font color="red">TODO: gofix should warn such uses.</font> |
| 639 | </p> |
| 640 | |
| 641 | <h3 id="subrepo">Packages moving to subrepositories</h3> |
| 642 | |
Rob Pike | 7eaad5e | 2012-01-25 13:29:25 -0800 | [diff] [blame] | 643 | <p> |
| 644 | Go 1 has moved a number of packages into sub-repositories of |
| 645 | <a href="http://code.google.com/p/go/">the main Go repository</a>. |
| 646 | This table lists the old and new import paths: |
| 647 | |
| 648 | <table class="codetable" frame="border" summary="Sub-repositories"> |
| 649 | <colgroup align="left" width="40%"></colgroup> |
| 650 | <colgroup align="left" width="60%"></colgroup> |
| 651 | <tr> |
| 652 | <th align="left">Old</th> |
| 653 | <th align="left">New</th> |
| 654 | </tr> |
| 655 | <tr> |
| 656 | <td colspan="2"><hr></td> |
| 657 | </tr> |
| 658 | <tr><td>crypto/bcrypt</td> <td>code.google.com/p/go.crypto/bcrypt</tr> |
| 659 | <tr><td>crypto/blowfish</td> <td>code.google.com/p/go.crypto/blowfish</tr> |
| 660 | <tr><td>crypto/cast5</td> <td>code.google.com/p/go.crypto/cast5</tr> |
| 661 | <tr><td>crypto/md4</td> <td>code.google.com/p/go.crypto/md4</tr> |
| 662 | <tr><td>crypto/ocsp</td> <td>code.google.com/p/go.crypto/ocsp</tr> |
| 663 | <tr><td>crypto/openpgp</td> <td>code.google.com/p/go.crypto/openpgp</tr> |
| 664 | <tr><td>crypto/openpgp/armor</td> <td>code.google.com/p/go.crypto/openpgp/armor</tr> |
| 665 | <tr><td>crypto/openpgp/elgamal</td> <td>code.google.com/p/go.crypto/openpgp/elgamal</tr> |
| 666 | <tr><td>crypto/openpgp/errors</td> <td>code.google.com/p/go.crypto/openpgp/errors</tr> |
| 667 | <tr><td>crypto/openpgp/packet</td> <td>code.google.com/p/go.crypto/openpgp/packet</tr> |
| 668 | <tr><td>crypto/openpgp/s2k</td> <td>code.google.com/p/go.crypto/openpgp/s2k</tr> |
| 669 | <tr><td>crypto/ripemd160</td> <td>code.google.com/p/go.crypto/ripemd160</tr> |
| 670 | <tr><td>crypto/twofish</td> <td>code.google.com/p/go.crypto/twofish</tr> |
| 671 | <tr><td>crypto/xtea</td> <td>code.google.com/p/go.crypto/xtea</tr> |
| 672 | <tr><td>exp/ssh</td> <td>code.google.com/p/go.crypto/ssh</tr> |
| 673 | <tr> |
| 674 | <td colspan="2"><hr></td> |
| 675 | </tr> |
| 676 | <tr><td>net/dict</td> <td>code.google.com/p/go.net/dict</tr> |
| 677 | <tr><td>net/websocket</td> <td>code.google.com/p/go.net/websocket</tr> |
| 678 | <tr><td>exp/spdy</td> <td>code.google.com/p/go.net/spdy</tr> |
| 679 | <tr> |
| 680 | <td colspan="2"><hr></td> |
| 681 | </tr> |
| 682 | <tr><td>encoding/git85</td> <td>code.google.com/p/go.codereview/git85</tr> |
| 683 | <tr><td>patch</td> <td>code.google.com/p/go.codereview/patch</tr> |
| 684 | </table> |
| 685 | |
| 686 | <p> |
| 687 | <em>Updating</em>: |
| 688 | Gofix will update imports of these packages to use the new import paths. |
| 689 | Installations that depend on these packages will need to install them using |
| 690 | a <code>go install</code> command. |
| 691 | </p> |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 692 | |
| 693 | <h2 id="major">Major changes to the library</h2> |
| 694 | |
| 695 | <p> |
| 696 | This section describes significant changes to the core libraries, the ones that |
| 697 | affect the most programs. |
| 698 | </p> |
| 699 | |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 700 | <h3 id="errors">The error type and errors package</h3> |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 701 | |
Rob Pike | ebdcbf1 | 2011-12-12 12:26:56 -0800 | [diff] [blame] | 702 | <p> |
| 703 | As mentioned above, Go 1 introduces a new built-in interface type called <code>error</code>. |
| 704 | Its intent is to replace the old <code>os.Error</code> type with a more central concept. |
| 705 | So the widely-used <code>String</code> method does not cause accidental satisfaction |
| 706 | of the <code>error</code> interface, the <code>error</code> interface uses instead |
| 707 | the name <code>Error</code> for that method: |
| 708 | </p> |
| 709 | |
| 710 | <pre> |
| 711 | type error interface { |
| 712 | Error() string |
| 713 | } |
| 714 | </pre> |
| 715 | |
| 716 | <p> |
| 717 | The <code>fmt</code> library automatically invokes <code>Error</code>, as it already |
| 718 | does for <code>String</code>, for easy printing of error values. |
| 719 | </p> |
| 720 | |
| 721 | <pre><!--{{code "progs/go1.go" `/START ERROR EXAMPLE/` `/END ERROR EXAMPLE/`}} |
| 722 | -->type SyntaxError struct { |
| 723 | File string |
| 724 | Line int |
| 725 | Message string |
| 726 | } |
| 727 | |
| 728 | func (se *SyntaxError) Error() string { |
| 729 | return fmt.Sprintf("%s:%d: %s", se.File, se.Line, se.Message) |
Andrew Gerrand | 5353e1e | 2012-01-06 09:20:31 +1100 | [diff] [blame] | 730 | }</pre> |
Rob Pike | ebdcbf1 | 2011-12-12 12:26:56 -0800 | [diff] [blame] | 731 | |
| 732 | <p> |
| 733 | All standard packages have been updated to use the new interface; the old <code>os.Error</code> is gone. |
| 734 | </p> |
| 735 | |
| 736 | <p> |
| 737 | A new package, <a href="/pkg/errors/"><code>errors</code></a>, contains the function |
| 738 | </p> |
| 739 | |
| 740 | <pre> |
| 741 | func New(text string) error |
| 742 | </pre> |
| 743 | |
| 744 | <p> |
| 745 | to turn a string into an error. It replaces the old <code>os.NewError</code>. |
| 746 | </p> |
| 747 | |
| 748 | <pre><!--{{code "progs/go1.go" `/ErrSyntax/`}} |
Andrew Gerrand | 468e692 | 2012-01-09 20:05:34 +1100 | [diff] [blame] | 749 | --> var ErrSyntax = errors.New("syntax error")</pre> |
Rob Pike | ebdcbf1 | 2011-12-12 12:26:56 -0800 | [diff] [blame] | 750 | |
| 751 | <p> |
| 752 | <em>Updating</em>: |
| 753 | Gofix will update almost all code affected by the change. |
| 754 | Code that defines error types with a <code>String</code> method will need to be updated |
| 755 | by hand to rename the methods to <code>Error</code>. |
| 756 | </p> |
| 757 | |
Rob Pike | 9d59c40 | 2011-12-08 11:35:28 -0800 | [diff] [blame] | 758 | <h3 id="errno">System call errors</h3> |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 759 | |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 760 | <p> |
| 761 | In Go 1, the |
Rob Pike | ebdcbf1 | 2011-12-12 12:26:56 -0800 | [diff] [blame] | 762 | <a href="/pkg/syscall/"><code>syscall</code></a> |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 763 | package returns an <code>error</code> for system call errors, |
| 764 | rather than plain integer <code>errno</code> values. |
| 765 | On Unix, the implementation is done by a |
Rob Pike | ebdcbf1 | 2011-12-12 12:26:56 -0800 | [diff] [blame] | 766 | <a href="/pkg/syscall/#Errno"><code>syscall.Errno</code></a> type |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 767 | that satisfies <code>error</code> and replaces the old <code>os.Errno</code>. |
| 768 | </p> |
| 769 | |
| 770 | <p> |
| 771 | <em>Updating</em>: |
| 772 | Gofix will update almost all code affected by the change. |
| 773 | Regardless, most code should use the <code>os</code> package |
| 774 | rather than <code>syscall</code> and so will be unaffected. |
| 775 | </p> |
| 776 | |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 777 | <h3 id="time">Time</h3> |
| 778 | |
Rob Pike | 5fa18e1 | 2011-12-12 21:08:03 -0800 | [diff] [blame] | 779 | <p> |
| 780 | One of the most sweeping changes in the Go 1 library is the |
| 781 | complete redesign of the |
| 782 | <a href="/pkg/time/"><code>time</code></a> package. |
| 783 | Instead of an integer number of nanoseconds as an <code>int64</code>, |
| 784 | and a separate <code>*time.Time</code> type to deal with human |
| 785 | units such as hours and years, |
| 786 | there are now two fundamental types: |
| 787 | <a href="/pkg/time/#Time"><code>time.Time</code></a> |
| 788 | (a value, so the <code>*</code> is gone), which represents a moment in time; |
| 789 | and <a href="/pkg/time/#Duration"><code>time.Duration</code></a>, |
| 790 | which represents an interval. |
| 791 | Both have nanosecond resolution. |
| 792 | A <code>Time</code> can represent any time into the ancient |
| 793 | past and remote future, while a <code>Duration</code> can |
| 794 | span plus or minus only about 290 years. |
| 795 | There are methods on these types, plus a number of helpful |
| 796 | predefined constant durations such as <code>time.Second</code>. |
| 797 | </p> |
| 798 | |
| 799 | <p> |
| 800 | Among the new methods are things like |
| 801 | <a href="/pkg/time/#Time.Add"><code>Time.Add</code></a>, |
| 802 | which adds a <code>Duration</code> to a <code>Time</code>, and |
| 803 | <a href="/pkg/time/#Time.Sub"><code>Time.Sub</code></a>, |
| 804 | which subtracts two <code>Times</code> to yield a <code>Duration</code>. |
| 805 | </p> |
| 806 | |
| 807 | <p> |
| 808 | The most important semantic change is that the Unix epoch (Jan 1, 1970) is now |
| 809 | relevant only for those functions and methods that mention Unix: |
| 810 | <a href="/pkg/time/#Unix"><code>time.Unix</code></a> |
| 811 | and the <a href="/pkg/time/#Time.Unix"><code>Unix</code></a> |
| 812 | and <a href="/pkg/time/#Time.UnixNano"><code>UnixNano</code></a> methods |
| 813 | of the <code>Time</code> type. |
| 814 | In particular, |
| 815 | <a href="/pkg/time/#Now"><code>time.Now</code></a> |
| 816 | returns a <code>time.Time</code> value rather than, in the old |
| 817 | API, an integer nanosecond count since the Unix epoch. |
| 818 | </p> |
| 819 | |
| 820 | <pre><!--{{code "progs/go1.go" `/sleepUntil/` `/^}/`}} |
| 821 | -->// sleepUntil sleeps until the specified time. It returns immediately if it's too late. |
| 822 | func sleepUntil(wakeup time.Time) { |
| 823 | now := time.Now() // A Time. |
| 824 | if !wakeup.After(now) { |
| 825 | return |
| 826 | } |
| 827 | delta := wakeup.Sub(now) // A Duration. |
| 828 | log.Printf("Sleeping for %.3fs", delta.Seconds()) |
| 829 | time.Sleep(delta) |
Andrew Gerrand | 5353e1e | 2012-01-06 09:20:31 +1100 | [diff] [blame] | 830 | }</pre> |
Rob Pike | 5fa18e1 | 2011-12-12 21:08:03 -0800 | [diff] [blame] | 831 | |
| 832 | <p> |
| 833 | The new types, methods, and constants have been propagated through |
| 834 | all the standard packages that use time, such as <code>os</code> and |
| 835 | its representation of file time stamps. |
| 836 | </p> |
| 837 | |
| 838 | <p> |
| 839 | <em>Updating</em>: |
| 840 | Gofix will update many uses of the old <code>time</code> package to use the new |
| 841 | types and methods, although it does not replace values such as <code>1e9</code> |
| 842 | representing nanoseconds per second. |
| 843 | Also, because of type changes in some of the values that arise, |
| 844 | some of the expressions rewritten by gofix may require |
| 845 | further hand editing; in such cases the rewrite will include |
| 846 | the correct function or method for the old functionality, but |
| 847 | may have the wrong type or require further analysis. |
| 848 | </p> |
| 849 | |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 850 | <h2 id="minor">Minor changes to the library</h2> |
| 851 | |
| 852 | <p> |
| 853 | This section describes smaller changes, such as those to less commonly |
| 854 | used packages or that affect |
| 855 | few programs beyond the need to run gofix. |
| 856 | This category includes packages that are new in Go 1. |
| 857 | </p> |
| 858 | |
| 859 | <h3 id="crypto_elliptic">The crypto/elliptic package</h3> |
| 860 | |
| 861 | <p> |
| 862 | In Go 1, <a href="/pkg/crypto/elliptic/#Curve"><code>elliptic.Curve</code></a> |
| 863 | has been made an interface to permit alternative implementations. The curve |
| 864 | parameters have been moved to the |
| 865 | <a href="/pkg/crypto/elliptic/#CurveParams"><code>elliptic.CurveParams</code></a> |
| 866 | structure. |
| 867 | </p> |
| 868 | |
| 869 | <p> |
| 870 | <em>Updating</em>: |
| 871 | Existing users of <code>*elliptic.Curve</code> will need to change to |
| 872 | simply <code>elliptic.Curve</code>. Calls to <code>Marshal</code>, |
| 873 | <code>Unmarshal</code> and <code>GenerateKey</code> are now functions |
| 874 | in <code>crypto/elliptic</code> that take an <code>elliptic.Curve</code> |
| 875 | as their first argument. |
| 876 | </p> |
| 877 | |
| 878 | <h3 id="crypto_x509">The crypto/x509 package</h3> |
| 879 | |
| 880 | <p> |
| 881 | In Go 1, the |
| 882 | <a href="/pkg/crypto/x509/#CreateCertificate"><code>CreateCertificate</code></a> |
| 883 | and |
| 884 | <a href="/pkg/crypto/x509/#CreateCRL"><code>CreateCRL</code></a> |
| 885 | functions in <code>crypto/x509</code> have been altered to take an |
| 886 | <code>interface{}</code> where they previously took a <code>*rsa.PublicKey</code> |
| 887 | or <code>*rsa.PrivateKey</code>. This will allow other public key algorithms |
| 888 | to be implemented in the future. |
| 889 | </p> |
| 890 | |
| 891 | <p> |
| 892 | <em>Updating</em>: |
| 893 | No changes will be needed. |
| 894 | </p> |
| 895 | |
Rob Pike | 531ded9 | 2012-01-20 15:38:03 -0800 | [diff] [blame] | 896 | <h3 id="flag">The flag package</h3> |
| 897 | |
| 898 | <p> |
| 899 | In Go 1, the interface <a href="/pkg/flag/#Value"><code>flag.Value</code></a> has changed slightly. |
| 900 | The <code>Set</code> method now returns an <code>error</code> instead of |
| 901 | a <code>bool</code> to indicate success or failure. |
| 902 | </p> |
| 903 | |
| 904 | <p> |
| 905 | There is also a new kind of flag, <code>Duration</code>, to support argument |
| 906 | values specifying time intervals. |
| 907 | Values for such flags must be given units, just as <code>time.Duration</code> |
| 908 | formats them: <code>10s</code>, <code>1h30m</code>, etc. |
| 909 | </p> |
| 910 | |
| 911 | <pre><!--{{code "progs/go1.go" `/timeout/`}} |
| 912 | -->var timeout = flag.Duration("timeout", 30*time.Second, "how long to wait for completion")</pre> |
| 913 | |
| 914 | <p> |
| 915 | <em>Updating</em>: |
| 916 | Programs that implement their own flags will need minor manual fixes to update their |
| 917 | <code>Set</code> methods. |
| 918 | The <code>Duration</code> flag is new and affects no existing code. |
| 919 | </p> |
| 920 | |
| 921 | |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 922 | <h3 id="go">The go/* packages</h3> |
| 923 | |
| 924 | <p> |
| 925 | Several packages under <code>go</code> have slightly revised APIs. |
| 926 | </p> |
| 927 | |
| 928 | <p> |
| 929 | The modes <code>AllowIllegalChars</code> and <code>InsertSemis</code> have been removed |
| 930 | from the <a href="/pkg/go/scanner/"><code>go/scanner</code></a> package. They were mostly |
| 931 | useful for scanning text other then Go source files. Instead, the |
| 932 | <a href="/pkg/text/scanner/"><code>text/scanner</code></a> package should be used |
| 933 | for that purpose. |
| 934 | </p> |
| 935 | |
| 936 | <p> |
| 937 | The set of parse functions provided by the <a href="/pkg/go/parser/"><code>go/parser</code></a> |
| 938 | package has been reduced to the primary parse function |
Gustavo Niemeyer | 75e9d24 | 2012-01-25 23:42:36 -0200 | [diff] [blame^] | 939 | <a href="/pkg/go/parser/#ParseFile"><code>ParseFile</code></a>, and a couple of |
| 940 | convenience functions <a href="/pkg/go/parser/#ParseDir"><code>ParseDir</code></a> |
| 941 | and <a href="/pkg/go/parser/#ParseExpr"><code>ParseExpr</code></a>. |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 942 | </p> |
| 943 | |
| 944 | <p> |
Gustavo Niemeyer | 75e9d24 | 2012-01-25 23:42:36 -0200 | [diff] [blame^] | 945 | 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] | 946 | streamlined by removing the <code>Doc</code> suffix: <code>PackageDoc</code> |
| 947 | is now <code>Package</code>, <code>ValueDoc</code> is <code>Value</code>, etc. |
| 948 | 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] | 949 | in the case of type <code>Value</code>) and <code>Type.Factories</code> has become |
| 950 | <code>Type.Funcs</code>. |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 951 | Instead of calling <code>doc.NewPackageDoc(pkg, importpath)</code>, |
| 952 | documentation for a package is created with: |
| 953 | </p> |
| 954 | |
| 955 | <pre> |
| 956 | doc.New(pkg, importpath, mode) |
| 957 | </pre> |
| 958 | |
| 959 | <p> |
| 960 | where the new <code>mode</code> parameter specifies the operation mode: |
Gustavo Niemeyer | 75e9d24 | 2012-01-25 23:42:36 -0200 | [diff] [blame^] | 961 | 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] | 962 | (not just exported ones) are considered. |
| 963 | The function <code>NewFileDoc</code> was removed, and the function |
| 964 | <code>CommentText</code> has become the method |
Gustavo Niemeyer | 75e9d24 | 2012-01-25 23:42:36 -0200 | [diff] [blame^] | 965 | <a href="/pkg/go/ast/#Text"><code>Text</code></a> of |
| 966 | <a href="/pkg/go/ast/#CommentGroup"><code>ast.CommentGroup</code></a>. |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 967 | </p> |
| 968 | |
| 969 | <p> |
Gustavo Niemeyer | 75e9d24 | 2012-01-25 23:42:36 -0200 | [diff] [blame^] | 970 | In package <a href="/pkg/go/token/"><code>go/token</code></a>, the |
| 971 | <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] | 972 | (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^] | 973 | 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] | 974 | accepts a function argument instead. |
| 975 | </p> |
| 976 | |
| 977 | <p> |
| 978 | <em>Updating</em>: |
| 979 | Code that uses packages in <code>go</code> will have to be updated by hand; the |
| 980 | compiler will reject incorrect uses. Templates used in conjuction with any of the |
| 981 | <code>go/doc</code> types may need manual fixes; the renamed fields will lead |
| 982 | to run-time errors. |
| 983 | </p> |
| 984 | |
Russ Cox | 1f1c9ba | 2012-01-18 10:36:43 -0500 | [diff] [blame] | 985 | <h3 id="hash">The hash package</h3> |
| 986 | |
| 987 | <p> |
| 988 | In Go 1, the definition of <a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> includes |
| 989 | a new method, <code>BlockSize</code>. This new method is used primarily in the |
| 990 | cryptographic libraries. |
| 991 | </p> |
| 992 | |
| 993 | <p> |
Rob Pike | 03ea8b1 | 2012-01-24 16:36:40 -0800 | [diff] [blame] | 994 | The <code>Sum</code> method of the |
| 995 | <a href="/pkg/hash/#Hash"><code>hash.Hash</code></a> interface now takes a |
| 996 | <code>[]byte</code> argument, to which the hash value will be appended. |
| 997 | The previous behavior can be recreated by adding a <code>nil</code> argument to the call. |
| 998 | </p> |
| 999 | |
| 1000 | <p> |
Russ Cox | 1f1c9ba | 2012-01-18 10:36:43 -0500 | [diff] [blame] | 1001 | <em>Updating</em>: |
| 1002 | Existing implementations of <code>hash.Hash</code> will need to add a |
| 1003 | <code>BlockSize</code> method. Hashes that process the input one byte at |
| 1004 | a time can implement <code>BlockSize</code> to return 1. |
Rob Pike | 03ea8b1 | 2012-01-24 16:36:40 -0800 | [diff] [blame] | 1005 | Gofix will update calls to the <code>Sum</code> methods of the various |
| 1006 | implementations of <code>hash.Hash</code>. |
Rob Pike | f76bd4f | 2011-12-12 19:25:25 -0800 | [diff] [blame] | 1007 | </p> |
| 1008 | |
| 1009 | <p> |
| 1010 | <em>Updating</em>: |
| 1011 | Since the package's functionality is new, no updating is necessary. |
| 1012 | </p> |
| 1013 | |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 1014 | <h3 id="http">The http package</h3> |
| 1015 | |
Rob Pike | f76bd4f | 2011-12-12 19:25:25 -0800 | [diff] [blame] | 1016 | <p> |
Jongmin Kim | 343098e | 2012-01-17 09:47:34 -0800 | [diff] [blame] | 1017 | 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] | 1018 | putting some of the utilities into a |
Jongmin Kim | 343098e | 2012-01-17 09:47:34 -0800 | [diff] [blame] | 1019 | <a href="/pkg/net/httputil/"><code>httputil</code></a> subdirectory. |
Rob Pike | f76bd4f | 2011-12-12 19:25:25 -0800 | [diff] [blame] | 1020 | These pieces are only rarely needed by HTTP clients. |
| 1021 | The affected items are: |
| 1022 | </p> |
| 1023 | |
| 1024 | <ul> |
| 1025 | <li>ClientConn</li> |
| 1026 | <li>DumpRequest</li> |
| 1027 | <li>DumpRequest</li> |
| 1028 | <li>DumpRequestOut</li> |
| 1029 | <li>DumpResponse</li> |
| 1030 | <li>NewChunkedReader</li> |
| 1031 | <li>NewChunkedWriter</li> |
| 1032 | <li>NewClientConn</li> |
| 1033 | <li>NewProxyClientConn</li> |
| 1034 | <li>NewServerConn</li> |
| 1035 | <li>NewSingleHostReverseProxy</li> |
| 1036 | <li>ReverseProxy</li> |
| 1037 | <li>ServerConn</li> |
| 1038 | </ul> |
| 1039 | |
| 1040 | <p> |
| 1041 | Also, the <code>Request.RawURL</code> field has been removed; it was a |
| 1042 | historical artifact. |
| 1043 | </p> |
| 1044 | |
| 1045 | <p> |
| 1046 | <em>Updating</em>: |
| 1047 | Gofix will update the few programs that are affected except for |
| 1048 | uses of <code>RawURL</code>, which must be fixed by hand. |
| 1049 | </p> |
| 1050 | |
Rob Pike | 2257e76 | 2012-01-23 16:11:49 -0800 | [diff] [blame] | 1051 | <h3 id="image">The image package</h3> |
| 1052 | |
| 1053 | <p> |
| 1054 | The <a href="/pkg/image/"><code>image</code></a> package has had a number of |
| 1055 | minor changes, rearrangements and renamings. |
| 1056 | </p> |
| 1057 | |
| 1058 | <p> |
| 1059 | Most of the color handling code has been moved into its own package, |
| 1060 | <a href="/pkg/image/color/"><code>image/color</code></a>. |
| 1061 | For the elements that moved, a symmetry arises; for instance, |
| 1062 | each pixel of an |
| 1063 | <a href="/pkg/image/#RGBA"><code>image.RGBA</code></a> |
| 1064 | is a |
| 1065 | <a href="/pkg/image/color/#RGBA"><code>color.RGBA</code></a>. |
| 1066 | </p> |
| 1067 | |
| 1068 | <p> |
| 1069 | The old <code>image/ycbcr</code> package has been folded, with some |
| 1070 | renamings, into the |
| 1071 | <a href="/pkg/image/"><code>image</code></a> |
| 1072 | and |
| 1073 | <a href="/pkg/image/color/"><code>image/color</code></a> |
| 1074 | packages. |
| 1075 | </p> |
| 1076 | |
| 1077 | <p> |
| 1078 | The old <code>image.ColorImage</code> type is still in the <code>image</code> |
| 1079 | package but has been renamed |
| 1080 | <a href="/pkg/image/#Uniform"><code>image.Uniform</code></a>, |
| 1081 | while <code>image.Tiled</code> |
| 1082 | has been renamed |
| 1083 | <a href="/pkg/image/#Repeated"><code>image.Repeated</code></a>. |
| 1084 | </p> |
| 1085 | |
| 1086 | <p> |
| 1087 | This table lists the renamings. |
| 1088 | </p> |
| 1089 | |
| 1090 | <table class="codetable" frame="border" summary="image renames"> |
| 1091 | <colgroup align="left" width="50%"></colgroup> |
| 1092 | <colgroup align="left" width="50%"></colgroup> |
| 1093 | <tr> |
| 1094 | <th align="left">Old</th> |
| 1095 | <th align="left">New</th> |
| 1096 | </tr> |
| 1097 | <tr> |
| 1098 | <td colspan="2"><hr></td> |
| 1099 | </tr> |
| 1100 | <tr><td>image.Color</td> <td>color.Color</td></tr> |
| 1101 | <tr><td>image.ColorModel</td> <td>color.Model</td></tr> |
| 1102 | <tr><td>image.ColorModelFunc</td> <td>color.ModelFunc</td></tr> |
| 1103 | <tr><td>image.PalettedColorModel</td> <td>color.Palette</td></tr> |
| 1104 | <tr> |
| 1105 | <td colspan="2"><hr></td> |
| 1106 | </tr> |
| 1107 | <tr><td>image.RGBAColor</td> <td>color.RGBA</td></tr> |
| 1108 | <tr><td>image.RGBA64Color</td> <td>color.RGBA64</td></tr> |
| 1109 | <tr><td>image.NRGBAColor</td> <td>color.NRGBA</td></tr> |
| 1110 | <tr><td>image.NRGBA64Color</td> <td>color.NRGBA64</td></tr> |
| 1111 | <tr><td>image.AlphaColor</td> <td>color.Alpha</td></tr> |
| 1112 | <tr><td>image.Alpha16Color</td> <td>color.Alpha16</td></tr> |
| 1113 | <tr><td>image.GrayColor</td> <td>color.Gray</td></tr> |
| 1114 | <tr><td>image.Gray16Color</td> <td>color.Gray16</td></tr> |
| 1115 | <tr> |
| 1116 | <td colspan="2"><hr></td> |
| 1117 | </tr> |
| 1118 | <tr><td>image.RGBAColorModel</td> <td>color.RGBAModel</td></tr> |
| 1119 | <tr><td>image.RGBA64ColorModel</td> <td>color.RGBA64Model</td></tr> |
| 1120 | <tr><td>image.NRGBAColorModel</td> <td>color.NRGBAModel</td></tr> |
| 1121 | <tr><td>image.NRGBA64ColorModel</td> <td>color.NRGBA64Model</td></tr> |
| 1122 | <tr><td>image.AlphaColorModel</td> <td>color.AlphaModel</td></tr> |
| 1123 | <tr><td>image.Alpha16ColorModel</td> <td>color.Alpha16Model</td></tr> |
| 1124 | <tr><td>image.GrayColorModel</td> <td>color.GrayModel</td></tr> |
| 1125 | <tr><td>image.Gray16ColorModel</td> <td>color.Gray16Model</td></tr> |
| 1126 | <tr> |
| 1127 | <td colspan="2"><hr></td> |
| 1128 | </tr> |
| 1129 | <tr><td>ycbcr.RGBToYCbCr</td> <td>color.RGBToYCbCr</td></tr> |
| 1130 | <tr><td>ycbcr.YCbCrToRGB</td> <td>color.YCbCrToRGB</td></tr> |
| 1131 | <tr><td>ycbcr.YCbCrColorModel</td> <td>color.YCbCrModel</td></tr> |
| 1132 | <tr><td>ycbcr.YCbCrColor</td> <td>color.YCbCr</td></tr> |
| 1133 | <tr><td>ycbcr.YCbCr</td> <td>image.YCbCr</td></tr> |
| 1134 | <tr> |
| 1135 | <td colspan="2"><hr></td> |
| 1136 | </tr> |
| 1137 | <tr><td>ycbcr.SubsampleRatio444</td> <td>image.YCbCrSubsampleRatio444</td></tr> |
| 1138 | <tr><td>ycbcr.SubsampleRatio422</td> <td>image.YCbCrSubsampleRatio422</td></tr> |
| 1139 | <tr><td>ycbcr.SubsampleRatio420</td> <td>image.YCbCrSubsampleRatio420</td></tr> |
| 1140 | <tr> |
| 1141 | <td colspan="2"><hr></td> |
| 1142 | </tr> |
| 1143 | <tr><td>image.ColorImage</td> <td>image.Uniform</td></tr> |
| 1144 | <tr><td>image.Tiled</td> <td>image.Repeated</td></tr> |
| 1145 | </table> |
| 1146 | |
| 1147 | <p> |
| 1148 | The image package's <code>New</code> functions |
| 1149 | (<a href="/pkg/image/#NewRGBA"><code>NewRGBA</code></a>, |
| 1150 | <a href="/pkg/image/#NewRGBA64"><code>NewRGBA64</code></a>, etc.) |
| 1151 | take an <a href="/pkg/image/#Rectangle"><code>image.Rectangle</code></a> as an argument |
| 1152 | instead of four integers. |
| 1153 | </p> |
| 1154 | |
| 1155 | <p> |
| 1156 | Finally, there are new predefined <code>color.Color</code> variables |
| 1157 | <a href="/pkg/image/color/#Black"><code>color.Black</code></a>, |
| 1158 | <a href="/pkg/image/color/#White"><code>color.White</code></a>, |
| 1159 | <a href="/pkg/image/color/#Opaque"><code>color.Opaque</code></a> |
| 1160 | and |
| 1161 | <a href="/pkg/image/color/#Transparent"><code>color.Transparent</code></a>. |
| 1162 | </p> |
| 1163 | |
| 1164 | <p> |
| 1165 | <em>Updating</em>: |
| 1166 | Gofix will update almost all code affected by the change. |
| 1167 | </p> |
| 1168 | |
Rob Pike | dd442a5 | 2012-01-24 17:02:06 -0800 | [diff] [blame] | 1169 | <h3 id="mime">The mime package</h3> |
| 1170 | |
| 1171 | <p> |
| 1172 | In Go 1, the <a href="/pkg/mime/#FormatMediaType"><code>FormatMediaType</code></a> function |
| 1173 | of the <code>mime</code> package has been simplified to make it |
| 1174 | consistent with |
| 1175 | <a href="/pkg/mime/#ParseMediaType"><code>ParseMediaType</code></a>. |
| 1176 | It now takes <code>"text/html"</code> rather than <code>"text"</code> and <code>"html"</code>. |
| 1177 | </p> |
| 1178 | |
| 1179 | <p> |
| 1180 | <em>Updating</em>: |
| 1181 | What little code is affected will be caught by the compiler and must be updated by hand. |
| 1182 | </p> |
| 1183 | |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 1184 | <h3 id="net">The net package</h3> |
| 1185 | |
Rob Pike | 03ea8b1 | 2012-01-24 16:36:40 -0800 | [diff] [blame] | 1186 | <p> |
| 1187 | In Go 1, the various <code>SetTimeout</code>, |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 1188 | <code>SetReadTimeout</code>, and <code>SetWriteTimeout</code> methods |
Rob Pike | 03ea8b1 | 2012-01-24 16:36:40 -0800 | [diff] [blame] | 1189 | have been replaced with |
| 1190 | <a href="/pkg/net/#IPConn.SetDeadline"><code>SetDeadline</code></a>, |
| 1191 | <a href="/pkg/net/#IPConn.SetReadDeadline"><code>SetReadDeadline</code></a>, and |
| 1192 | <a href="/pkg/net/#IPConn.SetWriteDeadline"><code>SetWriteDeadline</code></a>, |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 1193 | respectively. Rather than taking a timeout value in nanoseconds that |
| 1194 | apply to any activity on the connection, the new methods set an |
| 1195 | absolute deadline (as a <code>time.Time</code> value) after which |
Rob Pike | 03ea8b1 | 2012-01-24 16:36:40 -0800 | [diff] [blame] | 1196 | reads and writes will time out and no longer block. |
| 1197 | </p> |
| 1198 | |
| 1199 | <p> |
Rob Pike | dd442a5 | 2012-01-24 17:02:06 -0800 | [diff] [blame] | 1200 | There is also a new <a href="/pkg/net/#DialTimeout"><code>net.DialTimeout</code></a> method to simplify |
Rob Pike | 03ea8b1 | 2012-01-24 16:36:40 -0800 | [diff] [blame] | 1201 | timing out dialing a network address. |
| 1202 | </p> |
| 1203 | |
| 1204 | <p> |
| 1205 | <em>Updating</em>: |
| 1206 | Code that uses the old methods will fail to compile and must be updated by hand. |
| 1207 | The semantic change makes it difficult for gofix to update automatically. |
| 1208 | </p> |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 1209 | |
Rob Pike | 0a1376a | 2012-01-20 14:28:48 -0800 | [diff] [blame] | 1210 | <h3 id="os_fileinfo">The os.FileInfo type</h3> |
| 1211 | |
| 1212 | <p> |
| 1213 | Go 1 redefines the <a href="/pkg/os/#FileInfo"><code>os.FileInfo</code></a> type, |
| 1214 | changing it from a struct to an interface: |
| 1215 | </p> |
| 1216 | |
| 1217 | <pre> |
| 1218 | type FileInfo interface { |
| 1219 | Name() string // base name of the file |
| 1220 | Size() int64 // length in bytes |
| 1221 | Mode() FileMode // file mode bits |
| 1222 | ModTime() time.Time // modification time |
| 1223 | IsDir() bool // abbreviation for Mode().IsDir() |
| 1224 | } |
| 1225 | </pre> |
| 1226 | |
| 1227 | <p> |
| 1228 | The file mode information has been moved into a subtype called |
| 1229 | <a href="/pkg/os/#FileMode"><code>os.FileMode</code></a>, |
| 1230 | a simple integer type with <code>IsDir</code>, <code>Perm</code>, and <code>String</code> |
| 1231 | methods. |
| 1232 | </p> |
| 1233 | |
| 1234 | <p> |
| 1235 | The system-specific details of file modes and properties such as (on Unix) |
| 1236 | i-number have been removed from <code>FileInfo</code> altogether. |
| 1237 | Instead, each operating system's <code>os</code> package provides an |
| 1238 | implementation of the <code>FileInfo</code> interface, <code>*os.FileStat</code>, |
| 1239 | which in turn contains a <code>Sys</code> field that stores the |
| 1240 | system-specific representation of file metadata. |
| 1241 | For instance, to discover the i-number of a file on a Unix system, unpack |
| 1242 | the <code>FileInfo</code> like this: |
| 1243 | </p> |
| 1244 | |
| 1245 | <pre> |
| 1246 | fi, err := os.Stat("hello.go") |
| 1247 | if err != nil { |
| 1248 | log.Fatal(err) |
| 1249 | } |
| 1250 | // Make sure it's an implementation known to package os. |
| 1251 | fileStat, ok := fi.(*os.FileStat) |
| 1252 | if !ok { |
| 1253 | log.Fatal("hello.go: not an os File") |
| 1254 | } |
| 1255 | // Now check that it's a Unix file. |
| 1256 | unixStat, ok := fileStat.Sys.(*syscall.Stat_t) |
| 1257 | if !ok { |
| 1258 | log.Fatal("hello.go: not a Unix file") |
| 1259 | } |
| 1260 | fmt.Printf("file i-number: %d\n", unixStat.Ino) |
| 1261 | </pre> |
| 1262 | |
| 1263 | <p> |
| 1264 | Assuming (which is unwise) that <code>"hello.go"</code> is a Unix file, |
| 1265 | the i-number expression could be contracted to |
| 1266 | </p> |
| 1267 | |
| 1268 | <pre> |
| 1269 | fi.(*os.FileStat).Sys.(*syscall.Stat_t).Ino |
| 1270 | </pre> |
| 1271 | |
| 1272 | <p> |
| 1273 | The vast majority of uses of <code>FileInfo</code> need only the methods |
| 1274 | of the standard interface. |
| 1275 | </p> |
| 1276 | |
| 1277 | <p> |
| 1278 | <em>Updating</em>: |
| 1279 | Gofix will update code that uses the old equivalent of the current <code>os.FileInfo</code> |
| 1280 | and <code>os.FileMode</code> API. |
| 1281 | Code that needs system-specific file details will need to be updated by hand. |
| 1282 | </p> |
| 1283 | |
Rob Pike | dd442a5 | 2012-01-24 17:02:06 -0800 | [diff] [blame] | 1284 | <h3 id="path_filepath">The path/filepath package</h3> |
| 1285 | |
| 1286 | <p> |
| 1287 | In Go 1, the <a href="/pkg/path/filepath/#Walk"><code>Walk</code></a> function of the |
| 1288 | <code>path/filepath</code> package |
| 1289 | has been changed to take a function value of type |
| 1290 | <a href="/pkg/path/filepath/#WalkFunc"><code>WalkFunc</code></a> |
| 1291 | instead of a <code>Visitor</code> interface value. |
| 1292 | <code>WalkFunc</code> unifies the handling of both files and directories. |
| 1293 | </p> |
| 1294 | |
| 1295 | <pre> |
| 1296 | type WalkFunc func(path string, info *os.FileInfo, err os.Error) os.Error |
| 1297 | </pre> |
| 1298 | |
| 1299 | <p> |
| 1300 | The <code>WalkFunc</code> function will be called even for files or directories that could not be opened; |
| 1301 | in such cases the error argument will describe the failure. |
| 1302 | If a directory's contents are to be skipped, |
| 1303 | the function should return the value <code>SkipDir</code>. |
| 1304 | </p> |
| 1305 | |
| 1306 | <p> |
| 1307 | <font color="red">TODO: add an example?</font> |
| 1308 | </p> |
| 1309 | |
| 1310 | <p> |
| 1311 | <em>Updating</em>: |
| 1312 | The change simplifies most code but has subtle consequences, so affected programs |
| 1313 | will need to be updated by hand. |
| 1314 | The compiler will catch code using the old interface. |
| 1315 | </p> |
| 1316 | |
Rob Pike | 531ded9 | 2012-01-20 15:38:03 -0800 | [diff] [blame] | 1317 | <h3 id="runtime">The runtime package</h3> |
| 1318 | |
| 1319 | <p> |
| 1320 | The <code>runtime</code> package in Go 1 includes a new niladic function, |
| 1321 | <a href="/pkg/runtime/#NumCPU"><code>runtime.NumCPU</code></a>, that returns the number of CPUs available |
| 1322 | for parallel execution, as reported by the operating system kernel. |
| 1323 | Its value can inform the setting of <code>GOMAXPROCS</code>. |
| 1324 | </p> |
| 1325 | |
| 1326 | <p> |
| 1327 | <em>Updating</em>: |
| 1328 | No existing code is affected. |
| 1329 | </p> |
| 1330 | |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 1331 | <h3 id="strconv">The strconv package</h3> |
| 1332 | |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 1333 | <p> |
| 1334 | In Go 1, the |
Rob Pike | ebdcbf1 | 2011-12-12 12:26:56 -0800 | [diff] [blame] | 1335 | <a href="/pkg/strconv/"><code>strconv</code></a> |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 1336 | package has been significantly reworked to make it more Go-like and less C-like, |
| 1337 | although <code>Atoi</code> lives on (it's similar to |
| 1338 | <code>int(ParseInt(x, 10, 0))</code>, as does |
| 1339 | <code>Itoa(x)</code> (<code>FormatInt(int64(x), 10)</code>). |
| 1340 | There are also new variants of some of the functions that append to byte slices rather than |
| 1341 | return strings, to allow control over allocation. |
| 1342 | </p> |
| 1343 | |
| 1344 | <p> |
| 1345 | This table summarizes the renamings; see the |
Rob Pike | ebdcbf1 | 2011-12-12 12:26:56 -0800 | [diff] [blame] | 1346 | <a href="/pkg/strconv/">package documentation</a> |
Rob Pike | 71ccf73 | 2011-12-09 14:12:51 -0800 | [diff] [blame] | 1347 | for full details. |
| 1348 | </p> |
| 1349 | |
| 1350 | <table class="codetable" frame="border" summary="strconv renames"> |
| 1351 | <colgroup align="left" width="50%"></colgroup> |
| 1352 | <colgroup align="left" width="50%"></colgroup> |
| 1353 | <tr> |
| 1354 | <th align="left">Old call</th> |
| 1355 | <th align="left">New call</th> |
| 1356 | </tr> |
| 1357 | <tr> |
| 1358 | <td colspan="2"><hr></td> |
| 1359 | </tr> |
| 1360 | <tr><td>Atob(x)</td> <td>ParseBool(x)</td></tr> |
| 1361 | <tr> |
| 1362 | <td colspan="2"><hr></td> |
| 1363 | </tr> |
| 1364 | <tr><td>Atof32(x)</td> <td>ParseFloat(x, 32)§</td></tr> |
| 1365 | <tr><td>Atof64(x)</td> <td>ParseFloat(x, 64)</td></tr> |
| 1366 | <tr><td>AtofN(x, n)</td> <td>ParseFloat(x, n)</td></tr> |
| 1367 | <tr> |
| 1368 | <td colspan="2"><hr></td> |
| 1369 | </tr> |
| 1370 | <tr><td>Atoi(x)</td> <td>Atoi(x)</td></tr> |
| 1371 | <tr><td>Atoi(x)</td> <td>ParseInt(x, 10, 0)§</td></tr> |
| 1372 | <tr><td>Atoi64(x)</td> <td>ParseInt(x, 10, 64)</td></tr> |
| 1373 | <tr> |
| 1374 | <td colspan="2"><hr></td> |
| 1375 | </tr> |
| 1376 | <tr><td>Atoui(x)</td> <td>ParseUint(x, 10, 0)§</td></tr> |
| 1377 | <tr><td>Atoi64(x)</td> <td>ParseInt(x, 10, 64)</td></tr> |
| 1378 | <tr> |
| 1379 | <td colspan="2"><hr></td> |
| 1380 | </tr> |
| 1381 | <tr><td>Btoi64(x, b)</td> <td>ParseInt(x, b, 64)</td></tr> |
| 1382 | <tr><td>Btoui64(x, b)</td> <td>ParseUint(x, b, 64)</td></tr> |
| 1383 | <tr> |
| 1384 | <td colspan="2"><hr></td> |
| 1385 | </tr> |
| 1386 | <tr><td>Btoa(x)</td> <td>FormatBool(x)</td></tr> |
| 1387 | <tr> |
| 1388 | <td colspan="2"><hr></td> |
| 1389 | </tr> |
| 1390 | <tr><td>Ftoa32(x, f, p)</td> <td>FormatFloat(x, float64(f), p, 32)</td></tr> |
| 1391 | <tr><td>Ftoa64(x, f, p)</td> <td>FormatFloat(x, f, p, 64)</td></tr> |
| 1392 | <tr><td>FtoaN(x, f, p, n)</td> <td>FormatFloat(x, f, p, n)</td></tr> |
| 1393 | <tr> |
| 1394 | <td colspan="2"><hr></td> |
| 1395 | </tr> |
| 1396 | <tr><td>Itoa(x)</td> <td>Itoa(x)</td></tr> |
| 1397 | <tr><td>Itoa(x)</td> <td>FormatInt(int64(x), 10)</td></tr> |
| 1398 | <tr><td>Itoa64(x)</td> <td>FormatInt(x, 10)</td></tr> |
| 1399 | <tr> |
| 1400 | <td colspan="2"><hr></td> |
| 1401 | </tr> |
| 1402 | <tr><td>Itob(x, b)</td> <td>FormatInt(int64(x), b)</td></tr> |
| 1403 | <tr><td>Itob64(x, b)</td> <td>FormatInt(x, b)</td></tr> |
| 1404 | <tr> |
| 1405 | <td colspan="2"><hr></td> |
| 1406 | </tr> |
| 1407 | <tr><td>Uitoa(x)</td> <td>FormatUint(uint64(x), 10)</td></tr> |
| 1408 | <tr><td>Uitoa64(x)</td> <td>FormatUint(x, 10)</td></tr> |
| 1409 | <tr> |
| 1410 | <td colspan="2"><hr></td> |
| 1411 | </tr> |
| 1412 | <tr><td>Uitob(x, b)</td> <td>FormatUint(uint64(x), b)</td></tr> |
| 1413 | <tr><td>Uitob64(x, b)</td> <td>FormatUint(x, b)</td></tr> |
| 1414 | </table> |
| 1415 | |
| 1416 | <p> |
| 1417 | <em>Updating</em>: |
| 1418 | Gofix will update almost all code affected by the change. |
| 1419 | <br> |
| 1420 | § <code>Atoi</code> persists but <code>Atoui</code> and <code>Atof32</code> do not, so |
| 1421 | they may require |
| 1422 | a cast that must be added by hand; gofix will warn about it. |
| 1423 | </p> |
| 1424 | |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 1425 | |
Rob Pike | 531ded9 | 2012-01-20 15:38:03 -0800 | [diff] [blame] | 1426 | <h3 id="testing">The testing package</h3> |
| 1427 | |
| 1428 | <p> |
| 1429 | The testing package has a type, <code>B</code>, passed as an argument to benchmark functions. |
| 1430 | In Go 1, <code>B</code> has new methods, analogous to those of <code>T</code>, enabling |
| 1431 | logging and failure reporting. |
| 1432 | </p> |
| 1433 | |
| 1434 | <pre><!--{{code "progs/go1.go" `/func.*Benchmark/` `/^}/`}} |
| 1435 | -->func BenchmarkSprintf(b *testing.B) { |
| 1436 | // Verify correctness before running benchmark. |
| 1437 | b.StopTimer() |
| 1438 | got := fmt.Sprintf("%x", 23) |
| 1439 | const expect = "17" |
| 1440 | if expect != got { |
| 1441 | b.Fatalf("expected %q; got %q", expect, got) |
| 1442 | } |
| 1443 | b.StartTimer() |
| 1444 | for i := 0; i < b.N; i++ { |
| 1445 | fmt.Sprintf("%x", 23) |
| 1446 | } |
| 1447 | }</pre> |
| 1448 | |
| 1449 | <p> |
| 1450 | <em>Updating</em>: |
| 1451 | Existing code is unaffected, although benchmarks that use <code>println</code> |
Rob Pike | 2257e76 | 2012-01-23 16:11:49 -0800 | [diff] [blame] | 1452 | or <code>panic</code> should be updated to use the new methods. |
Rob Pike | 531ded9 | 2012-01-20 15:38:03 -0800 | [diff] [blame] | 1453 | </p> |
| 1454 | |
Gustavo Niemeyer | 805d620 | 2012-01-25 23:11:25 -0200 | [diff] [blame] | 1455 | <h3 id="url">The url package</h3> |
| 1456 | |
| 1457 | <p> |
| 1458 | In Go 1 several fields from the <a href="/pkg/net/url#URL"><code>url.URL</code></a> type |
| 1459 | were removed or replaced. |
| 1460 | </p> |
| 1461 | |
| 1462 | <p> |
| 1463 | The <a href="/pkg/net/url#URL.String"><code>String</code></a> method now |
| 1464 | predictably rebuilds an encoded URL string using all of <code>URL</code>'s |
| 1465 | fields as necessary. The resulting string will also no longer have |
| 1466 | passwords escaped. |
| 1467 | </p> |
| 1468 | |
| 1469 | <p> |
| 1470 | The <code>Raw</code> field has been removed. In most cases the <code>String</code> |
| 1471 | method may be used in its place. |
| 1472 | </p> |
| 1473 | |
| 1474 | <p> |
| 1475 | The old <code>RawUserinfo</code> field is replaced by the <code>User</code> |
| 1476 | field, of type <a href="/pkg/net/url#Userinfo"><code>*net.Userinfo</code></a>. |
| 1477 | Values of this type may be created using the new <a href="/pkg/net/url#User"><code>net.User</code></a> |
| 1478 | and <a href="/pkg/net/url#UserPassword"><code>net.UserPassword</code></a> |
| 1479 | functions. The <code>EscapeUserinfo</code> and <code>UnescapeUserinfo</code> |
| 1480 | functions are also gone. |
| 1481 | </p> |
| 1482 | |
| 1483 | <p> |
| 1484 | The <code>RawAuthority</code> field has been removed. The same information is |
| 1485 | available in the <code>Host</code> and <code>User</code> fields. |
| 1486 | </p> |
| 1487 | |
| 1488 | <p> |
| 1489 | The <code>RawPath</code> field and the <code>EncodedPath</code> method have |
| 1490 | been removed. The path information in rooted URLs (with a slash following the |
| 1491 | schema) is now available only in decoded form in the <code>Path</code> field. |
| 1492 | Occasionally, the encoded data may be required to obtain information that |
| 1493 | was lost in the decoding process. These cases must be handled by accessing |
| 1494 | the data the URL was built from. |
| 1495 | </p> |
| 1496 | |
| 1497 | <p> |
| 1498 | URLs with non-rooted paths, such as <code>"mailto:dev@golang.org?subject=Hi"</code>, |
| 1499 | are also handled differently. The <code>OpaquePath</code> boolean field has been |
| 1500 | removed and a new <code>Opaque</code> string field introduced to hold the encoded |
| 1501 | path for such URLs. In Go 1, the cited URL parses as: |
| 1502 | </p> |
| 1503 | |
| 1504 | <pre> |
| 1505 | URL{ |
| 1506 | Scheme: "mailto", |
| 1507 | Opaque: "dev@golang.org", |
| 1508 | RawQuery: "subject=Hi", |
| 1509 | } |
| 1510 | </pre> |
| 1511 | |
| 1512 | <p> |
| 1513 | A new <a href="/pkg/net/url#URL.RequestURI"><code>RequestURI</code></a> method was |
| 1514 | added to <code>URL</code>. |
| 1515 | </p> |
| 1516 | |
| 1517 | <p> |
| 1518 | <em>Updating</em>: |
| 1519 | Code that uses the old fields will fail to compile and must be updated by hand. |
| 1520 | The semantic changes make it difficult for gofix to update automatically. |
| 1521 | </p> |
| 1522 | |
Rob Pike | bab4dec | 2011-12-07 14:33:37 -0800 | [diff] [blame] | 1523 | <h2 id="go_command">The go command</h2> |
| 1524 | |
| 1525 | <h2 id="releases">Packaged releases</h2> |
| 1526 | |