Andrew Gerrand | 7cb21a7 | 2012-01-19 11:24:54 +1100 | [diff] [blame] | 1 | <!--{ |
| 2 | "Title": "FAQ" |
| 3 | }--> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 4 | |
| 5 | <h2 id="Origins">Origins</h2> |
| 6 | |
| 7 | <h3 id="What_is_the_purpose_of_the_project"> |
| 8 | What is the purpose of the project?</h3> |
| 9 | |
| 10 | <p> |
| 11 | No major systems language has emerged in over a decade, but over that time |
| 12 | the computing landscape has changed tremendously. There are several trends: |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 13 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 14 | |
| 15 | <ul> |
| 16 | <li> |
| 17 | Computers are enormously quicker but software development is not faster. |
| 18 | <li> |
| 19 | Dependency management is a big part of software development today but the |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 20 | “header files” of languages in the C tradition are antithetical to clean |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 21 | dependency analysis—and fast compilation. |
| 22 | <li> |
| 23 | There is a growing rebellion against cumbersome type systems like those of |
| 24 | Java and C++, pushing people towards dynamically typed languages such as |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 25 | Python and JavaScript. |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 26 | <li> |
| 27 | Some fundamental concepts such as garbage collection and parallel computation |
| 28 | are not well supported by popular systems languages. |
| 29 | <li> |
| 30 | The emergence of multicore computers has generated worry and confusion. |
| 31 | </ul> |
| 32 | |
| 33 | <p> |
| 34 | We believe it's worth trying again with a new language, a concurrent, |
| 35 | garbage-collected language with fast compilation. Regarding the points above: |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 36 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 37 | |
| 38 | <ul> |
| 39 | <li> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 40 | It is possible to compile a large Go program in a few seconds on a single computer. |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 41 | <li> |
| 42 | Go provides a model for software construction that makes dependency |
| 43 | analysis easy and avoids much of the overhead of C-style include files and |
| 44 | libraries. |
| 45 | <li> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 46 | Go's type system has no hierarchy, so no time is spent defining the |
| 47 | relationships between types. Also, although Go has static types the language |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 48 | attempts to make types feel lighter weight than in typical OO languages. |
| 49 | <li> |
| 50 | Go is fully garbage-collected and provides fundamental support for |
| 51 | concurrent execution and communication. |
| 52 | <li> |
| 53 | By its design, Go proposes an approach for the construction of system |
| 54 | software on multicore machines. |
| 55 | </ul> |
| 56 | |
Rob Pike | 8de5080 | 2012-07-16 13:31:15 -0700 | [diff] [blame^] | 57 | <h3 id="What_is_the_status_of_the_project"> |
| 58 | What is the status of the project?</h3> |
| 59 | |
| 60 | <p> |
| 61 | Go became a public open source project on November 10, 2009. |
| 62 | After a couple of years of very active design and development, stability was called for and |
| 63 | Go 1 was <a href="http://blog.golang.org/2012/03/go-version-1-is-released.html">released</a> |
| 64 | on March 28, 2012. |
| 65 | Go 1, which includes a <a href="/ref/spec">language specification</a>, |
| 66 | <a href="/pkg/">standard libraries</a>, |
| 67 | and <a href="/cmd/go/">custom tools</a>, |
| 68 | provides a stable foundation for creating reliable products, projects, and publications. |
| 69 | </p> |
| 70 | |
| 71 | <p> |
| 72 | With that stability established, we are using Go to develop programs, products, and tools rather than |
| 73 | actively changing the language and libraries. |
| 74 | In fact, the purpose of Go 1 is to provide <a href="/doc/go1compat.html">long-term stability</a>. |
| 75 | Backwards-incompatible changes will not be made to any Go 1 point release. |
| 76 | We want to use what we have to learn how a future version of Go might look, rather than to play with |
| 77 | the language underfoot. |
| 78 | </p> |
| 79 | |
| 80 | <p> |
| 81 | Of course, development will continue on Go itself, but the focus will be on performance, reliability, |
| 82 | portability and the addition of new functionality such as improved support for internationalization. |
| 83 | </p> |
| 84 | |
| 85 | <p> |
| 86 | There may well be a Go 2 one day, but not for a few years and it will be influenced by what we learn using Go 1 as it is today. |
| 87 | </p> |
| 88 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 89 | <h3 id="What_is_the_origin_of_the_name"> |
| 90 | What is the origin of the name?</h3> |
| 91 | |
| 92 | <p> |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 93 | “Ogle” would be a good name for a Go debugger. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 94 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 95 | |
Rob Pike | bdecae9 | 2009-11-23 17:34:23 -0800 | [diff] [blame] | 96 | <h3 id="Whats_the_origin_of_the_mascot"> |
| 97 | What's the origin of the mascot?</h3> |
| 98 | |
| 99 | <p> |
| 100 | The mascot and logo were designed by |
| 101 | <a href="http://reneefrench.blogspot.com">Renée French</a>, who also designed |
| 102 | <a href="http://plan9.bell-labs.com/plan9/glenda.html">Glenda</a>, |
| 103 | the Plan 9 bunny. |
| 104 | The gopher is derived from one she used for an <a href="http://wfmu.org/">WFMU</a> |
| 105 | T-shirt design some years ago. |
| 106 | The logo and mascot are covered by the |
| 107 | <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0</a> |
| 108 | license. |
| 109 | </p> |
| 110 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 111 | <h3 id="history"> |
| 112 | What is the history of the project?</h3> |
| 113 | <p> |
| 114 | Robert Griesemer, Rob Pike and Ken Thompson started sketching the |
| 115 | goals for a new language on the white board on September 21, 2007. |
| 116 | Within a few days the goals had settled into a plan to do something |
| 117 | and a fair idea of what it would be. Design continued part-time in |
| 118 | parallel with unrelated work. By January 2008, Ken had started work |
| 119 | on a compiler with which to explore ideas; it generated C code as its |
| 120 | output. By mid-year the language had become a full-time project and |
| 121 | had settled enough to attempt a production compiler. In May 2008, |
| 122 | Ian Taylor independently started on a GCC front end for Go using the |
| 123 | draft specification. Russ Cox joined in late 2008 and helped move the language |
| 124 | and libraries from prototype to reality. |
| 125 | </p> |
| 126 | |
| 127 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 128 | Go became a public open source project on November 10, 2009. |
| 129 | Many people from the community have contributed ideas, discussions, and code. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 130 | </p> |
| 131 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 132 | <h3 id="creating_a_new_language"> |
| 133 | Why are you creating a new language?</h3> |
| 134 | <p> |
| 135 | Go was born out of frustration with existing languages and |
| 136 | environments for systems programming. Programming had become too |
| 137 | difficult and the choice of languages was partly to blame. One had to |
| 138 | choose either efficient compilation, efficient execution, or ease of |
| 139 | programming; all three were not available in the same mainstream |
| 140 | language. Programmers who could were choosing ease over |
| 141 | safety and efficiency by moving to dynamically typed languages such as |
| 142 | Python and JavaScript rather than C++ or, to a lesser extent, Java. |
| 143 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 144 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 145 | <p> |
| 146 | Go is an attempt to combine the ease of programming of an interpreted, |
| 147 | dynamically typed |
| 148 | language with the efficiency and safety of a statically typed, compiled language. |
| 149 | It also aims to be modern, with support for networked and multicore |
| 150 | computing. Finally, it is intended to be <i>fast</i>: it should take |
| 151 | at most a few seconds to build a large executable on a single computer. |
| 152 | To meet these goals required addressing a number of |
| 153 | linguistic issues: an expressive but lightweight type system; |
| 154 | concurrency and garbage collection; rigid dependency specification; |
| 155 | and so on. These cannot be addressed well by libraries or tools; a new |
| 156 | language was called for. |
| 157 | </p> |
| 158 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 159 | <h3 id="ancestors"> |
| 160 | What are Go's ancestors?</h3> |
| 161 | <p> |
| 162 | Go is mostly in the C family (basic syntax), |
| 163 | with significant input from the Pascal/Modula/Oberon |
| 164 | family (declarations, packages), |
| 165 | plus some ideas from languages |
| 166 | inspired by Tony Hoare's CSP, |
| 167 | such as Newsqueak and Limbo (concurrency). |
| 168 | However, it is a new language across the board. |
| 169 | In every respect the language was designed by thinking |
| 170 | about what programmers do and how to make programming, at least the |
| 171 | kind of programming we do, more effective, which means more fun. |
| 172 | </p> |
| 173 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 174 | <h3 id="principles"> |
| 175 | What are the guiding principles in the design?</h3> |
| 176 | <p> |
| 177 | Programming today involves too much bookkeeping, repetition, and |
| 178 | clerical work. As Dick Gabriel says, “Old programs read |
| 179 | like quiet conversations between a well-spoken research worker and a |
| 180 | well-studied mechanical colleague, not as a debate with a compiler. |
| 181 | Who'd have guessed sophistication bought such noise?” |
| 182 | The sophistication is worthwhile—no one wants to go back to |
| 183 | the old languages—but can it be more quietly achieved? |
| 184 | </p> |
| 185 | <p> |
| 186 | Go attempts to reduce the amount of typing in both senses of the word. |
| 187 | Throughout its design, we have tried to reduce clutter and |
| 188 | complexity. There are no forward declarations and no header files; |
| 189 | everything is declared exactly once. Initialization is expressive, |
| 190 | automatic, and easy to use. Syntax is clean and light on keywords. |
| 191 | Stuttering (<code>foo.Foo* myFoo = new(foo.Foo)</code>) is reduced by |
| 192 | simple type derivation using the <code>:=</code> |
| 193 | declare-and-initialize construct. And perhaps most radically, there |
| 194 | is no type hierarchy: types just <i>are</i>, they don't have to |
| 195 | announce their relationships. These simplifications allow Go to be |
| 196 | expressive yet comprehensible without sacrificing, well, sophistication. |
| 197 | </p> |
| 198 | <p> |
| 199 | Another important principle is to keep the concepts orthogonal. |
| 200 | Methods can be implemented for any type; structures represent data while |
| 201 | interfaces represent abstraction; and so on. Orthogonality makes it |
| 202 | easier to understand what happens when things combine. |
| 203 | </p> |
| 204 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 205 | <h2 id="Usage">Usage</h2> |
| 206 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 207 | <h3 id="Is_Google_using_go_internally"> Is Google using Go internally?</h3> |
Rob Pike | 7685a67 | 2009-11-09 20:25:45 -0800 | [diff] [blame] | 208 | |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 209 | <p> |
| 210 | Yes. There are now several Go programs deployed in |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 211 | production inside Google. A public example is the server behind |
| 212 | <a href="http://golang.org">http://golang.org</a>. |
| 213 | It's just the <a href="/cmd/godoc"><code>godoc</code></a> |
| 214 | document server running in a production configuration on |
Dave Cheney | 82cbcb0 | 2012-07-11 09:41:08 -0700 | [diff] [blame] | 215 | <a href="https://developers.google.com/appengine/">Google App Engine</a>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 216 | </p> |
Rob Pike | 7685a67 | 2009-11-09 20:25:45 -0800 | [diff] [blame] | 217 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 218 | <h3 id="Do_Go_programs_link_with_Cpp_programs"> |
| 219 | Do Go programs link with C/C++ programs?</h3> |
| 220 | |
| 221 | <p> |
Andrew Gerrand | 2a5879d | 2012-03-20 13:50:05 +1100 | [diff] [blame] | 222 | There are two Go compiler implementations, <code>gc</code> |
| 223 | (the <code>6g</code> program and friends) and <code>gccgo</code>. |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 224 | <code>Gc</code> uses a different calling convention and linker and can |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 225 | therefore only be linked with C programs using the same convention. |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 226 | There is such a C compiler but no C++ compiler. |
| 227 | <code>Gccgo</code> is a GCC front-end that can, with care, be linked with |
| 228 | GCC-compiled C or C++ programs. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 229 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 230 | |
| 231 | <p> |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 232 | The <a href="/cmd/cgo/">cgo</a> program provides the mechanism for a |
| 233 | “foreign function interface” to allow safe calling of |
| 234 | C libraries from Go code. SWIG extends this capability to C++ libraries. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 235 | </p> |
| 236 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 237 | |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 238 | <h3 id="Does_Go_support_Google_protocol_buffers"> |
| 239 | Does Go support Google's protocol buffers?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 240 | |
| 241 | <p> |
Rob Pike | 6b3031b | 2010-03-23 17:03:28 -0700 | [diff] [blame] | 242 | A separate open source project provides the necessary compiler plugin and library. |
| 243 | It is available at |
| 244 | <a href="http://code.google.com/p/goprotobuf/">http://code.google.com/p/goprotobuf/</a> |
| 245 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 246 | |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 247 | |
Russ Cox | 6301fb4 | 2009-12-03 17:23:33 -0800 | [diff] [blame] | 248 | <h3 id="Can_I_translate_the_Go_home_page"> |
| 249 | Can I translate the Go home page into another language?</h3> |
| 250 | |
| 251 | <p> |
| 252 | Absolutely. We encourage developers to make Go Language sites in their own languages. |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 253 | However, if you choose to add the Google logo or branding to your site |
Russ Cox | 6301fb4 | 2009-12-03 17:23:33 -0800 | [diff] [blame] | 254 | (it does not appear on <a href="http://golang.org/">golang.org</a>), |
| 255 | you will need to abide by the guidelines at |
| 256 | <a href="http://www.google.com/permissions/guidelines.html">http://www.google.com/permissions/guidelines.html</a> |
| 257 | </p> |
| 258 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 259 | <h2 id="Design">Design</h2> |
| 260 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 261 | <h3 id="unicode_identifiers"> |
| 262 | What's up with Unicode identifiers?</h3> |
| 263 | |
| 264 | <p> |
| 265 | It was important to us to extend the space of identifiers from the |
| 266 | confines of ASCII. Go's rule—identifier characters must be |
| 267 | letters or digits as defined by Unicode—is simple to understand |
| 268 | and to implement but has restrictions. Combining characters are |
| 269 | excluded by design, for instance. |
| 270 | Until there |
| 271 | is an agreed external definition of what an identifier might be, |
| 272 | plus a definition of canonicalization of identifiers that guarantees |
| 273 | no ambiguity, it seemed better to keep combining characters out of |
| 274 | the mix. Thus we have a simple rule that can be expanded later |
| 275 | without breaking programs, one that avoids bugs that would surely arise |
| 276 | from a rule that admits ambiguous identifiers. |
| 277 | </p> |
| 278 | |
| 279 | <p> |
| 280 | On a related note, since an exported identifier must begin with an |
| 281 | upper-case letter, identifiers created from “letters” |
| 282 | in some languages can, by definition, not be exported. For now the |
| 283 | only solution is to use something like <code>X日本語</code>, which |
| 284 | is clearly unsatisfactory; we are considering other options. The |
| 285 | case-for-visibility rule is unlikely to change however; it's one |
| 286 | of our favorite features of Go. |
| 287 | </p> |
| 288 | |
| 289 | <h3 id="Why_doesnt_Go_have_feature_X">Why does Go not have feature X?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 290 | |
| 291 | <p> |
| 292 | Every language contains novel features and omits someone's favorite |
| 293 | feature. Go was designed with an eye on felicity of programming, speed of |
| 294 | compilation, orthogonality of concepts, and the need to support features |
| 295 | such as concurrency and garbage collection. Your favorite feature may be |
| 296 | missing because it doesn't fit, because it affects compilation speed or |
| 297 | clarity of design, or because it would make the fundamental system model |
| 298 | too difficult. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 299 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 300 | |
| 301 | <p> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 302 | If it bothers you that Go is missing feature <var>X</var>, |
| 303 | please forgive us and investigate the features that Go does have. You might find that |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 304 | they compensate in interesting ways for the lack of <var>X</var>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 305 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 306 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 307 | <h3 id="generics"> |
| 308 | Why does Go not have generic types?</h3> |
| 309 | <p> |
| 310 | Generics may well be added at some point. We don't feel an urgency for |
| 311 | them, although we understand some programmers do. |
| 312 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 313 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 314 | <p> |
| 315 | Generics are convenient but they come at a cost in |
| 316 | complexity in the type system and run-time. We haven't yet found a |
| 317 | design that gives value proportionate to the complexity, although we |
| 318 | continue to think about it. Meanwhile, Go's built-in maps and slices, |
| 319 | plus the ability to use the empty interface to construct containers |
| 320 | (with explicit unboxing) mean in many cases it is possible to write |
| 321 | code that does what generics would enable, if less smoothly. |
| 322 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 323 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 324 | <p> |
| 325 | This remains an open issue. |
| 326 | </p> |
| 327 | |
| 328 | <h3 id="exceptions"> |
| 329 | Why does Go not have exceptions?</h3> |
| 330 | <p> |
| 331 | We believe that coupling exceptions to a control |
| 332 | structure, as in the <code>try-catch-finally</code> idiom, results in |
| 333 | convoluted code. It also tends to encourage programmers to label |
| 334 | too many ordinary errors, such as failing to open a file, as |
| 335 | exceptional. |
| 336 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 337 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 338 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 339 | Go takes a different approach. For plain error handling, Go's multi-value |
| 340 | returns make it easy to report an error without overloading the return value. |
Shenghou Ma | 97b13ac | 2012-03-07 08:15:47 +1100 | [diff] [blame] | 341 | <a href="/doc/articles/error_handling.html">A canonical error type, coupled |
| 342 | with Go's other features</a>, makes error handling pleasant but quite different |
| 343 | from that in other languages. |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 344 | </p> |
| 345 | |
| 346 | <p> |
| 347 | Go also has a couple |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 348 | of built-in functions to signal and recover from truly exceptional |
| 349 | conditions. The recovery mechanism is executed only as part of a |
| 350 | function's state being torn down after an error, which is sufficient |
| 351 | to handle catastrophe but requires no extra control structures and, |
| 352 | when used well, can result in clean error-handling code. |
| 353 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 354 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 355 | <p> |
Shenghou Ma | 97b13ac | 2012-03-07 08:15:47 +1100 | [diff] [blame] | 356 | See the <a href="/doc/articles/defer_panic_recover.html">Defer, Panic, and Recover</a> article for details. |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 357 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 358 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 359 | <h3 id="assertions"> |
| 360 | Why does Go not have assertions?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 361 | |
| 362 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 363 | Go doesn't provide assertions. They are undeniably convenient, but our |
| 364 | experience has been that programmers use them as a crutch to avoid thinking |
| 365 | about proper error handling and reporting. Proper error handling means that |
| 366 | servers continue operation after non-fatal errors instead of crashing. |
| 367 | Proper error reporting means that errors are direct and to the point, |
| 368 | saving the programmer from interpreting a large crash trace. Precise |
| 369 | errors are particularly important when the programmer seeing the errors is |
| 370 | not familiar with the code. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 371 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 372 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 373 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 374 | We understand that this is a point of contention. There are many things in |
| 375 | the Go language and libraries that differ from modern practices, simply |
| 376 | because we feel it's sometimes worth trying a different approach. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 377 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 378 | |
| 379 | <h3 id="csp"> |
| 380 | Why build concurrency on the ideas of CSP?</h3> |
| 381 | <p> |
| 382 | Concurrency and multi-threaded programming have a reputation |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 383 | for difficulty. We believe this is due partly to complex |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 384 | designs such as pthreads and partly to overemphasis on low-level details |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 385 | such as mutexes, condition variables, and memory barriers. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 386 | Higher-level interfaces enable much simpler code, even if there are still |
| 387 | mutexes and such under the covers. |
| 388 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 389 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 390 | <p> |
| 391 | One of the most successful models for providing high-level linguistic support |
| 392 | for concurrency comes from Hoare's Communicating Sequential Processes, or CSP. |
| 393 | Occam and Erlang are two well known languages that stem from CSP. |
| 394 | Go's concurrency primitives derive from a different part of the family tree |
| 395 | whose main contribution is the powerful notion of channels as first class objects. |
| 396 | </p> |
| 397 | |
| 398 | <h3 id="goroutines"> |
| 399 | Why goroutines instead of threads?</h3> |
| 400 | <p> |
| 401 | Goroutines are part of making concurrency easy to use. The idea, which has |
| 402 | been around for a while, is to multiplex independently executing |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 403 | functions—coroutines—onto a set of threads. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 404 | When a coroutine blocks, such as by calling a blocking system call, |
| 405 | the run-time automatically moves other coroutines on the same operating |
| 406 | system thread to a different, runnable thread so they won't be blocked. |
| 407 | The programmer sees none of this, which is the point. |
| 408 | The result, which we call goroutines, can be very cheap: unless they spend a lot of time |
| 409 | in long-running system calls, they cost little more than the memory |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 410 | for the stack, which is just a few kilobytes. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 411 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 412 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 413 | <p> |
| 414 | To make the stacks small, Go's run-time uses segmented stacks. A newly |
| 415 | minted goroutine is given a few kilobytes, which is almost always enough. |
| 416 | When it isn't, the run-time allocates (and frees) extension segments automatically. |
| 417 | The overhead averages about three cheap instructions per function call. |
| 418 | It is practical to create hundreds of thousands of goroutines in the same |
| 419 | address space. If goroutines were just threads, system resources would |
| 420 | run out at a much smaller number. |
| 421 | </p> |
| 422 | |
| 423 | <h3 id="atomic_maps"> |
| 424 | Why are map operations not defined to be atomic?</h3> |
| 425 | |
| 426 | <p> |
| 427 | After long discussion it was decided that the typical use of maps did not require |
| 428 | safe access from multiple threads, and in those cases where it did, the map was |
| 429 | probably part of some larger data structure or computation that was already |
| 430 | synchronized. Therefore requiring that all map operations grab a mutex would slow |
| 431 | down most programs and add safety to few. This was not an easy decision, |
| 432 | however, since it means uncontrolled map access can crash the program. |
| 433 | </p> |
| 434 | |
| 435 | <p> |
| 436 | The language does not preclude atomic map updates. When required, such |
| 437 | as when hosting an untrusted program, the implementation could interlock |
| 438 | map access. |
| 439 | </p> |
| 440 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 441 | <h2 id="types">Types</h2> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 442 | |
| 443 | <h3 id="Is_Go_an_object-oriented_language"> |
| 444 | Is Go an object-oriented language?</h3> |
| 445 | |
| 446 | <p> |
| 447 | Yes and no. Although Go has types and methods and allows an |
| 448 | object-oriented style of programming, there is no type hierarchy. |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 449 | The concept of “interface” in Go provides a different approach that |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 450 | we believe is easy to use and in some ways more general. There are |
| 451 | also ways to embed types in other types to provide something |
| 452 | analogous—but not identical—to subclassing. |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 453 | Moreover, methods in Go are more general than in C++ or Java: |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 454 | they can be defined for any sort of data, even built-in types such |
| 455 | as plain, “unboxed” integers. |
| 456 | They are not restricted to structs (classes). |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 457 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 458 | |
| 459 | <p> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 460 | Also, the lack of type hierarchy makes “objects” in Go feel much more |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 461 | lightweight than in languages such as C++ or Java. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 462 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 463 | |
| 464 | <h3 id="How_do_I_get_dynamic_dispatch_of_methods"> |
| 465 | How do I get dynamic dispatch of methods?</h3> |
| 466 | |
| 467 | <p> |
| 468 | The only way to have dynamically dispatched methods is through an |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 469 | interface. Methods on a struct or any other concrete type are always resolved statically. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 470 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 471 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 472 | <h3 id="inheritance"> |
| 473 | Why is there no type inheritance?</h3> |
| 474 | <p> |
| 475 | Object-oriented programming, at least in the best-known languages, |
| 476 | involves too much discussion of the relationships between types, |
| 477 | relationships that often could be derived automatically. Go takes a |
| 478 | different approach. |
| 479 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 480 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 481 | <p> |
| 482 | Rather than requiring the programmer to declare ahead of time that two |
| 483 | types are related, in Go a type automatically satisfies any interface |
| 484 | that specifies a subset of its methods. Besides reducing the |
| 485 | bookkeeping, this approach has real advantages. Types can satisfy |
| 486 | many interfaces at once, without the complexities of traditional |
| 487 | multiple inheritance. |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 488 | Interfaces can be very lightweight—an interface with |
| 489 | one or even zero methods can express a useful concept. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 490 | Interfaces can be added after the fact if a new idea comes along |
| 491 | or for testing—without annotating the original types. |
| 492 | Because there are no explicit relationships between types |
| 493 | and interfaces, there is no type hierarchy to manage or discuss. |
| 494 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 495 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 496 | <p> |
| 497 | It's possible to use these ideas to construct something analogous to |
| 498 | type-safe Unix pipes. For instance, see how <code>fmt.Fprintf</code> |
| 499 | enables formatted printing to any output, not just a file, or how the |
| 500 | <code>bufio</code> package can be completely separate from file I/O, |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 501 | or how the <code>image</code> packages generate compressed |
| 502 | image files. All these ideas stem from a single interface |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 503 | (<code>io.Writer</code>) representing a single method |
| 504 | (<code>Write</code>). And that's only scratching the surface. |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 505 | Go's interfaces have a profound influence on how programs are structured. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 506 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 507 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 508 | <p> |
| 509 | It takes some getting used to but this implicit style of type |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 510 | dependency is one of the most productive things about Go. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 511 | </p> |
| 512 | |
| 513 | <h3 id="methods_on_basics"> |
| 514 | Why is <code>len</code> a function and not a method?</h3> |
| 515 | <p> |
| 516 | We debated this issue but decided |
| 517 | implementing <code>len</code> and friends as functions was fine in practice and |
| 518 | didn't complicate questions about the interface (in the Go type sense) |
| 519 | of basic types. |
| 520 | </p> |
| 521 | |
| 522 | <h3 id="overloading"> |
| 523 | Why does Go not support overloading of methods and operators?</h3> |
| 524 | <p> |
| 525 | Method dispatch is simplified if it doesn't need to do type matching as well. |
| 526 | Experience with other languages told us that having a variety of |
| 527 | methods with the same name but different signatures was occasionally useful |
| 528 | but that it could also be confusing and fragile in practice. Matching only by name |
| 529 | and requiring consistency in the types was a major simplifying decision |
| 530 | in Go's type system. |
| 531 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 532 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 533 | <p> |
| 534 | Regarding operator overloading, it seems more a convenience than an absolute |
| 535 | requirement. Again, things are simpler without it. |
| 536 | </p> |
| 537 | |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 538 | <h3 id="implements_interface"> |
| 539 | Why doesn't Go have "implements" declarations?</h3> |
| 540 | |
| 541 | <p> |
| 542 | A Go type satisfies an interface by implementing the methods of that interface, |
| 543 | nothing more. This property allows interfaces to be defined and used without |
| 544 | having to modify existing code. It enables a kind of "duck typing" that |
| 545 | promotes separation of concerns and improves code re-use, and makes it easier |
| 546 | to build on patterns that emerge as the code develops. |
| 547 | The semantics of interfaces is one of the main reasons for Go's nimble, |
| 548 | lightweight feel. |
| 549 | </p> |
| 550 | |
| 551 | <p> |
| 552 | See the <a href="#inheritance">question on type inheritance</a> for more detail. |
| 553 | </p> |
| 554 | |
| 555 | <h3 id="guarantee_satisfies_interface"> |
| 556 | How can I guarantee my type satisfies an interface?</h3> |
| 557 | |
| 558 | <p> |
| 559 | You can ask the compiler to check that the type <code>T</code> implements the |
| 560 | interface <code>I</code> by attempting an assignment: |
| 561 | </p> |
| 562 | |
| 563 | <pre> |
| 564 | type T struct{} |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 565 | var _ I = T{} // Verify that T implements I. |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 566 | </pre> |
| 567 | |
| 568 | <p> |
| 569 | If <code>T</code> doesn't implement <code>I</code>, the mistake will be caught |
| 570 | at compile time. |
| 571 | </p> |
| 572 | |
| 573 | <p> |
| 574 | If you wish the users of an interface to explicitly declare that they implement |
| 575 | it, you can add a method with a descriptive name to the interface's method set. |
| 576 | For example: |
| 577 | </p> |
| 578 | |
| 579 | <pre> |
| 580 | type Fooer interface { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 581 | Foo() |
| 582 | ImplementsFooer() |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 583 | } |
| 584 | </pre> |
| 585 | |
| 586 | <p> |
| 587 | A type must then implement the <code>ImplementsFooer</code> method to be a |
Andrew Gerrand | 393ea2d | 2011-03-17 16:37:34 +1100 | [diff] [blame] | 588 | <code>Fooer</code>, clearly documenting the fact and announcing it in |
| 589 | <a href="/cmd/godoc/">godoc</a>'s output. |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 590 | </p> |
| 591 | |
| 592 | <pre> |
| 593 | type Bar struct{} |
| 594 | func (b Bar) ImplementsFooer() {} |
| 595 | func (b Bar) Foo() {} |
| 596 | </pre> |
| 597 | |
| 598 | <p> |
| 599 | Most code doesn't make use of such constraints, since they limit the utility of |
| 600 | the interface idea. Sometimes, though, they're necessary to resolve ambiguities |
| 601 | among similar interfaces. |
| 602 | </p> |
| 603 | |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 604 | <h3 id="t_and_equal_interface"> |
| 605 | Why doesn't type T satisfy the Equal interface?</h3> |
| 606 | |
| 607 | <p> |
| 608 | Consider this simple interface to represent an object that can compare |
| 609 | itself with another value: |
| 610 | </p> |
| 611 | |
| 612 | <pre> |
| 613 | type Equaler interface { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 614 | Equal(Equaler) bool |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 615 | } |
| 616 | </pre> |
| 617 | |
| 618 | <p> |
| 619 | and this type, <code>T</code>: |
| 620 | </p> |
| 621 | |
| 622 | <pre> |
| 623 | type T int |
| 624 | func (t T) Equal(u T) bool { return t == u } // does not satisfy Equaler |
| 625 | </pre> |
| 626 | |
| 627 | <p> |
| 628 | Unlike the analogous situation in some polymorphic type systems, |
| 629 | <code>T</code> does not implement <code>Equaler</code>. |
| 630 | The argument type of <code>T.Equal</code> is <code>T</code>, |
| 631 | not literally the required type <code>Equaler</code>. |
| 632 | </p> |
| 633 | |
| 634 | <p> |
| 635 | In Go, the type system does not promote the argument of |
| 636 | <code>Equal</code>; that is the programmer's responsibility, as |
| 637 | illustrated by the type <code>T2</code>, which does implement |
| 638 | <code>Equaler</code>: |
| 639 | </p> |
| 640 | |
| 641 | <pre> |
| 642 | type T2 int |
| 643 | func (t T2) Equal(u Equaler) bool { return t == u.(T2) } // satisfies Equaler |
| 644 | </pre> |
| 645 | |
| 646 | <p> |
| 647 | Even this isn't like other type systems, though, because in Go <em>any</em> |
| 648 | type that satisfies <code>Equaler</code> could be passed as the |
| 649 | argument to <code>T2.Equal</code>, and at run time we must |
| 650 | check that the argument is of type <code>T2</code>. |
| 651 | Some languages arrange to make that guarantee at compile time. |
| 652 | </p> |
| 653 | |
| 654 | <p> |
| 655 | A related example goes the other way: |
| 656 | </p> |
| 657 | |
| 658 | <pre> |
| 659 | type Opener interface { |
| 660 | Open(name) Reader |
| 661 | } |
| 662 | |
| 663 | func (t T3) Open() *os.File |
| 664 | </pre> |
| 665 | |
| 666 | <p> |
| 667 | In Go, <code>T3</code> does not satisfy <code>Opener</code>, |
| 668 | although it might in another language. |
| 669 | </p> |
| 670 | |
| 671 | <p> |
| 672 | While it is true that Go's type system does less for the programmer |
| 673 | in such cases, the lack of subtyping makes the rules about |
| 674 | interface satisfaction very easy to state: are the function's names |
| 675 | and signatures exactly those of the interface? |
| 676 | Go's rule is also easy to implement efficiently. |
| 677 | We feel these benefits offset the lack of |
| 678 | automatic type promotion. Should Go one day adopt some form of generic |
| 679 | typing, we expect there would be a way to express the idea of these |
| 680 | examples and also have them be statically checked. |
| 681 | </p> |
| 682 | |
Andrew Gerrand | 17805dd | 2011-06-18 20:31:38 +1000 | [diff] [blame] | 683 | <h3 id="convert_slice_of_interface"> |
| 684 | Can I convert a []T to an []interface{}?</h3> |
| 685 | |
| 686 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 687 | Not directly, because they do not have the same representation in memory. |
Andrew Gerrand | 17805dd | 2011-06-18 20:31:38 +1000 | [diff] [blame] | 688 | It is necessary to copy the elements individually to the destination |
| 689 | slice. This example converts a slice of <code>int</code> to a slice of |
| 690 | <code>interface{}</code>: |
| 691 | </p> |
| 692 | |
| 693 | <pre> |
| 694 | t := []int{1, 2, 3, 4} |
| 695 | s := make([]interface{}, len(t)) |
| 696 | for i, v := range t { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 697 | s[i] = v |
Andrew Gerrand | 17805dd | 2011-06-18 20:31:38 +1000 | [diff] [blame] | 698 | } |
| 699 | </pre> |
| 700 | |
Rob Pike | 1e0f97a | 2012-02-17 16:27:17 +1100 | [diff] [blame] | 701 | <h3 id="nil_error"> |
| 702 | Why is my nil error value not equal to nil? |
| 703 | </h3> |
| 704 | |
| 705 | <p> |
| 706 | Under the covers, interfaces are implemented as two elements, a type and a value. |
| 707 | The value, called the interface's dynamic value, |
| 708 | is an arbitrary concrete value and the type is that of the value. |
| 709 | For the <code>int</code> value 3, an interface value contains, |
| 710 | schematically, (<code>int</code>, <code>3</code>). |
| 711 | </p> |
| 712 | |
| 713 | <p> |
| 714 | An interface value is <code>nil</code> only if the inner value and type are both unset, |
| 715 | (<code>nil</code>, <code>nil</code>). |
| 716 | In particular, a <code>nil</code> interface will always hold a <code>nil</code> type. |
| 717 | If we store a pointer of type <code>*int</code> inside |
| 718 | an interface value, the inner type will be <code>*int</code> regardless of the value of the pointer: |
| 719 | (<code>*int</code>, <code>nil</code>). |
| 720 | Such an interface value will therefore be non-<code>nil</code> |
| 721 | <em>even when the pointer inside is</em> <code>nil</code>. |
| 722 | </p> |
| 723 | |
| 724 | <p> |
| 725 | This situation can be confusing, and often arises when a <code>nil</code> value is |
| 726 | stored inside an interface value such as an <code>error</code> return: |
| 727 | </p> |
| 728 | |
| 729 | <pre> |
| 730 | func returnsError() error { |
| 731 | var p *MyError = nil |
| 732 | if bad() { |
| 733 | p = ErrBad |
| 734 | } |
| 735 | return p // Will always return a non-nil error. |
| 736 | } |
| 737 | </pre> |
| 738 | |
| 739 | <p> |
| 740 | If all goes well, the function returns a <code>nil</code> <code>p</code>, |
| 741 | so the return value is an <code>error</code> interface |
| 742 | value holding (<code>*MyError</code>, <code>nil</code>). |
| 743 | This means that if the caller compares the returned error to <code>nil</code>, |
| 744 | it will always look as if there was an error even if nothing bad happened. |
| 745 | To return a proper <code>nil</code> <code>error</code> to the caller, |
| 746 | the function must return an explicit <code>nil</code>: |
| 747 | </p> |
| 748 | |
| 749 | |
| 750 | <pre> |
| 751 | func returnsError() error { |
| 752 | if bad() { |
| 753 | return ErrBad |
| 754 | } |
| 755 | return nil |
| 756 | } |
| 757 | </pre> |
| 758 | |
| 759 | <p> |
| 760 | It's a good idea for functions |
| 761 | that return errors always to use the <code>error</code> type in |
| 762 | their signature (as we did above) rather than a concrete type such |
| 763 | as <code>*MyError</code>, to help guarantee the error is |
| 764 | created correctly. As an example, |
| 765 | <a href="/pkg/os/#Open"><code>os.Open</code></a> |
| 766 | returns an <code>error</code> even though, if not <code>nil</code>, |
| 767 | it's always of concrete type |
| 768 | <a href="/pkg/os/#PathError"><code>*os.PathError</code></a>. |
| 769 | </p> |
| 770 | |
| 771 | <p> |
| 772 | Similar situations to those described here can arise whenever interfaces are used. |
| 773 | Just keep in mind that if any concrete value |
| 774 | has been stored in the interface, the interface will not be <code>nil</code>. |
| 775 | For more information, see |
Shenghou Ma | cb6c09a | 2012-03-01 14:54:35 +0800 | [diff] [blame] | 776 | <a href="/doc/articles/laws_of_reflection.html">The Laws of Reflection</a>. |
Rob Pike | 1e0f97a | 2012-02-17 16:27:17 +1100 | [diff] [blame] | 777 | </p> |
| 778 | |
| 779 | |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 780 | <h3 id="unions"> |
| 781 | Why are there no untagged unions, as in C?</h3> |
| 782 | |
| 783 | <p> |
| 784 | Untagged unions would violate Go's memory safety |
| 785 | guarantees. |
| 786 | </p> |
| 787 | |
| 788 | <h3 id="variant_types"> |
| 789 | Why does Go not have variant types?</h3> |
| 790 | |
| 791 | <p> |
| 792 | Variant types, also known as algebraic types, provide a way to specify |
| 793 | that a value might take one of a set of other types, but only those |
| 794 | types. A common example in systems programming would specify that an |
| 795 | error is, say, a network error, a security error or an application |
| 796 | error and allow the caller to discriminate the source of the problem |
| 797 | by examining the type of the error. Another example is a syntax tree |
| 798 | in which each node can be a different type: declaration, statement, |
| 799 | assignment and so on. |
| 800 | </p> |
| 801 | |
| 802 | <p> |
| 803 | We considered adding variant types to Go, but after discussion |
| 804 | decided to leave them out because they overlap in confusing ways |
| 805 | with interfaces. What would happen if the elements of a variant type |
| 806 | were themselves interfaces? |
| 807 | </p> |
| 808 | |
| 809 | <p> |
| 810 | Also, some of what variant types address is already covered by the |
| 811 | language. The error example is easy to express using an interface |
| 812 | value to hold the error and a type switch to discriminate cases. The |
| 813 | syntax tree example is also doable, although not as elegantly. |
| 814 | </p> |
| 815 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 816 | <h2 id="values">Values</h2> |
| 817 | |
| 818 | <h3 id="conversions"> |
| 819 | Why does Go not provide implicit numeric conversions?</h3> |
| 820 | <p> |
| 821 | The convenience of automatic conversion between numeric types in C is |
| 822 | outweighed by the confusion it causes. When is an expression unsigned? |
| 823 | How big is the value? Does it overflow? Is the result portable, independent |
| 824 | of the machine on which it executes? |
| 825 | It also complicates the compiler; “the usual arithmetic conversions” |
| 826 | are not easy to implement and inconsistent across architectures. |
| 827 | For reasons of portability, we decided to make things clear and straightforward |
| 828 | at the cost of some explicit conversions in the code. |
| 829 | The definition of constants in Go—arbitrary precision values free |
| 830 | of signedness and size annotations—ameliorates matters considerably, |
| 831 | though. |
| 832 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 833 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 834 | <p> |
| 835 | A related detail is that, unlike in C, <code>int</code> and <code>int64</code> |
| 836 | are distinct types even if <code>int</code> is a 64-bit type. The <code>int</code> |
| 837 | type is generic; if you care about how many bits an integer holds, Go |
| 838 | encourages you to be explicit. |
| 839 | </p> |
| 840 | |
| 841 | <h3 id="builtin_maps"> |
| 842 | Why are maps built in?</h3> |
| 843 | <p> |
| 844 | The same reason strings are: they are such a powerful and important data |
| 845 | structure that providing one excellent implementation with syntactic support |
| 846 | makes programming more pleasant. We believe that Go's implementation of maps |
| 847 | is strong enough that it will serve for the vast majority of uses. |
| 848 | If a specific application can benefit from a custom implementation, it's possible |
| 849 | to write one but it will not be as convenient syntactically; this seems a reasonable tradeoff. |
| 850 | </p> |
| 851 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 852 | <h3 id="map_keys"> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 853 | Why don't maps allow slices as keys?</h3> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 854 | <p> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 855 | Map lookup requires an equality operator, which slices do not implement. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 856 | They don't implement equality because equality is not well defined on such types; |
| 857 | there are multiple considerations involving shallow vs. deep comparison, pointer vs. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 858 | value comparison, how to deal with recursive types, and so on. |
| 859 | We may revisit this issue—and implementing equality for slices |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 860 | will not invalidate any existing programs—but without a clear idea of what |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 861 | equality of slices should mean, it was simpler to leave it out for now. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 862 | </p> |
| 863 | |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 864 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 865 | In Go 1, unlike prior releases, equality is defined for structs and arrays, so such |
| 866 | types can be used as map keys. Slices still do not have a definition of equality, though. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 867 | </p> |
| 868 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 869 | <h3 id="references"> |
| 870 | Why are maps, slices, and channels references while arrays are values?</h3> |
| 871 | <p> |
| 872 | There's a lot of history on that topic. Early on, maps and channels |
| 873 | were syntactically pointers and it was impossible to declare or use a |
| 874 | non-pointer instance. Also, we struggled with how arrays should work. |
| 875 | Eventually we decided that the strict separation of pointers and |
| 876 | values made the language harder to use. Introducing reference types, |
| 877 | including slices to handle the reference form of arrays, resolved |
| 878 | these issues. Reference types add some regrettable complexity to the |
| 879 | language but they have a large effect on usability: Go became a more |
| 880 | productive, comfortable language when they were introduced. |
| 881 | </p> |
| 882 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 883 | <h2 id="Writing_Code">Writing Code</h2> |
| 884 | |
| 885 | <h3 id="How_are_libraries_documented"> |
| 886 | How are libraries documented?</h3> |
| 887 | |
| 888 | <p> |
| 889 | There is a program, <code>godoc</code>, written in Go, that extracts |
| 890 | package documentation from the source code. It can be used on the |
| 891 | command line or on the web. An instance is running at |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 892 | <a href="http://golang.org/pkg/">http://golang.org/pkg/</a>. |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 893 | In fact, <code>godoc</code> implements the full site at |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 894 | <a href="http://golang.org/">http://golang.org/</a>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 895 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 896 | |
| 897 | <h3 id="Is_there_a_Go_programming_style_guide"> |
| 898 | Is there a Go programming style guide?</h3> |
| 899 | |
| 900 | <p> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 901 | Eventually, there may be a small number of rules to guide things |
| 902 | like naming, layout, and file organization. |
| 903 | The document <a href="effective_go.html">Effective Go</a> |
| 904 | contains some style advice. |
| 905 | More directly, the program <code>gofmt</code> is a pretty-printer |
| 906 | whose purpose is to enforce layout rules; it replaces the usual |
| 907 | compendium of do's and don'ts that allows interpretation. |
| 908 | All the Go code in the repository has been run through <code>gofmt</code>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 909 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 910 | |
| 911 | <h3 id="How_do_I_submit_patches_to_the_Go_libraries"> |
| 912 | How do I submit patches to the Go libraries?</h3> |
| 913 | |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 914 | <p> |
| 915 | The library sources are in <code>go/src/pkg</code>. |
| 916 | If you want to make a significant change, please discuss on the mailing list before embarking. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 917 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 918 | |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 919 | <p> |
| 920 | See the document |
| 921 | <a href="contribute.html">Contributing to the Go project</a> |
| 922 | for more information about how to proceed. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 923 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 924 | |
| 925 | <h2 id="Pointers">Pointers and Allocation</h2> |
| 926 | |
| 927 | <h3 id="pass_by_value"> |
| 928 | When are function parameters passed by value?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 929 | |
| 930 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 931 | As in all languages in the C family, everything in Go is passed by value. |
| 932 | That is, a function always gets a copy of the |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 933 | thing being passed, as if there were an assignment statement assigning the |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 934 | value to the parameter. For instance, passing an <code>int</code> value |
| 935 | to a function makes a copy of the <code>int</code>, and passing a pointer |
| 936 | value makes a copy of the pointer, but not the data it points to. |
| 937 | (See the next section for a discussion of how this affects method receivers.) |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 938 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 939 | |
| 940 | <p> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 941 | Map and slice values behave like pointers: they are descriptors that |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 942 | contain pointers to the underlying map or slice data. Copying a map or |
| 943 | slice value doesn't copy the data it points to. Copying an interface value |
| 944 | makes a copy of the thing stored in the interface value. If the interface |
| 945 | value holds a struct, copying the interface value makes a copy of the |
| 946 | struct. If the interface value holds a pointer, copying the interface value |
| 947 | makes a copy of the pointer, but again not the data it points to. |
| 948 | </p> |
| 949 | |
| 950 | <h3 id="methods_on_values_or_pointers"> |
| 951 | Should I define methods on values or pointers?</h3> |
| 952 | |
| 953 | <pre> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 954 | func (s *MyStruct) pointerMethod() { } // method on pointer |
| 955 | func (s MyStruct) valueMethod() { } // method on value |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 956 | </pre> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 957 | |
| 958 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 959 | For programmers unaccustomed to pointers, the distinction between these |
| 960 | two examples can be confusing, but the situation is actually very simple. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 961 | When defining a method on a type, the receiver (<code>s</code> in the above |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 962 | examples) behaves exactly as if it were an argument to the method. |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 963 | Whether to define the receiver as a value or as a pointer is the same |
| 964 | question, then, as whether a function argument should be a value or |
| 965 | a pointer. |
| 966 | There are several considerations. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 967 | </p> |
| 968 | |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 969 | <p> |
| 970 | First, and most important, does the method need to modify the |
| 971 | receiver? |
| 972 | If it does, the receiver <em>must</em> be a pointer. |
| 973 | (Slices and maps are reference types, so their story is a little |
| 974 | more subtle, but for instance to change the length of a slice |
| 975 | in a method the receiver must still be a pointer.) |
| 976 | In the examples above, if <code>pointerMethod</code> modifies |
| 977 | the fields of <code>s</code>, |
| 978 | the caller will see those changes, but <code>valueMethod</code> |
| 979 | is called with a copy of the caller's argument (that's the definition |
| 980 | of passing a value), so changes it makes will be invisible to the caller. |
| 981 | </p> |
| 982 | |
| 983 | <p> |
| 984 | By the way, pointer receivers are identical to the situation in Java, |
| 985 | although in Java the pointers are hidden under the covers; it's Go's |
| 986 | value receivers that are unusual. |
| 987 | </p> |
| 988 | |
| 989 | <p> |
| 990 | Second is the consideration of efficiency. If the receiver is large, |
| 991 | a big <code>struct</code> for instance, it will be much cheaper to |
| 992 | use a pointer receiver. |
| 993 | </p> |
| 994 | |
| 995 | <p> |
| 996 | Next is consistency. If some of the methods of the type must have |
| 997 | pointer receivers, the rest should too, so the method set is |
| 998 | consistent regardless of how the type is used. |
| 999 | See the section on <a href="#different_method_sets">method sets</a> |
| 1000 | for details. |
| 1001 | </p> |
| 1002 | |
| 1003 | <p> |
| 1004 | For types such as basic types, slices, and small <code>structs</code>, |
| 1005 | a value receiver is very cheap so unless the semantics of the method |
| 1006 | requires a pointer, a value receiver is efficient and clear. |
| 1007 | </p> |
| 1008 | |
| 1009 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1010 | <h3 id="new_and_make"> |
| 1011 | What's the difference between new and make?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1012 | |
| 1013 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1014 | In short: <code>new</code> allocates memory, <code>make</code> initializes |
| 1015 | the slice, map, and channel types. |
| 1016 | </p> |
| 1017 | |
| 1018 | <p> |
| 1019 | See the <a href="/doc/effective_go.html#allocation_new">relevant section |
| 1020 | of Effective Go</a> for more details. |
| 1021 | </p> |
| 1022 | |
Andrew Gerrand | affd1ba | 2010-12-09 08:59:29 +1100 | [diff] [blame] | 1023 | <h3 id="q_int_sizes"> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1024 | Why is <code>int</code> 32 bits on 64 bit machines?</h3> |
| 1025 | |
| 1026 | <p> |
Rob Pike | 80e25fc | 2011-01-19 23:07:38 -0500 | [diff] [blame] | 1027 | The sizes of <code>int</code> and <code>uint</code> are implementation-specific |
| 1028 | but the same as each other on a given platform. |
Andrew Gerrand | 2a5879d | 2012-03-20 13:50:05 +1100 | [diff] [blame] | 1029 | The 64 bit Go compilers (both gc and gccgo) use a 32 bit representation for |
Rob Pike | 80e25fc | 2011-01-19 23:07:38 -0500 | [diff] [blame] | 1030 | <code>int</code>. Code that relies on a particular |
| 1031 | size of value should use an explicitly sized type, like <code>int64</code>. |
| 1032 | On the other hand, floating-point scalars and complex |
| 1033 | numbers are always sized: <code>float32</code>, <code>complex64</code>, |
| 1034 | etc., because programmers should be aware of precision when using |
| 1035 | floating-point numbers. |
| 1036 | The default size of a floating-point constant is <code>float64</code>. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1037 | </p> |
| 1038 | |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1039 | <p> |
| 1040 | At the moment, all implementations use 32-bit ints, an essentially arbitrary decision. |
| 1041 | However, we expect that <code>int</code> will be increased to 64 bits on 64-bit |
| 1042 | architectures in a future release of Go. |
| 1043 | </p> |
| 1044 | |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1045 | <h3 id="stack_or_heap"> |
| 1046 | How do I know whether a variable is allocated on the heap or the stack?</h3> |
| 1047 | |
| 1048 | <p> |
| 1049 | From a correctness standpoint, you don't need to know. |
| 1050 | Each variable in Go exists as long as there are references to it. |
| 1051 | The storage location chosen by the implementation is irrelevant to the |
| 1052 | semantics of the language. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1053 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1054 | |
| 1055 | <p> |
| 1056 | The storage location does have an effect on writing efficient programs. |
| 1057 | When possible, the Go compilers will allocate variables that are |
| 1058 | local to a function in that function's stack frame. However, if the |
| 1059 | compiler cannot prove that the variable is not referenced after the |
| 1060 | function returns, then the compiler must allocate the variable on the |
| 1061 | garbage-collected heap to avoid dangling pointer errors. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1062 | Also, if a local variable is very large, it might make more sense |
| 1063 | to store it on the heap rather than the stack. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1064 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1065 | |
| 1066 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1067 | In the current compilers, if a variable has its address taken, that variable |
| 1068 | is a candidate for allocation on the heap. However, a basic <em>escape |
| 1069 | analysis</em> recognizes some cases when such variables will not |
| 1070 | live past the return from the function and can reside on the stack. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1071 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1072 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1073 | <h2 id="Concurrency">Concurrency</h2> |
| 1074 | |
| 1075 | <h3 id="What_operations_are_atomic_What_about_mutexes"> |
| 1076 | What operations are atomic? What about mutexes?</h3> |
| 1077 | |
| 1078 | <p> |
| 1079 | We haven't fully defined it all yet, but some details about atomicity are |
Andrew Gerrand | a22b0f8 | 2012-03-05 15:30:27 +1100 | [diff] [blame] | 1080 | available in the <a href="/ref/mem">Go Memory Model specification</a>. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1081 | </p> |
| 1082 | |
| 1083 | <p> |
| 1084 | Regarding mutexes, the <a href="/pkg/sync">sync</a> |
| 1085 | package implements them, but we hope Go programming style will |
| 1086 | encourage people to try higher-level techniques. In particular, consider |
| 1087 | structuring your program so that only one goroutine at a time is ever |
| 1088 | responsible for a particular piece of data. |
| 1089 | </p> |
| 1090 | |
| 1091 | <p> |
| 1092 | Do not communicate by sharing memory. Instead, share memory by communicating. |
| 1093 | </p> |
| 1094 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1095 | <p> |
| 1096 | See the <a href="/doc/codewalk/sharemem/">Share Memory By Communicating</a> code walk and its <a href="http://blog.golang.org/2010/07/share-memory-by-communicating.html">associated article</a> for a detailed discussion of this concept. |
| 1097 | </p> |
| 1098 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1099 | <h3 id="Why_no_multi_CPU"> |
| 1100 | Why doesn't my multi-goroutine program use multiple CPUs?</h3> |
| 1101 | |
| 1102 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1103 | You must set the <code>GOMAXPROCS</code> shell environment variable |
| 1104 | or use the similarly-named <a href="/pkg/runtime/#GOMAXPROCS"><code>function</code></a> |
| 1105 | of the runtime package to allow the |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1106 | run-time support to utilize more than one OS thread. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1107 | </p> |
| 1108 | |
| 1109 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1110 | Programs that perform parallel computation should benefit from an increase in |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1111 | <code>GOMAXPROCS</code>. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1112 | </p> |
| 1113 | |
| 1114 | <h3 id="Why_GOMAXPROCS"> |
| 1115 | Why does using <code>GOMAXPROCS</code> > 1 sometimes make my program |
| 1116 | slower?</h3> |
| 1117 | |
| 1118 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1119 | It depends on the nature of your program. |
Rob Pike | 01afb79 | 2012-01-26 14:44:38 -0800 | [diff] [blame] | 1120 | Problems that are intrinsically sequential cannot be sped up by adding |
| 1121 | more goroutines. |
| 1122 | Concurrency only becomes parallelism when the problem is |
| 1123 | intrinsically parallel. |
| 1124 | </p> |
| 1125 | |
| 1126 | <p> |
| 1127 | In practical terms, programs that spend more time |
| 1128 | communicating on channels than doing computation |
| 1129 | will experience performance degradation when using |
| 1130 | multiple OS threads. |
| 1131 | This is because sending data between threads involves switching |
| 1132 | contexts, which has significant cost. |
Andrew Gerrand | a22b0f8 | 2012-03-05 15:30:27 +1100 | [diff] [blame] | 1133 | For instance, the <a href="/ref/spec#An_example_package">prime sieve example</a> |
Rob Pike | 01afb79 | 2012-01-26 14:44:38 -0800 | [diff] [blame] | 1134 | from the Go specification has no significant parallelism although it launches many |
| 1135 | goroutines; increasing <code>GOMAXPROCS</code> is more likely to slow it down than |
| 1136 | to speed it up. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1137 | </p> |
| 1138 | |
| 1139 | <p> |
Rob Pike | 966bf71 | 2011-03-01 13:54:22 -0800 | [diff] [blame] | 1140 | Go's goroutine scheduler is not as good as it needs to be. In future, it |
| 1141 | should recognize such cases and optimize its use of OS threads. For now, |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1142 | <code>GOMAXPROCS</code> should be set on a per-application basis. |
| 1143 | </p> |
| 1144 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1145 | <h2 id="Functions_methods">Functions and Methods</h2> |
| 1146 | |
| 1147 | <h3 id="different_method_sets"> |
| 1148 | Why do T and *T have different method sets?</h3> |
| 1149 | |
| 1150 | <p> |
Andrew Gerrand | a22b0f8 | 2012-03-05 15:30:27 +1100 | [diff] [blame] | 1151 | From the <a href="/ref/spec#Types">Go Spec</a>: |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1152 | </p> |
| 1153 | |
| 1154 | <blockquote> |
| 1155 | The method set of any other named type <code>T</code> consists of all methods |
| 1156 | with receiver type <code>T</code>. The method set of the corresponding pointer |
| 1157 | type <code>*T</code> is the set of all methods with receiver <code>*T</code> or |
| 1158 | <code>T</code> (that is, it also contains the method set of <code>T</code>). |
| 1159 | </blockquote> |
| 1160 | |
| 1161 | <p> |
| 1162 | If an interface value contains a pointer <code>*T</code>, |
| 1163 | a method call can obtain a value by dereferencing the pointer, |
| 1164 | but if an interface value contains a value <code>T</code>, |
| 1165 | there is no useful way for a method call to obtain a pointer. |
| 1166 | </p> |
| 1167 | |
| 1168 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1169 | Even in cases where the compiler could take the address of a value |
| 1170 | to pass to the method, if the method modifies the value the changes |
| 1171 | will be lost in the caller. |
| 1172 | As a common example, this code: |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1173 | </p> |
| 1174 | |
| 1175 | <pre> |
| 1176 | var buf bytes.Buffer |
| 1177 | io.Copy(buf, os.Stdin) |
| 1178 | </pre> |
| 1179 | |
| 1180 | <p> |
| 1181 | would copy standard input into a <i>copy</i> of <code>buf</code>, |
| 1182 | not into <code>buf</code> itself. |
| 1183 | This is almost never the desired behavior. |
| 1184 | </p> |
| 1185 | |
| 1186 | <h3 id="closures_and_goroutines"> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1187 | What happens with closures running as goroutines?</h3> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1188 | |
| 1189 | <p> |
| 1190 | Some confusion may arise when using closures with concurrency. |
| 1191 | Consider the following program: |
| 1192 | </p> |
| 1193 | |
| 1194 | <pre> |
| 1195 | func main() { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1196 | done := make(chan bool) |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1197 | |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1198 | values := []string{"a", "b", "c"} |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1199 | for _, v := range values { |
| 1200 | go func() { |
| 1201 | fmt.Println(v) |
| 1202 | done <- true |
| 1203 | }() |
| 1204 | } |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1205 | |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1206 | // wait for all goroutines to complete before exiting |
| 1207 | for _ = range values { |
| 1208 | <-done |
| 1209 | } |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1210 | } |
| 1211 | </pre> |
| 1212 | |
| 1213 | <p> |
| 1214 | One might mistakenly expect to see <code>a, b, c</code> as the output. |
| 1215 | What you'll probably see instead is <code>c, c, c</code>. This is because |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1216 | each iteration of the loop uses the same instance of the variable <code>v</code>, so |
| 1217 | each closure shares that single variable. When the closure runs, it prints the |
| 1218 | value of <code>v</code> at the time <code>fmt.Println</code> is executed, |
| 1219 | but <code>v</code> may have been modified since the goroutine was launched. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1220 | </p> |
| 1221 | |
| 1222 | <p> |
| 1223 | To bind the value of <code>v</code> to each closure as they are launched, one |
| 1224 | could modify the inner loop to read: |
| 1225 | </p> |
| 1226 | |
| 1227 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1228 | for _, v := range values { |
| 1229 | go func(<b>u</b> string) { |
| 1230 | fmt.Println(<b>u</b>) |
| 1231 | done <- true |
| 1232 | }(<b>v</b>) |
| 1233 | } |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1234 | </pre> |
| 1235 | |
| 1236 | <p> |
| 1237 | In this example, the value of <code>v</code> is passed as an argument to the |
| 1238 | anonymous function. That value is then accessible inside the function as |
| 1239 | the variable <code>u</code>. |
| 1240 | </p> |
| 1241 | |
| 1242 | <h2 id="Control_flow">Control flow</h2> |
| 1243 | |
| 1244 | <h3 id="Does_Go_have_a_ternary_form"> |
| 1245 | Does Go have the <code>?:</code> operator?</h3> |
| 1246 | |
| 1247 | <p> |
| 1248 | There is no ternary form in Go. You may use the following to achieve the same |
| 1249 | result: |
| 1250 | </p> |
| 1251 | |
| 1252 | <pre> |
| 1253 | if expr { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1254 | n = trueVal |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1255 | } else { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1256 | n = falseVal |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1257 | } |
| 1258 | </pre> |
| 1259 | |
| 1260 | <h2 id="Packages_Testing">Packages and Testing</h2> |
| 1261 | |
| 1262 | <h3 id="How_do_I_create_a_multifile_package"> |
| 1263 | How do I create a multifile package?</h3> |
| 1264 | |
| 1265 | <p> |
| 1266 | Put all the source files for the package in a directory by themselves. |
| 1267 | Source files can refer to items from different files at will; there is |
| 1268 | no need for forward declarations or a header file. |
| 1269 | </p> |
| 1270 | |
| 1271 | <p> |
| 1272 | Other than being split into multiple files, the package will compile and test |
| 1273 | just like a single-file package. |
| 1274 | </p> |
| 1275 | |
| 1276 | <h3 id="How_do_I_write_a_unit_test"> |
| 1277 | How do I write a unit test?</h3> |
| 1278 | |
| 1279 | <p> |
| 1280 | Create a new file ending in <code>_test.go</code> in the same directory |
| 1281 | as your package sources. Inside that file, <code>import "testing"</code> |
| 1282 | and write functions of the form |
| 1283 | </p> |
| 1284 | |
| 1285 | <pre> |
| 1286 | func TestFoo(t *testing.T) { |
| 1287 | ... |
| 1288 | } |
| 1289 | </pre> |
| 1290 | |
| 1291 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1292 | Run <code>go test</code> in that directory. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1293 | That script finds the <code>Test</code> functions, |
| 1294 | builds a test binary, and runs it. |
| 1295 | </p> |
| 1296 | |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1297 | <p>See the <a href="/doc/code.html">How to Write Go Code</a> document, |
| 1298 | the <a href="/pkg/testing/"><code>testing</code></a> package |
| 1299 | and the <a href="/cmd/go/#Test_packages"><code>go test</code></a> subcommand for more details. |
| 1300 | </p> |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1301 | |
Rob Pike | f6615f1 | 2011-11-09 13:19:23 -0800 | [diff] [blame] | 1302 | <h3 id="testing_framework"> |
| 1303 | Where is my favorite helper function for testing?</h3> |
| 1304 | |
| 1305 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1306 | Go's standard <a href="/pkg/testing/"><code>testing</code></a> package makes it easy to write unit tests, but it lacks |
Rob Pike | f6615f1 | 2011-11-09 13:19:23 -0800 | [diff] [blame] | 1307 | features provided in other language's testing frameworks such as assertion functions. |
| 1308 | An <a href="#assertions">earlier section</a> of this document explained why Go |
| 1309 | doesn't have assertions, and |
| 1310 | the same arguments apply to the use of <code>assert</code> in tests. |
| 1311 | Proper error handling means letting other tests run after one has failed, so |
| 1312 | that the person debugging the failure gets a complete picture of what is |
| 1313 | wrong. It is more useful for a test to report that |
| 1314 | <code>isPrime</code> gives the wrong answer for 2, 3, 5, and 7 (or for |
| 1315 | 2, 4, 8, and 16) than to report that <code>isPrime</code> gives the wrong |
| 1316 | answer for 2 and therefore no more tests were run. The programmer who |
| 1317 | triggers the test failure may not be familiar with the code that fails. |
| 1318 | Time invested writing a good error message now pays off later when the |
| 1319 | test breaks. |
| 1320 | </p> |
| 1321 | |
| 1322 | <p> |
| 1323 | A related point is that testing frameworks tend to develop into mini-languages |
| 1324 | of their own, with conditionals and controls and printing mechanisms, |
| 1325 | but Go already has all those capabilities; why recreate them? |
| 1326 | We'd rather write tests in Go; it's one fewer language to learn and the |
| 1327 | approach keeps the tests straightforward and easy to understand. |
| 1328 | </p> |
| 1329 | |
| 1330 | <p> |
| 1331 | If the amount of extra code required to write |
| 1332 | good errors seems repetitive and overwhelming, the test might work better if |
| 1333 | table-driven, iterating over a list of inputs and outputs defined |
| 1334 | in a data structure (Go has excellent support for data structure literals). |
| 1335 | The work to write a good test and good error messages will then be amortized over many |
| 1336 | test cases. The standard Go library is full of illustrative examples, such as in |
Shenghou Ma | 97b13ac | 2012-03-07 08:15:47 +1100 | [diff] [blame] | 1337 | <a href="/src/pkg/fmt/fmt_test.go">the formatting tests for the <code>fmt</code> package</a>. |
Rob Pike | f6615f1 | 2011-11-09 13:19:23 -0800 | [diff] [blame] | 1338 | </p> |
| 1339 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1340 | |
| 1341 | <h2 id="Implementation">Implementation</h2> |
| 1342 | |
| 1343 | <h3 id="What_compiler_technology_is_used_to_build_the_compilers"> |
| 1344 | What compiler technology is used to build the compilers?</h3> |
| 1345 | |
| 1346 | <p> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 1347 | <code>Gccgo</code> has a C++ front-end with a recursive descent parser coupled to the |
| 1348 | standard GCC back end. <code>Gc</code> is written in C using |
| 1349 | <code>yacc</code>/<code>bison</code> for the parser. |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1350 | Although it's a new program, it fits in the Plan 9 C compiler suite |
| 1351 | (<a href="http://plan9.bell-labs.com/sys/doc/compiler.html">http://plan9.bell-labs.com/sys/doc/compiler.html</a>) |
Shenghou Ma | c3eaef7 | 2012-03-17 15:53:40 +0800 | [diff] [blame] | 1352 | and uses a variant of the Plan 9 loader to generate ELF/Mach-O/PE binaries. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1353 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1354 | |
| 1355 | <p> |
Andrew Gerrand | 2a5879d | 2012-03-20 13:50:05 +1100 | [diff] [blame] | 1356 | We considered writing <code>gc</code>, the original Go compiler, in Go itself but |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1357 | elected not to do so because of the difficulties of bootstrapping and |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 1358 | especially of open source distribution—you'd need a Go compiler to |
| 1359 | set up a Go environment. <code>Gccgo</code>, which came later, makes it possible to |
| 1360 | consider writing a compiler in Go, which might well happen. (Go would be a |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1361 | fine language in which to implement a compiler; a native lexer and |
Shenghou Ma | c3eaef7 | 2012-03-17 15:53:40 +0800 | [diff] [blame] | 1362 | parser are already available in the <a href="/pkg/go/"><code>go</code></a> package.) |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1363 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1364 | |
| 1365 | <p> |
Andrew Gerrand | 2a5879d | 2012-03-20 13:50:05 +1100 | [diff] [blame] | 1366 | We also considered using LLVM for <code>gc</code> but we felt it was too large and |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1367 | slow to meet our performance goals. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1368 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1369 | |
Rob Pike | 966bf71 | 2011-03-01 13:54:22 -0800 | [diff] [blame] | 1370 | <h3 id="How_is_the_run_time_support_implemented"> |
| 1371 | How is the run-time support implemented?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1372 | |
| 1373 | <p> |
Rob Pike | 966bf71 | 2011-03-01 13:54:22 -0800 | [diff] [blame] | 1374 | Again due to bootstrapping issues, the run-time code is mostly in C (with a |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1375 | tiny bit of assembler) although Go is capable of implementing most of |
Rob Pike | 966bf71 | 2011-03-01 13:54:22 -0800 | [diff] [blame] | 1376 | it now. <code>Gccgo</code>'s run-time support uses <code>glibc</code>. |
David Symonds | be96fa5 | 2011-07-31 12:59:58 +1000 | [diff] [blame] | 1377 | <code>Gc</code> uses a custom library to keep the footprint under |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 1378 | control; it is |
| 1379 | compiled with a version of the Plan 9 C compiler that supports |
| 1380 | segmented stacks for goroutines. |
Ian Lance Taylor | ebc4007 | 2012-02-17 05:59:15 -0800 | [diff] [blame] | 1381 | The <code>gccgo</code> compiler implements segmented |
| 1382 | stacks on Linux only, supported by recent modifications to the gold linker. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1383 | </p> |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1384 | |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1385 | <h3 id="Why_is_my_trivial_program_such_a_large_binary"> |
| 1386 | Why is my trivial program such a large binary?</h3> |
| 1387 | |
| 1388 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1389 | The linkers in the gc tool chain (<code>5l</code>, <code>6l</code>, and <code>8l</code>) |
| 1390 | do static linking. All Go binaries therefore include the Go |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1391 | run-time, along with the run-time type information necessary to support dynamic |
| 1392 | type checks, reflection, and even panic-time stack traces. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1393 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1394 | |
| 1395 | <p> |
Rob Pike | 0130a31 | 2012-03-07 15:29:26 +1100 | [diff] [blame] | 1396 | A simple C "hello, world" program compiled and linked statically using gcc |
| 1397 | on Linux is around 750 kB, |
| 1398 | including an implementation of <code>printf</code>. |
| 1399 | An equivalent Go program using <code>fmt.Printf</code> |
| 1400 | is around 1.2 MB, but |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1401 | that includes more powerful run-time support. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1402 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1403 | |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 1404 | <h3 id="unused_variables_and_imports"> |
| 1405 | Can I stop these complaints about my unused variable/import?</h3> |
| 1406 | |
| 1407 | <p> |
| 1408 | The presence of an unused variable may indicate a bug, while |
| 1409 | unused imports just slow down compilation. |
| 1410 | Accumulate enough unused imports in your code tree and |
| 1411 | things can get very slow. |
| 1412 | For these reasons, Go allows neither. |
| 1413 | </p> |
| 1414 | |
| 1415 | <p> |
| 1416 | When developing code, it's common to create these situations |
| 1417 | temporarily and it can be annoying to have to edit them out before the |
| 1418 | program will compile. |
| 1419 | </p> |
| 1420 | |
| 1421 | <p> |
| 1422 | Some have asked for a compiler option to turn those checks off |
| 1423 | or at least reduce them to warnings. |
| 1424 | Such an option has not been added, though, |
| 1425 | because compiler options should not affect the semantics of the |
| 1426 | language and because the Go compiler does not report warnings, only |
| 1427 | errors that prevent compilation. |
| 1428 | </p> |
| 1429 | |
| 1430 | <p> |
| 1431 | There are two reasons for having no warnings. First, if it's worth |
| 1432 | complaining about, it's worth fixing in the code. (And if it's not |
| 1433 | worth fixing, it's not worth mentioning.) Second, having the compiler |
| 1434 | generate warnings encourages the implementation to warn about weak |
| 1435 | cases that can make compilation noisy, masking real errors that |
| 1436 | <em>should</em> be fixed. |
| 1437 | </p> |
| 1438 | |
| 1439 | <p> |
| 1440 | It's easy to address the situation, though. Use the blank identifier |
| 1441 | to let unused things persist while you're developing. |
| 1442 | </p> |
| 1443 | |
| 1444 | <pre> |
| 1445 | import "unused" |
| 1446 | |
| 1447 | // This declaration marks the import as used by referencing an |
| 1448 | // item from the package. |
| 1449 | var _ = unused.Item // TODO: Delete before committing! |
| 1450 | |
| 1451 | func main() { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1452 | debugData := debug.Profile() |
| 1453 | _ = debugData // Used only during debugging. |
| 1454 | .... |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 1455 | } |
| 1456 | </pre> |
| 1457 | |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1458 | <h2 id="Performance">Performance</h2> |
| 1459 | |
| 1460 | <h3 id="Why_does_Go_perform_badly_on_benchmark_x"> |
| 1461 | Why does Go perform badly on benchmark X?</h3> |
| 1462 | |
| 1463 | <p> |
| 1464 | One of Go's design goals is to approach the performance of C for comparable |
| 1465 | programs, yet on some benchmarks it does quite poorly, including several |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1466 | in <a href="/test/bench/shootout/">test/bench/shootout</a>. The slowest depend on libraries |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1467 | for which versions of comparable performance are not available in Go. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1468 | For instance, <a href="/test/bench/shootout/pidigits.go">pidigits.go</a> |
| 1469 | depends on a multi-precision math package, and the C |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1470 | versions, unlike Go's, use <a href="http://gmplib.org/">GMP</a> (which is |
| 1471 | written in optimized assembler). |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1472 | Benchmarks that depend on regular expressions |
| 1473 | (<a href="/test/bench/shootout/regex-dna.go">regex-dna.go</a>, for instance) are |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1474 | essentially comparing Go's native <a href="/pkg/regexp">regexp package</a> to |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1475 | mature, highly optimized regular expression libraries like PCRE. |
| 1476 | </p> |
| 1477 | |
| 1478 | <p> |
| 1479 | Benchmark games are won by extensive tuning and the Go versions of most |
| 1480 | of the benchmarks need attention. If you measure comparable C |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1481 | and Go programs |
| 1482 | (<a href="/test/bench/shootout/reverse-complement.go">reverse-complement.go</a> is one example), you'll see the two |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1483 | languages are much closer in raw performance than this suite would |
| 1484 | indicate. |
| 1485 | </p> |
| 1486 | |
| 1487 | <p> |
| 1488 | Still, there is room for improvement. The compilers are good but could be |
Andrew Gerrand | 0542774 | 2010-04-23 10:02:10 +1000 | [diff] [blame] | 1489 | better, many libraries need major performance work, and the garbage collector |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1490 | isn't fast enough yet. (Even if it were, taking care not to generate unnecessary |
| 1491 | garbage can have a huge effect.) |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1492 | </p> |
| 1493 | |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1494 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1495 | In any case, Go can often be very competitive. |
| 1496 | There has been significant improvement in the performance of many programs |
| 1497 | as the language and tools have developed. |
| 1498 | See the blog post about |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1499 | <a href="http://blog.golang.org/2011/06/profiling-go-programs.html">profiling |
| 1500 | Go programs</a> for an informative example. |
| 1501 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1502 | <h2 id="change_from_c">Changes from C</h2> |
| 1503 | |
| 1504 | <h3 id="different_syntax"> |
| 1505 | Why is the syntax so different from C?</h3> |
| 1506 | <p> |
| 1507 | Other than declaration syntax, the differences are not major and stem |
| 1508 | from two desires. First, the syntax should feel light, without too |
| 1509 | many mandatory keywords, repetition, or arcana. Second, the language |
| 1510 | has been designed to be easy to analyze |
| 1511 | and can be parsed without a symbol table. This makes it much easier |
| 1512 | to build tools such as debuggers, dependency analyzers, automated |
| 1513 | documentation extractors, IDE plug-ins, and so on. C and its |
| 1514 | descendants are notoriously difficult in this regard. |
| 1515 | </p> |
| 1516 | |
| 1517 | <h3 id="declarations_backwards"> |
| 1518 | Why are declarations backwards?</h3> |
| 1519 | <p> |
| 1520 | They're only backwards if you're used to C. In C, the notion is that a |
| 1521 | variable is declared like an expression denoting its type, which is a |
| 1522 | nice idea, but the type and expression grammars don't mix very well and |
| 1523 | the results can be confusing; consider function pointers. Go mostly |
| 1524 | separates expression and type syntax and that simplifies things (using |
| 1525 | prefix <code>*</code> for pointers is an exception that proves the rule). In C, |
| 1526 | the declaration |
| 1527 | </p> |
| 1528 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1529 | int* a, b; |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1530 | </pre> |
| 1531 | <p> |
| 1532 | declares <code>a</code> to be a pointer but not <code>b</code>; in Go |
| 1533 | </p> |
| 1534 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1535 | var a, b *int |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1536 | </pre> |
| 1537 | <p> |
| 1538 | declares both to be pointers. This is clearer and more regular. |
| 1539 | Also, the <code>:=</code> short declaration form argues that a full variable |
| 1540 | declaration should present the same order as <code>:=</code> so |
| 1541 | </p> |
| 1542 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1543 | var a uint64 = 1 |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1544 | </pre> |
Stefan Nilsson | c50074e | 2012-02-29 15:07:52 -0800 | [diff] [blame] | 1545 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1546 | has the same effect as |
Stefan Nilsson | c50074e | 2012-02-29 15:07:52 -0800 | [diff] [blame] | 1547 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1548 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1549 | a := uint64(1) |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1550 | </pre> |
| 1551 | <p> |
| 1552 | Parsing is also simplified by having a distinct grammar for types that |
| 1553 | is not just the expression grammar; keywords such as <code>func</code> |
| 1554 | and <code>chan</code> keep things clear. |
| 1555 | </p> |
| 1556 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1557 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1558 | See the article about |
Francisco Souza | 6033a48 | 2012-03-13 14:46:08 +1100 | [diff] [blame] | 1559 | <a href="/doc/articles/gos_declaration_syntax.html">Go's Declaration Syntax</a> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1560 | for more details. |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1561 | </p> |
| 1562 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1563 | <h3 id="no_pointer_arithmetic"> |
| 1564 | Why is there no pointer arithmetic?</h3> |
| 1565 | <p> |
| 1566 | Safety. Without pointer arithmetic it's possible to create a |
| 1567 | language that can never derive an illegal address that succeeds |
| 1568 | incorrectly. Compiler and hardware technology have advanced to the |
| 1569 | point where a loop using array indices can be as efficient as a loop |
| 1570 | using pointer arithmetic. Also, the lack of pointer arithmetic can |
| 1571 | simplify the implementation of the garbage collector. |
| 1572 | </p> |
| 1573 | |
| 1574 | <h3 id="inc_dec"> |
| 1575 | Why are <code>++</code> and <code>--</code> statements and not expressions? And why postfix, not prefix?</h3> |
| 1576 | <p> |
| 1577 | Without pointer arithmetic, the convenience value of pre- and postfix |
| 1578 | increment operators drops. By removing them from the expression |
| 1579 | hierarchy altogether, expression syntax is simplified and the messy |
| 1580 | issues around order of evaluation of <code>++</code> and <code>--</code> |
| 1581 | (consider <code>f(i++)</code> and <code>p[i] = q[++i]</code>) |
| 1582 | are eliminated as well. The simplification is |
| 1583 | significant. As for postfix vs. prefix, either would work fine but |
| 1584 | the postfix version is more traditional; insistence on prefix arose |
| 1585 | with the STL, a library for a language whose name contains, ironically, a |
| 1586 | postfix increment. |
| 1587 | </p> |
| 1588 | |
| 1589 | <h3 id="semicolons"> |
| 1590 | Why are there braces but no semicolons? And why can't I put the opening |
| 1591 | brace on the next line?</h3> |
| 1592 | <p> |
| 1593 | Go uses brace brackets for statement grouping, a syntax familiar to |
| 1594 | programmers who have worked with any language in the C family. |
| 1595 | Semicolons, however, are for parsers, not for people, and we wanted to |
| 1596 | eliminate them as much as possible. To achieve this goal, Go borrows |
| 1597 | a trick from BCPL: the semicolons that separate statements are in the |
| 1598 | formal grammar but are injected automatically, without lookahead, by |
| 1599 | the lexer at the end of any line that could be the end of a statement. |
| 1600 | This works very well in practice but has the effect that it forces a |
| 1601 | brace style. For instance, the opening brace of a function cannot |
| 1602 | appear on a line by itself. |
| 1603 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1604 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1605 | <p> |
| 1606 | Some have argued that the lexer should do lookahead to permit the |
| 1607 | brace to live on the next line. We disagree. Since Go code is meant |
| 1608 | to be formatted automatically by |
Shenghou Ma | 97b13ac | 2012-03-07 08:15:47 +1100 | [diff] [blame] | 1609 | <a href="/cmd/gofmt/"><code>gofmt</code></a>, |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1610 | <i>some</i> style must be chosen. That style may differ from what |
| 1611 | you've used in C or Java, but Go is a new language and |
| 1612 | <code>gofmt</code>'s style is as good as any other. More |
| 1613 | important—much more important—the advantages of a single, |
| 1614 | programmatically mandated format for all Go programs greatly outweigh |
| 1615 | any perceived disadvantages of the particular style. |
| 1616 | Note too that Go's style means that an interactive implementation of |
| 1617 | Go can use the standard syntax one line at a time without special rules. |
| 1618 | </p> |
| 1619 | |
| 1620 | <h3 id="garbage_collection"> |
| 1621 | Why do garbage collection? Won't it be too expensive?</h3> |
| 1622 | <p> |
| 1623 | One of the biggest sources of bookkeeping in systems programs is |
| 1624 | memory management. We feel it's critical to eliminate that |
| 1625 | programmer overhead, and advances in garbage collection |
| 1626 | technology in the last few years give us confidence that we can |
| 1627 | implement it with low enough overhead and no significant |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1628 | latency. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1629 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1630 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1631 | <p> |
| 1632 | Another point is that a large part of the difficulty of concurrent |
| 1633 | and multi-threaded programming is memory management; |
| 1634 | as objects get passed among threads it becomes cumbersome |
| 1635 | to guarantee they become freed safely. |
| 1636 | Automatic garbage collection makes concurrent code far easier to write. |
| 1637 | Of course, implementing garbage collection in a concurrent environment is |
| 1638 | itself a challenge, but meeting it once rather than in every |
| 1639 | program helps everyone. |
| 1640 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1641 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1642 | <p> |
| 1643 | Finally, concurrency aside, garbage collection makes interfaces |
| 1644 | simpler because they don't need to specify how memory is managed across them. |
| 1645 | </p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1646 | |
| 1647 | <p> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1648 | The current implementation is a parallel mark-and-sweep |
| 1649 | collector but a future version might take a different approach. |
| 1650 | </p> |
| 1651 | |
| 1652 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1653 | On the topic of performance, keep in mind that Go gives the programmer |
| 1654 | considerable control over memory layout and allocation, much more than |
| 1655 | is typical in garbage-collected languages. A careful programmer can reduce |
| 1656 | the garbage collection overhead dramatically by using the language well; |
| 1657 | see the article about |
| 1658 | <a href="http://blog.golang.org/2011/06/profiling-go-programs.html">profiling |
| 1659 | Go programs</a> for a worked example, including a demonstration of Go's |
| 1660 | profiling tools. |
| 1661 | </p> |