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