Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | # Go 1.1 "gotchas" |
| 2 | |
| 3 | While Go 1.1 is compatible with Go 1.0, the [compatibility promise](http://golang.org/doc/go1compat.html) permits the Go authors to break existing programs if they were incorrect in the first place. |
| 4 | |
| 5 | Here are a few ways in which the bug fixes in Go 1.1 may have broken your Go programs. |
| 6 | |
| 7 | |
| 8 | ## Unknown foo.Bar field in struct literal |
| 9 | |
| 10 | Struct field names must not include package qualifiers. |
| 11 | For example, take this struct with an embedded ` *bytes.Buffer ` field: |
| 12 | |
| 13 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 14 | type S struct { |
| 15 | *bytes.Buffer |
| 16 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 17 | ``` |
| 18 | |
| 19 | In Go 1.0 the compiler would (incorrectly) accept this struct literal: |
| 20 | |
| 21 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 22 | s := S{ |
| 23 | bytes.Buffer: new(bytes.Buffer), |
| 24 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 25 | ``` |
| 26 | |
| 27 | Under Go 1.1 the compiler rejects this. |
| 28 | Instead you should use the field name without the package qualifier: |
| 29 | |
| 30 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 31 | s := S{ |
| 32 | Buffer: new(bytes.Buffer), |
| 33 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 34 | ``` |
| 35 | |
| 36 | ## Initialization loop |
| 37 | |
| 38 | The Go 1.1 compiler now better detects initialization loops. |
| 39 | |
| 40 | For instance, the following code compiled under Go 1.0. |
| 41 | |
| 42 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 43 | var funcVar = fn |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 44 | |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 45 | func fn() { |
| 46 | funcVar() |
| 47 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 48 | ``` |
| 49 | |
| 50 | Such code must now use an ` init ` function for the variable assignment to avoid |
| 51 | the initialization loop. |
| 52 | |
| 53 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 54 | var funcVar func() |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 55 | |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 56 | func fn() { |
| 57 | funcVar() |
| 58 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 59 | |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 60 | func init() { |
| 61 | funcVar = fn |
| 62 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 63 | ``` |
| 64 | |
| 65 | In particular, this affects users of App Engine's [delay package](https://developers.google.com/appengine/docs/go/taskqueue/delay). |
| 66 | |
| 67 | |
| 68 | ## Cannot fallthrough final case in switch |
| 69 | |
| 70 | Go 1.0 permitted fallthrough in the final case of a switch statement: |
| 71 | |
| 72 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 73 | switch { |
| 74 | case false: |
| 75 | fallthrough // fall through to 'true' case |
| 76 | case true: |
| 77 | fallthrough // fall through to... nowhere? |
| 78 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 79 | ``` |
| 80 | |
| 81 | A language change affecting [return requirements](http://golang.org/doc/go1.1#return) led us to make the superfluous fallthrough illegal. |
| 82 | |
| 83 | The fix is to remove such statements from your code. |
| 84 | |
| 85 | |
| 86 | ## Duplicate argument name in parameters and return values |
| 87 | |
| 88 | A compiler bug permitted function type declarations with parameters and return values of the same name. This would compile under Go 1.0: |
| 89 | |
| 90 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 91 | type T func(a int) (a int) |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 92 | ``` |
| 93 | |
| 94 | Under Go 1.1, the compiler gives an error: |
| 95 | |
| 96 | ``` |
| 97 | duplicate argument a |
| 98 | ``` |
| 99 | |
| 100 | The fix is to rename the arguments so that they use different names. |
| 101 | |
| 102 | |
| 103 | ## Package "go" forbidden |
| 104 | |
| 105 | The import path "go" is now reserved. If you have a package in your workspace |
| 106 | whose import path is "go", you will need to rename it and move it somewhere |
| 107 | else. |
| 108 | |
| 109 | While it was permitted, it is a bad idea to use this import path. |
| 110 | The standard library includes "go/parser", "go/ast", and so on. |
| 111 | It's posssible that a "go" package might be introduced in a future Go release, |
| 112 | making your "go" package unusable. |
| 113 | |
| 114 | Please read [How to write Go code](http://golang.org/doc/code.html) for more |
| 115 | details about importh paths. |