Andrew Gerrand | 7cb21a7 | 2012-01-19 11:24:54 +1100 | [diff] [blame] | 1 | <!--{ |
Andrew Gerrand | 5dd7417 | 2013-09-16 15:47:13 +1000 | [diff] [blame] | 2 | "Title": "Frequently Asked Questions (FAQ)", |
Russ Cox | 10ea651 | 2012-09-24 20:57:01 -0400 | [diff] [blame] | 3 | "Path": "/doc/faq" |
Andrew Gerrand | 7cb21a7 | 2012-01-19 11:24:54 +1100 | [diff] [blame] | 4 | }--> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 5 | |
| 6 | <h2 id="Origins">Origins</h2> |
| 7 | |
| 8 | <h3 id="What_is_the_purpose_of_the_project"> |
| 9 | What is the purpose of the project?</h3> |
| 10 | |
| 11 | <p> |
| 12 | No major systems language has emerged in over a decade, but over that time |
| 13 | the computing landscape has changed tremendously. There are several trends: |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 14 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 15 | |
| 16 | <ul> |
| 17 | <li> |
| 18 | Computers are enormously quicker but software development is not faster. |
| 19 | <li> |
| 20 | Dependency management is a big part of software development today but the |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 21 | “header files” of languages in the C tradition are antithetical to clean |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 22 | dependency analysis—and fast compilation. |
| 23 | <li> |
| 24 | There is a growing rebellion against cumbersome type systems like those of |
| 25 | Java and C++, pushing people towards dynamically typed languages such as |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 26 | Python and JavaScript. |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 27 | <li> |
| 28 | Some fundamental concepts such as garbage collection and parallel computation |
| 29 | are not well supported by popular systems languages. |
| 30 | <li> |
| 31 | The emergence of multicore computers has generated worry and confusion. |
| 32 | </ul> |
| 33 | |
| 34 | <p> |
| 35 | We believe it's worth trying again with a new language, a concurrent, |
| 36 | garbage-collected language with fast compilation. Regarding the points above: |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 37 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 38 | |
| 39 | <ul> |
| 40 | <li> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 41 | 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] | 42 | <li> |
| 43 | Go provides a model for software construction that makes dependency |
| 44 | analysis easy and avoids much of the overhead of C-style include files and |
| 45 | libraries. |
| 46 | <li> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 47 | Go's type system has no hierarchy, so no time is spent defining the |
| 48 | relationships between types. Also, although Go has static types the language |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 49 | attempts to make types feel lighter weight than in typical OO languages. |
| 50 | <li> |
| 51 | Go is fully garbage-collected and provides fundamental support for |
| 52 | concurrent execution and communication. |
| 53 | <li> |
| 54 | By its design, Go proposes an approach for the construction of system |
| 55 | software on multicore machines. |
| 56 | </ul> |
| 57 | |
Rob Pike | 0d5bc0c | 2013-08-20 06:44:41 +1000 | [diff] [blame] | 58 | <p> |
| 59 | A much more expansive answer to this question is available in the article, |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 60 | <a href="//talks.golang.org/2012/splash.article">Go at Google: |
Rob Pike | 0d5bc0c | 2013-08-20 06:44:41 +1000 | [diff] [blame] | 61 | Language Design in the Service of Software Engineering</a>. |
| 62 | |
Rob Pike | 8de5080 | 2012-07-16 13:31:15 -0700 | [diff] [blame] | 63 | <h3 id="What_is_the_status_of_the_project"> |
| 64 | What is the status of the project?</h3> |
| 65 | |
| 66 | <p> |
| 67 | Go became a public open source project on November 10, 2009. |
| 68 | After a couple of years of very active design and development, stability was called for and |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 69 | Go 1 was <a href="//blog.golang.org/2012/03/go-version-1-is-released.html">released</a> |
Rob Pike | 8de5080 | 2012-07-16 13:31:15 -0700 | [diff] [blame] | 70 | on March 28, 2012. |
Andrew Gerrand | 48ba6fe | 2013-10-04 09:45:06 +1000 | [diff] [blame] | 71 | Go 1, which includes a <a href="/ref/spec">language specification</a>, |
Rob Pike | 8de5080 | 2012-07-16 13:31:15 -0700 | [diff] [blame] | 72 | <a href="/pkg/">standard libraries</a>, |
| 73 | and <a href="/cmd/go/">custom tools</a>, |
| 74 | provides a stable foundation for creating reliable products, projects, and publications. |
| 75 | </p> |
| 76 | |
| 77 | <p> |
| 78 | With that stability established, we are using Go to develop programs, products, and tools rather than |
| 79 | actively changing the language and libraries. |
| 80 | In fact, the purpose of Go 1 is to provide <a href="/doc/go1compat.html">long-term stability</a>. |
| 81 | Backwards-incompatible changes will not be made to any Go 1 point release. |
| 82 | We want to use what we have to learn how a future version of Go might look, rather than to play with |
| 83 | the language underfoot. |
| 84 | </p> |
| 85 | |
| 86 | <p> |
| 87 | Of course, development will continue on Go itself, but the focus will be on performance, reliability, |
| 88 | portability and the addition of new functionality such as improved support for internationalization. |
| 89 | </p> |
| 90 | |
| 91 | <p> |
| 92 | 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. |
| 93 | </p> |
| 94 | |
Rob Pike | bdecae9 | 2009-11-23 17:34:23 -0800 | [diff] [blame] | 95 | <h3 id="Whats_the_origin_of_the_mascot"> |
| 96 | What's the origin of the mascot?</h3> |
| 97 | |
| 98 | <p> |
| 99 | The mascot and logo were designed by |
| 100 | <a href="http://reneefrench.blogspot.com">Renée French</a>, who also designed |
Andrew Gerrand | 39304eb | 2016-02-05 09:43:46 +1100 | [diff] [blame^] | 101 | <a href="https://9p.io/plan9/glenda.html">Glenda</a>, |
Rob Pike | bdecae9 | 2009-11-23 17:34:23 -0800 | [diff] [blame] | 102 | the Plan 9 bunny. |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 103 | The <a href="https://blog.golang.org/gopher">gopher</a> |
| 104 | is derived from one she used for an <a href="http://wfmu.org/">WFMU</a> |
Rob Pike | bdecae9 | 2009-11-23 17:34:23 -0800 | [diff] [blame] | 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 |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 150 | computing. Finally, working with Go is intended to be <i>fast</i>: it should take |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 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 | |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 159 | <p> |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 160 | The article <a href="//talks.golang.org/2012/splash.article">Go at Google</a> |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 161 | discusses the background and motivation behind the design of the Go language, |
| 162 | as well as providing more detail about many of the answers presented in this FAQ. |
| 163 | </p> |
| 164 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 165 | <h3 id="ancestors"> |
| 166 | What are Go's ancestors?</h3> |
| 167 | <p> |
| 168 | Go is mostly in the C family (basic syntax), |
| 169 | with significant input from the Pascal/Modula/Oberon |
| 170 | family (declarations, packages), |
| 171 | plus some ideas from languages |
| 172 | inspired by Tony Hoare's CSP, |
| 173 | such as Newsqueak and Limbo (concurrency). |
| 174 | However, it is a new language across the board. |
| 175 | In every respect the language was designed by thinking |
| 176 | about what programmers do and how to make programming, at least the |
| 177 | kind of programming we do, more effective, which means more fun. |
| 178 | </p> |
| 179 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 180 | <h3 id="principles"> |
| 181 | What are the guiding principles in the design?</h3> |
| 182 | <p> |
| 183 | Programming today involves too much bookkeeping, repetition, and |
| 184 | clerical work. As Dick Gabriel says, “Old programs read |
| 185 | like quiet conversations between a well-spoken research worker and a |
| 186 | well-studied mechanical colleague, not as a debate with a compiler. |
| 187 | Who'd have guessed sophistication bought such noise?” |
| 188 | The sophistication is worthwhile—no one wants to go back to |
| 189 | the old languages—but can it be more quietly achieved? |
| 190 | </p> |
| 191 | <p> |
| 192 | Go attempts to reduce the amount of typing in both senses of the word. |
| 193 | Throughout its design, we have tried to reduce clutter and |
| 194 | complexity. There are no forward declarations and no header files; |
| 195 | everything is declared exactly once. Initialization is expressive, |
| 196 | automatic, and easy to use. Syntax is clean and light on keywords. |
| 197 | Stuttering (<code>foo.Foo* myFoo = new(foo.Foo)</code>) is reduced by |
| 198 | simple type derivation using the <code>:=</code> |
| 199 | declare-and-initialize construct. And perhaps most radically, there |
| 200 | is no type hierarchy: types just <i>are</i>, they don't have to |
| 201 | announce their relationships. These simplifications allow Go to be |
| 202 | expressive yet comprehensible without sacrificing, well, sophistication. |
| 203 | </p> |
| 204 | <p> |
| 205 | Another important principle is to keep the concepts orthogonal. |
| 206 | Methods can be implemented for any type; structures represent data while |
| 207 | interfaces represent abstraction; and so on. Orthogonality makes it |
| 208 | easier to understand what happens when things combine. |
| 209 | </p> |
| 210 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 211 | <h2 id="Usage">Usage</h2> |
| 212 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 213 | <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] | 214 | |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 215 | <p> |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 216 | Yes. There are now several Go programs deployed in |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 217 | production inside Google. A public example is the server behind |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 218 | <a href="//golang.org">golang.org</a>. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 219 | It's just the <a href="/cmd/godoc"><code>godoc</code></a> |
| 220 | document server running in a production configuration on |
Dave Cheney | 82cbcb0 | 2012-07-11 09:41:08 -0700 | [diff] [blame] | 221 | <a href="https://developers.google.com/appengine/">Google App Engine</a>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 222 | </p> |
Rob Pike | 7685a67 | 2009-11-09 20:25:45 -0800 | [diff] [blame] | 223 | |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 224 | <p> |
Emil Hessman | f3de217 | 2014-12-30 06:45:24 +0100 | [diff] [blame] | 225 | Other examples include the <a href="//github.com/youtube/vitess/">Vitess</a> |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 226 | system for large-scale SQL installations and Google's download server, <code>dl.google.com</code>, |
| 227 | which delivers Chrome binaries and other large installables such as <code>apt-get</code> |
| 228 | packages. |
| 229 | </p> |
| 230 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 231 | <h3 id="Do_Go_programs_link_with_Cpp_programs"> |
| 232 | Do Go programs link with C/C++ programs?</h3> |
| 233 | |
| 234 | <p> |
Andrew Gerrand | 2a5879d | 2012-03-20 13:50:05 +1100 | [diff] [blame] | 235 | There are two Go compiler implementations, <code>gc</code> |
Aaron Jacobs | 8628688 | 2015-06-24 09:50:12 +1000 | [diff] [blame] | 236 | and <code>gccgo</code>. |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 237 | <code>Gc</code> uses a different calling convention and linker and can |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 238 | therefore only be linked with C programs using the same convention. |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 239 | There is such a C compiler but no C++ compiler. |
| 240 | <code>Gccgo</code> is a GCC front-end that can, with care, be linked with |
| 241 | GCC-compiled C or C++ programs. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 242 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 243 | |
| 244 | <p> |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 245 | The <a href="/cmd/cgo/">cgo</a> program provides the mechanism for a |
| 246 | “foreign function interface” to allow safe calling of |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 247 | C libraries from Go code. SWIG extends this capability to C++ libraries. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 248 | </p> |
| 249 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 250 | |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 251 | <h3 id="Does_Go_support_Google_protocol_buffers"> |
| 252 | Does Go support Google's protocol buffers?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 253 | |
| 254 | <p> |
Rob Pike | 6b3031b | 2010-03-23 17:03:28 -0700 | [diff] [blame] | 255 | A separate open source project provides the necessary compiler plugin and library. |
| 256 | It is available at |
Emil Hessman | f3de217 | 2014-12-30 06:45:24 +0100 | [diff] [blame] | 257 | <a href="//github.com/golang/protobuf">github.com/golang/protobuf/</a> |
Rob Pike | 6b3031b | 2010-03-23 17:03:28 -0700 | [diff] [blame] | 258 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 259 | |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 260 | |
Russ Cox | 6301fb4 | 2009-12-03 17:23:33 -0800 | [diff] [blame] | 261 | <h3 id="Can_I_translate_the_Go_home_page"> |
| 262 | Can I translate the Go home page into another language?</h3> |
| 263 | |
| 264 | <p> |
| 265 | 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] | 266 | However, if you choose to add the Google logo or branding to your site |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 267 | (it does not appear on <a href="//golang.org/">golang.org</a>), |
Russ Cox | 6301fb4 | 2009-12-03 17:23:33 -0800 | [diff] [blame] | 268 | you will need to abide by the guidelines at |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 269 | <a href="//www.google.com/permissions/guidelines.html">www.google.com/permissions/guidelines.html</a> |
Russ Cox | 6301fb4 | 2009-12-03 17:23:33 -0800 | [diff] [blame] | 270 | </p> |
| 271 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 272 | <h2 id="Design">Design</h2> |
| 273 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 274 | <h3 id="unicode_identifiers"> |
| 275 | What's up with Unicode identifiers?</h3> |
| 276 | |
| 277 | <p> |
| 278 | It was important to us to extend the space of identifiers from the |
| 279 | confines of ASCII. Go's rule—identifier characters must be |
| 280 | letters or digits as defined by Unicode—is simple to understand |
| 281 | and to implement but has restrictions. Combining characters are |
| 282 | excluded by design, for instance. |
| 283 | Until there |
| 284 | is an agreed external definition of what an identifier might be, |
| 285 | plus a definition of canonicalization of identifiers that guarantees |
| 286 | no ambiguity, it seemed better to keep combining characters out of |
| 287 | the mix. Thus we have a simple rule that can be expanded later |
| 288 | without breaking programs, one that avoids bugs that would surely arise |
| 289 | from a rule that admits ambiguous identifiers. |
| 290 | </p> |
| 291 | |
| 292 | <p> |
| 293 | On a related note, since an exported identifier must begin with an |
| 294 | upper-case letter, identifiers created from “letters” |
| 295 | in some languages can, by definition, not be exported. For now the |
| 296 | only solution is to use something like <code>X日本語</code>, which |
| 297 | is clearly unsatisfactory; we are considering other options. The |
| 298 | case-for-visibility rule is unlikely to change however; it's one |
| 299 | of our favorite features of Go. |
| 300 | </p> |
| 301 | |
| 302 | <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] | 303 | |
| 304 | <p> |
| 305 | Every language contains novel features and omits someone's favorite |
| 306 | feature. Go was designed with an eye on felicity of programming, speed of |
| 307 | compilation, orthogonality of concepts, and the need to support features |
| 308 | such as concurrency and garbage collection. Your favorite feature may be |
| 309 | missing because it doesn't fit, because it affects compilation speed or |
| 310 | clarity of design, or because it would make the fundamental system model |
| 311 | too difficult. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 312 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 313 | |
| 314 | <p> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 315 | If it bothers you that Go is missing feature <var>X</var>, |
| 316 | 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] | 317 | they compensate in interesting ways for the lack of <var>X</var>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 318 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 319 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 320 | <h3 id="generics"> |
| 321 | Why does Go not have generic types?</h3> |
| 322 | <p> |
| 323 | Generics may well be added at some point. We don't feel an urgency for |
| 324 | them, although we understand some programmers do. |
| 325 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 326 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 327 | <p> |
| 328 | Generics are convenient but they come at a cost in |
| 329 | complexity in the type system and run-time. We haven't yet found a |
| 330 | design that gives value proportionate to the complexity, although we |
| 331 | continue to think about it. Meanwhile, Go's built-in maps and slices, |
| 332 | plus the ability to use the empty interface to construct containers |
| 333 | (with explicit unboxing) mean in many cases it is possible to write |
| 334 | code that does what generics would enable, if less smoothly. |
| 335 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 336 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 337 | <p> |
| 338 | This remains an open issue. |
| 339 | </p> |
| 340 | |
| 341 | <h3 id="exceptions"> |
| 342 | Why does Go not have exceptions?</h3> |
| 343 | <p> |
| 344 | We believe that coupling exceptions to a control |
| 345 | structure, as in the <code>try-catch-finally</code> idiom, results in |
| 346 | convoluted code. It also tends to encourage programmers to label |
| 347 | too many ordinary errors, such as failing to open a file, as |
| 348 | exceptional. |
| 349 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 350 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 351 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 352 | Go takes a different approach. For plain error handling, Go's multi-value |
| 353 | 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] | 354 | <a href="/doc/articles/error_handling.html">A canonical error type, coupled |
| 355 | with Go's other features</a>, makes error handling pleasant but quite different |
| 356 | from that in other languages. |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 357 | </p> |
| 358 | |
| 359 | <p> |
| 360 | Go also has a couple |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 361 | of built-in functions to signal and recover from truly exceptional |
| 362 | conditions. The recovery mechanism is executed only as part of a |
| 363 | function's state being torn down after an error, which is sufficient |
| 364 | to handle catastrophe but requires no extra control structures and, |
| 365 | when used well, can result in clean error-handling code. |
| 366 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 367 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 368 | <p> |
Shenghou Ma | 97b13ac | 2012-03-07 08:15:47 +1100 | [diff] [blame] | 369 | 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] | 370 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 371 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 372 | <h3 id="assertions"> |
| 373 | Why does Go not have assertions?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 374 | |
| 375 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 376 | Go doesn't provide assertions. They are undeniably convenient, but our |
| 377 | experience has been that programmers use them as a crutch to avoid thinking |
| 378 | about proper error handling and reporting. Proper error handling means that |
| 379 | servers continue operation after non-fatal errors instead of crashing. |
| 380 | Proper error reporting means that errors are direct and to the point, |
| 381 | saving the programmer from interpreting a large crash trace. Precise |
| 382 | errors are particularly important when the programmer seeing the errors is |
| 383 | not familiar with the code. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 384 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 385 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 386 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 387 | We understand that this is a point of contention. There are many things in |
| 388 | the Go language and libraries that differ from modern practices, simply |
| 389 | because we feel it's sometimes worth trying a different approach. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 390 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 391 | |
| 392 | <h3 id="csp"> |
| 393 | Why build concurrency on the ideas of CSP?</h3> |
| 394 | <p> |
| 395 | Concurrency and multi-threaded programming have a reputation |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 396 | for difficulty. We believe this is due partly to complex |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 397 | designs such as pthreads and partly to overemphasis on low-level details |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 398 | such as mutexes, condition variables, and memory barriers. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 399 | Higher-level interfaces enable much simpler code, even if there are still |
| 400 | mutexes and such under the covers. |
| 401 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 402 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 403 | <p> |
| 404 | One of the most successful models for providing high-level linguistic support |
| 405 | for concurrency comes from Hoare's Communicating Sequential Processes, or CSP. |
| 406 | Occam and Erlang are two well known languages that stem from CSP. |
| 407 | Go's concurrency primitives derive from a different part of the family tree |
| 408 | whose main contribution is the powerful notion of channels as first class objects. |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 409 | Experience with several earlier languages has shown that the CSP model |
| 410 | fits well into a procedural language framework. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 411 | </p> |
| 412 | |
| 413 | <h3 id="goroutines"> |
| 414 | Why goroutines instead of threads?</h3> |
| 415 | <p> |
| 416 | Goroutines are part of making concurrency easy to use. The idea, which has |
| 417 | been around for a while, is to multiplex independently executing |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 418 | functions—coroutines—onto a set of threads. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 419 | When a coroutine blocks, such as by calling a blocking system call, |
| 420 | the run-time automatically moves other coroutines on the same operating |
| 421 | system thread to a different, runnable thread so they won't be blocked. |
| 422 | The programmer sees none of this, which is the point. |
Rob Pike | 287967f | 2014-03-21 13:59:30 +1100 | [diff] [blame] | 423 | The result, which we call goroutines, can be very cheap: they have little |
| 424 | overhead beyond the memory for the stack, which is just a few kilobytes. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 425 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 426 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 427 | <p> |
Rob Pike | 287967f | 2014-03-21 13:59:30 +1100 | [diff] [blame] | 428 | To make the stacks small, Go's run-time uses resizable, bounded stacks. A newly |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 429 | minted goroutine is given a few kilobytes, which is almost always enough. |
Rob Pike | 287967f | 2014-03-21 13:59:30 +1100 | [diff] [blame] | 430 | When it isn't, the run-time grows (and shrinks) the memory for storing |
| 431 | the stack automatically, allowing many goroutines to live in a modest |
| 432 | amount of memory. |
| 433 | The CPU overhead averages about three cheap instructions per function call. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 434 | It is practical to create hundreds of thousands of goroutines in the same |
Rob Pike | 287967f | 2014-03-21 13:59:30 +1100 | [diff] [blame] | 435 | address space. |
| 436 | If goroutines were just threads, system resources would |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 437 | run out at a much smaller number. |
| 438 | </p> |
| 439 | |
| 440 | <h3 id="atomic_maps"> |
| 441 | Why are map operations not defined to be atomic?</h3> |
| 442 | |
| 443 | <p> |
| 444 | After long discussion it was decided that the typical use of maps did not require |
Ian Lance Taylor | 8189605 | 2013-12-12 18:48:40 -0800 | [diff] [blame] | 445 | safe access from multiple goroutines, and in those cases where it did, the map was |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 446 | probably part of some larger data structure or computation that was already |
| 447 | synchronized. Therefore requiring that all map operations grab a mutex would slow |
| 448 | down most programs and add safety to few. This was not an easy decision, |
| 449 | however, since it means uncontrolled map access can crash the program. |
| 450 | </p> |
| 451 | |
| 452 | <p> |
| 453 | The language does not preclude atomic map updates. When required, such |
| 454 | as when hosting an untrusted program, the implementation could interlock |
| 455 | map access. |
| 456 | </p> |
| 457 | |
Andrew Gerrand | 61dbc34 | 2013-05-06 15:02:56 -0700 | [diff] [blame] | 458 | <h3 id="language_changes"> |
| 459 | Will you accept my language change?</h3> |
| 460 | |
| 461 | <p> |
| 462 | People often suggest improvements to the language—the |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 463 | <a href="//groups.google.com/group/golang-nuts">mailing list</a> |
Andrew Gerrand | 61dbc34 | 2013-05-06 15:02:56 -0700 | [diff] [blame] | 464 | contains a rich history of such discussions—but very few of these changes have |
| 465 | been accepted. |
| 466 | </p> |
| 467 | |
| 468 | <p> |
| 469 | Although Go is an open source project, the language and libraries are protected |
| 470 | by a <a href="/doc/go1compat.html">compatibility promise</a> that prevents |
| 471 | changes that break existing programs. |
| 472 | If your proposal violates the Go 1 specification we cannot even entertain the |
| 473 | idea, regardless of its merit. |
| 474 | A future major release of Go may be incompatible with Go 1, but we're not ready |
| 475 | to start talking about what that might be. |
| 476 | </p> |
| 477 | |
| 478 | <p> |
Rob Pike | 61f3fdc | 2013-08-25 23:50:44 +1000 | [diff] [blame] | 479 | Even if your proposal is compatible with the Go 1 spec, it might |
Andrew Gerrand | 61dbc34 | 2013-05-06 15:02:56 -0700 | [diff] [blame] | 480 | not be in the spirit of Go's design goals. |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 481 | The article <i><a href="//talks.golang.org/2012/splash.article">Go |
Andrew Gerrand | 61dbc34 | 2013-05-06 15:02:56 -0700 | [diff] [blame] | 482 | at Google: Language Design in the Service of Software Engineering</a></i> |
| 483 | explains Go's origins and the motivation behind its design. |
| 484 | </p> |
| 485 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 486 | <h2 id="types">Types</h2> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 487 | |
| 488 | <h3 id="Is_Go_an_object-oriented_language"> |
| 489 | Is Go an object-oriented language?</h3> |
| 490 | |
| 491 | <p> |
| 492 | Yes and no. Although Go has types and methods and allows an |
| 493 | object-oriented style of programming, there is no type hierarchy. |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 494 | The concept of “interface” in Go provides a different approach that |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 495 | we believe is easy to use and in some ways more general. There are |
| 496 | also ways to embed types in other types to provide something |
| 497 | analogous—but not identical—to subclassing. |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 498 | Moreover, methods in Go are more general than in C++ or Java: |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 499 | they can be defined for any sort of data, even built-in types such |
| 500 | as plain, “unboxed” integers. |
| 501 | They are not restricted to structs (classes). |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 502 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 503 | |
| 504 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 505 | Also, the lack of a type hierarchy makes “objects” in Go feel much more |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 506 | lightweight than in languages such as C++ or Java. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 507 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 508 | |
| 509 | <h3 id="How_do_I_get_dynamic_dispatch_of_methods"> |
| 510 | How do I get dynamic dispatch of methods?</h3> |
| 511 | |
| 512 | <p> |
| 513 | The only way to have dynamically dispatched methods is through an |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 514 | 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] | 515 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 516 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 517 | <h3 id="inheritance"> |
| 518 | Why is there no type inheritance?</h3> |
| 519 | <p> |
| 520 | Object-oriented programming, at least in the best-known languages, |
| 521 | involves too much discussion of the relationships between types, |
| 522 | relationships that often could be derived automatically. Go takes a |
| 523 | different approach. |
| 524 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 525 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 526 | <p> |
| 527 | Rather than requiring the programmer to declare ahead of time that two |
| 528 | types are related, in Go a type automatically satisfies any interface |
| 529 | that specifies a subset of its methods. Besides reducing the |
| 530 | bookkeeping, this approach has real advantages. Types can satisfy |
| 531 | many interfaces at once, without the complexities of traditional |
| 532 | multiple inheritance. |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 533 | Interfaces can be very lightweight—an interface with |
| 534 | one or even zero methods can express a useful concept. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 535 | Interfaces can be added after the fact if a new idea comes along |
| 536 | or for testing—without annotating the original types. |
| 537 | Because there are no explicit relationships between types |
| 538 | and interfaces, there is no type hierarchy to manage or discuss. |
| 539 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 540 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 541 | <p> |
| 542 | It's possible to use these ideas to construct something analogous to |
| 543 | type-safe Unix pipes. For instance, see how <code>fmt.Fprintf</code> |
| 544 | enables formatted printing to any output, not just a file, or how the |
| 545 | <code>bufio</code> package can be completely separate from file I/O, |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 546 | or how the <code>image</code> packages generate compressed |
| 547 | image files. All these ideas stem from a single interface |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 548 | (<code>io.Writer</code>) representing a single method |
| 549 | (<code>Write</code>). And that's only scratching the surface. |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 550 | Go's interfaces have a profound influence on how programs are structured. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 551 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 552 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 553 | <p> |
| 554 | It takes some getting used to but this implicit style of type |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 555 | dependency is one of the most productive things about Go. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 556 | </p> |
| 557 | |
| 558 | <h3 id="methods_on_basics"> |
| 559 | Why is <code>len</code> a function and not a method?</h3> |
| 560 | <p> |
| 561 | We debated this issue but decided |
| 562 | implementing <code>len</code> and friends as functions was fine in practice and |
| 563 | didn't complicate questions about the interface (in the Go type sense) |
| 564 | of basic types. |
| 565 | </p> |
| 566 | |
| 567 | <h3 id="overloading"> |
| 568 | Why does Go not support overloading of methods and operators?</h3> |
| 569 | <p> |
| 570 | Method dispatch is simplified if it doesn't need to do type matching as well. |
| 571 | Experience with other languages told us that having a variety of |
| 572 | methods with the same name but different signatures was occasionally useful |
| 573 | but that it could also be confusing and fragile in practice. Matching only by name |
| 574 | and requiring consistency in the types was a major simplifying decision |
| 575 | in Go's type system. |
| 576 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 577 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 578 | <p> |
| 579 | Regarding operator overloading, it seems more a convenience than an absolute |
| 580 | requirement. Again, things are simpler without it. |
| 581 | </p> |
| 582 | |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 583 | <h3 id="implements_interface"> |
| 584 | Why doesn't Go have "implements" declarations?</h3> |
| 585 | |
| 586 | <p> |
| 587 | A Go type satisfies an interface by implementing the methods of that interface, |
| 588 | nothing more. This property allows interfaces to be defined and used without |
Rob Pike | 4be9b83 | 2012-09-07 14:01:02 -0700 | [diff] [blame] | 589 | having to modify existing code. It enables a kind of structural typing that |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 590 | promotes separation of concerns and improves code re-use, and makes it easier |
| 591 | to build on patterns that emerge as the code develops. |
| 592 | The semantics of interfaces is one of the main reasons for Go's nimble, |
| 593 | lightweight feel. |
| 594 | </p> |
| 595 | |
| 596 | <p> |
| 597 | See the <a href="#inheritance">question on type inheritance</a> for more detail. |
| 598 | </p> |
| 599 | |
| 600 | <h3 id="guarantee_satisfies_interface"> |
| 601 | How can I guarantee my type satisfies an interface?</h3> |
| 602 | |
| 603 | <p> |
| 604 | You can ask the compiler to check that the type <code>T</code> implements the |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 605 | interface <code>I</code> by attempting an assignment using the zero value for |
| 606 | <code>T</code> or pointer to <code>T</code>, as appropriate: |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 607 | </p> |
| 608 | |
| 609 | <pre> |
| 610 | type T struct{} |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 611 | var _ I = T{} // Verify that T implements I. |
| 612 | var _ I = (*T)(nil) // Verify that *T implements I. |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 613 | </pre> |
| 614 | |
| 615 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 616 | If <code>T</code> (or <code>*T</code>, accordingly) doesn't implement |
| 617 | <code>I</code>, the mistake will be caught at compile time. |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 618 | </p> |
| 619 | |
| 620 | <p> |
| 621 | If you wish the users of an interface to explicitly declare that they implement |
| 622 | it, you can add a method with a descriptive name to the interface's method set. |
| 623 | For example: |
| 624 | </p> |
| 625 | |
| 626 | <pre> |
| 627 | type Fooer interface { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 628 | Foo() |
| 629 | ImplementsFooer() |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 630 | } |
| 631 | </pre> |
| 632 | |
| 633 | <p> |
| 634 | 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] | 635 | <code>Fooer</code>, clearly documenting the fact and announcing it in |
| 636 | <a href="/cmd/godoc/">godoc</a>'s output. |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 637 | </p> |
| 638 | |
| 639 | <pre> |
| 640 | type Bar struct{} |
| 641 | func (b Bar) ImplementsFooer() {} |
| 642 | func (b Bar) Foo() {} |
| 643 | </pre> |
| 644 | |
| 645 | <p> |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 646 | Most code doesn't make use of such constraints, since they limit the utility of |
Andrew Gerrand | aef4e1c | 2011-03-04 13:11:07 +1100 | [diff] [blame] | 647 | the interface idea. Sometimes, though, they're necessary to resolve ambiguities |
| 648 | among similar interfaces. |
| 649 | </p> |
| 650 | |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 651 | <h3 id="t_and_equal_interface"> |
| 652 | Why doesn't type T satisfy the Equal interface?</h3> |
| 653 | |
| 654 | <p> |
| 655 | Consider this simple interface to represent an object that can compare |
| 656 | itself with another value: |
| 657 | </p> |
| 658 | |
| 659 | <pre> |
| 660 | type Equaler interface { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 661 | Equal(Equaler) bool |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 662 | } |
| 663 | </pre> |
| 664 | |
| 665 | <p> |
| 666 | and this type, <code>T</code>: |
| 667 | </p> |
| 668 | |
| 669 | <pre> |
| 670 | type T int |
| 671 | func (t T) Equal(u T) bool { return t == u } // does not satisfy Equaler |
| 672 | </pre> |
| 673 | |
| 674 | <p> |
| 675 | Unlike the analogous situation in some polymorphic type systems, |
| 676 | <code>T</code> does not implement <code>Equaler</code>. |
| 677 | The argument type of <code>T.Equal</code> is <code>T</code>, |
| 678 | not literally the required type <code>Equaler</code>. |
| 679 | </p> |
| 680 | |
| 681 | <p> |
| 682 | In Go, the type system does not promote the argument of |
| 683 | <code>Equal</code>; that is the programmer's responsibility, as |
| 684 | illustrated by the type <code>T2</code>, which does implement |
| 685 | <code>Equaler</code>: |
| 686 | </p> |
| 687 | |
| 688 | <pre> |
| 689 | type T2 int |
| 690 | func (t T2) Equal(u Equaler) bool { return t == u.(T2) } // satisfies Equaler |
| 691 | </pre> |
| 692 | |
| 693 | <p> |
| 694 | Even this isn't like other type systems, though, because in Go <em>any</em> |
| 695 | type that satisfies <code>Equaler</code> could be passed as the |
| 696 | argument to <code>T2.Equal</code>, and at run time we must |
| 697 | check that the argument is of type <code>T2</code>. |
| 698 | Some languages arrange to make that guarantee at compile time. |
| 699 | </p> |
| 700 | |
| 701 | <p> |
| 702 | A related example goes the other way: |
| 703 | </p> |
| 704 | |
| 705 | <pre> |
| 706 | type Opener interface { |
David Symonds | c912150 | 2013-05-22 12:28:58 +1000 | [diff] [blame] | 707 | Open() Reader |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | func (t T3) Open() *os.File |
| 711 | </pre> |
| 712 | |
| 713 | <p> |
| 714 | In Go, <code>T3</code> does not satisfy <code>Opener</code>, |
| 715 | although it might in another language. |
| 716 | </p> |
| 717 | |
| 718 | <p> |
| 719 | While it is true that Go's type system does less for the programmer |
| 720 | in such cases, the lack of subtyping makes the rules about |
| 721 | interface satisfaction very easy to state: are the function's names |
| 722 | and signatures exactly those of the interface? |
| 723 | Go's rule is also easy to implement efficiently. |
| 724 | We feel these benefits offset the lack of |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 725 | automatic type promotion. Should Go one day adopt some form of polymorphic |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 726 | typing, we expect there would be a way to express the idea of these |
| 727 | examples and also have them be statically checked. |
| 728 | </p> |
| 729 | |
Andrew Gerrand | 17805dd | 2011-06-18 20:31:38 +1000 | [diff] [blame] | 730 | <h3 id="convert_slice_of_interface"> |
| 731 | Can I convert a []T to an []interface{}?</h3> |
| 732 | |
| 733 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 734 | Not directly, because they do not have the same representation in memory. |
Andrew Gerrand | 17805dd | 2011-06-18 20:31:38 +1000 | [diff] [blame] | 735 | It is necessary to copy the elements individually to the destination |
| 736 | slice. This example converts a slice of <code>int</code> to a slice of |
| 737 | <code>interface{}</code>: |
| 738 | </p> |
| 739 | |
| 740 | <pre> |
| 741 | t := []int{1, 2, 3, 4} |
| 742 | s := make([]interface{}, len(t)) |
| 743 | for i, v := range t { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 744 | s[i] = v |
Andrew Gerrand | 17805dd | 2011-06-18 20:31:38 +1000 | [diff] [blame] | 745 | } |
| 746 | </pre> |
| 747 | |
Rob Pike | 1e0f97a | 2012-02-17 16:27:17 +1100 | [diff] [blame] | 748 | <h3 id="nil_error"> |
| 749 | Why is my nil error value not equal to nil? |
| 750 | </h3> |
| 751 | |
| 752 | <p> |
| 753 | Under the covers, interfaces are implemented as two elements, a type and a value. |
| 754 | The value, called the interface's dynamic value, |
| 755 | is an arbitrary concrete value and the type is that of the value. |
| 756 | For the <code>int</code> value 3, an interface value contains, |
| 757 | schematically, (<code>int</code>, <code>3</code>). |
| 758 | </p> |
| 759 | |
| 760 | <p> |
| 761 | An interface value is <code>nil</code> only if the inner value and type are both unset, |
| 762 | (<code>nil</code>, <code>nil</code>). |
| 763 | In particular, a <code>nil</code> interface will always hold a <code>nil</code> type. |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 764 | If we store a <code>nil</code> pointer of type <code>*int</code> inside |
Rob Pike | 1e0f97a | 2012-02-17 16:27:17 +1100 | [diff] [blame] | 765 | an interface value, the inner type will be <code>*int</code> regardless of the value of the pointer: |
| 766 | (<code>*int</code>, <code>nil</code>). |
| 767 | Such an interface value will therefore be non-<code>nil</code> |
| 768 | <em>even when the pointer inside is</em> <code>nil</code>. |
| 769 | </p> |
| 770 | |
| 771 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 772 | This situation can be confusing, and arises when a <code>nil</code> value is |
Rob Pike | 1e0f97a | 2012-02-17 16:27:17 +1100 | [diff] [blame] | 773 | stored inside an interface value such as an <code>error</code> return: |
| 774 | </p> |
| 775 | |
| 776 | <pre> |
| 777 | func returnsError() error { |
| 778 | var p *MyError = nil |
| 779 | if bad() { |
| 780 | p = ErrBad |
| 781 | } |
| 782 | return p // Will always return a non-nil error. |
| 783 | } |
| 784 | </pre> |
| 785 | |
| 786 | <p> |
| 787 | If all goes well, the function returns a <code>nil</code> <code>p</code>, |
| 788 | so the return value is an <code>error</code> interface |
| 789 | value holding (<code>*MyError</code>, <code>nil</code>). |
| 790 | This means that if the caller compares the returned error to <code>nil</code>, |
| 791 | it will always look as if there was an error even if nothing bad happened. |
| 792 | To return a proper <code>nil</code> <code>error</code> to the caller, |
| 793 | the function must return an explicit <code>nil</code>: |
| 794 | </p> |
| 795 | |
| 796 | |
| 797 | <pre> |
| 798 | func returnsError() error { |
| 799 | if bad() { |
| 800 | return ErrBad |
| 801 | } |
| 802 | return nil |
| 803 | } |
| 804 | </pre> |
| 805 | |
| 806 | <p> |
| 807 | It's a good idea for functions |
| 808 | that return errors always to use the <code>error</code> type in |
| 809 | their signature (as we did above) rather than a concrete type such |
| 810 | as <code>*MyError</code>, to help guarantee the error is |
| 811 | created correctly. As an example, |
| 812 | <a href="/pkg/os/#Open"><code>os.Open</code></a> |
| 813 | returns an <code>error</code> even though, if not <code>nil</code>, |
| 814 | it's always of concrete type |
| 815 | <a href="/pkg/os/#PathError"><code>*os.PathError</code></a>. |
| 816 | </p> |
| 817 | |
| 818 | <p> |
| 819 | Similar situations to those described here can arise whenever interfaces are used. |
| 820 | Just keep in mind that if any concrete value |
| 821 | has been stored in the interface, the interface will not be <code>nil</code>. |
| 822 | For more information, see |
Shenghou Ma | cb6c09a | 2012-03-01 14:54:35 +0800 | [diff] [blame] | 823 | <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] | 824 | </p> |
| 825 | |
| 826 | |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 827 | <h3 id="unions"> |
| 828 | Why are there no untagged unions, as in C?</h3> |
| 829 | |
| 830 | <p> |
| 831 | Untagged unions would violate Go's memory safety |
| 832 | guarantees. |
| 833 | </p> |
| 834 | |
| 835 | <h3 id="variant_types"> |
| 836 | Why does Go not have variant types?</h3> |
| 837 | |
| 838 | <p> |
| 839 | Variant types, also known as algebraic types, provide a way to specify |
| 840 | that a value might take one of a set of other types, but only those |
| 841 | types. A common example in systems programming would specify that an |
| 842 | error is, say, a network error, a security error or an application |
| 843 | error and allow the caller to discriminate the source of the problem |
| 844 | by examining the type of the error. Another example is a syntax tree |
| 845 | in which each node can be a different type: declaration, statement, |
| 846 | assignment and so on. |
| 847 | </p> |
| 848 | |
| 849 | <p> |
| 850 | We considered adding variant types to Go, but after discussion |
| 851 | decided to leave them out because they overlap in confusing ways |
| 852 | with interfaces. What would happen if the elements of a variant type |
| 853 | were themselves interfaces? |
| 854 | </p> |
| 855 | |
| 856 | <p> |
| 857 | Also, some of what variant types address is already covered by the |
| 858 | language. The error example is easy to express using an interface |
| 859 | value to hold the error and a type switch to discriminate cases. The |
| 860 | syntax tree example is also doable, although not as elegantly. |
| 861 | </p> |
| 862 | |
Ian Lance Taylor | 85dcc34 | 2015-11-20 07:00:09 -0800 | [diff] [blame] | 863 | <h3 id="covariant_types"> |
| 864 | Why does Go not have covariant result types?</h3> |
| 865 | |
| 866 | <p> |
| 867 | Covariant result types would mean that an interface like |
| 868 | |
| 869 | <pre> |
| 870 | type Copyable interface { |
| 871 | Copy() interface{} |
| 872 | } |
| 873 | </pre> |
| 874 | |
| 875 | would be satisfied by the method |
| 876 | |
| 877 | <pre> |
| 878 | func (v Value) Copy() Value |
| 879 | </pre> |
| 880 | |
| 881 | because <code>Value</code> implements the empty interface. |
| 882 | In Go method types must match exactly, so <code>Value</code> does not |
| 883 | implement <code>Copyable</code>. |
| 884 | Go separates the notion of what a |
| 885 | type does—its methods—from the type's implementation. |
| 886 | If two methods return different types, they are not doing the same thing. |
| 887 | Programmers who want covariant result types are often trying to |
Russ Cox | cf49b35 | 2015-12-11 11:35:36 -0500 | [diff] [blame] | 888 | express a type hierarchy through interfaces. |
Ian Lance Taylor | 85dcc34 | 2015-11-20 07:00:09 -0800 | [diff] [blame] | 889 | In Go it's more natural to have a clean separation between interface |
| 890 | and implementation. |
| 891 | </p> |
| 892 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 893 | <h2 id="values">Values</h2> |
| 894 | |
| 895 | <h3 id="conversions"> |
| 896 | Why does Go not provide implicit numeric conversions?</h3> |
| 897 | <p> |
| 898 | The convenience of automatic conversion between numeric types in C is |
| 899 | outweighed by the confusion it causes. When is an expression unsigned? |
| 900 | How big is the value? Does it overflow? Is the result portable, independent |
| 901 | of the machine on which it executes? |
| 902 | It also complicates the compiler; “the usual arithmetic conversions” |
| 903 | are not easy to implement and inconsistent across architectures. |
| 904 | For reasons of portability, we decided to make things clear and straightforward |
| 905 | at the cost of some explicit conversions in the code. |
| 906 | The definition of constants in Go—arbitrary precision values free |
| 907 | of signedness and size annotations—ameliorates matters considerably, |
| 908 | though. |
| 909 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 910 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 911 | <p> |
| 912 | A related detail is that, unlike in C, <code>int</code> and <code>int64</code> |
| 913 | are distinct types even if <code>int</code> is a 64-bit type. The <code>int</code> |
| 914 | type is generic; if you care about how many bits an integer holds, Go |
| 915 | encourages you to be explicit. |
| 916 | </p> |
| 917 | |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 918 | <p> |
Brad Fitzpatrick | 783297a | 2015-07-11 08:51:20 -0600 | [diff] [blame] | 919 | A blog post titled <a href="https://blog.golang.org/constants">Constants</a> |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 920 | explores this topic in more detail. |
| 921 | </p> |
| 922 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 923 | <h3 id="builtin_maps"> |
| 924 | Why are maps built in?</h3> |
| 925 | <p> |
| 926 | The same reason strings are: they are such a powerful and important data |
| 927 | structure that providing one excellent implementation with syntactic support |
| 928 | makes programming more pleasant. We believe that Go's implementation of maps |
| 929 | is strong enough that it will serve for the vast majority of uses. |
| 930 | If a specific application can benefit from a custom implementation, it's possible |
| 931 | to write one but it will not be as convenient syntactically; this seems a reasonable tradeoff. |
| 932 | </p> |
| 933 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 934 | <h3 id="map_keys"> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 935 | Why don't maps allow slices as keys?</h3> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 936 | <p> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 937 | Map lookup requires an equality operator, which slices do not implement. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 938 | They don't implement equality because equality is not well defined on such types; |
| 939 | there are multiple considerations involving shallow vs. deep comparison, pointer vs. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 940 | value comparison, how to deal with recursive types, and so on. |
| 941 | We may revisit this issue—and implementing equality for slices |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 942 | 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] | 943 | 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] | 944 | </p> |
| 945 | |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 946 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 947 | In Go 1, unlike prior releases, equality is defined for structs and arrays, so such |
| 948 | 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] | 949 | </p> |
| 950 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 951 | <h3 id="references"> |
| 952 | Why are maps, slices, and channels references while arrays are values?</h3> |
| 953 | <p> |
| 954 | There's a lot of history on that topic. Early on, maps and channels |
| 955 | were syntactically pointers and it was impossible to declare or use a |
| 956 | non-pointer instance. Also, we struggled with how arrays should work. |
| 957 | Eventually we decided that the strict separation of pointers and |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 958 | values made the language harder to use. Changing these |
| 959 | types to act as references to the associated, shared data structures resolved |
| 960 | these issues. This change added some regrettable complexity to the |
| 961 | language but had a large effect on usability: Go became a more |
| 962 | productive, comfortable language when it was introduced. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 963 | </p> |
| 964 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 965 | <h2 id="Writing_Code">Writing Code</h2> |
| 966 | |
| 967 | <h3 id="How_are_libraries_documented"> |
| 968 | How are libraries documented?</h3> |
| 969 | |
| 970 | <p> |
| 971 | There is a program, <code>godoc</code>, written in Go, that extracts |
| 972 | package documentation from the source code. It can be used on the |
| 973 | command line or on the web. An instance is running at |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 974 | <a href="/pkg/">golang.org/pkg/</a>. |
Russ Cox | e434f1a | 2009-11-07 17:31:22 -0800 | [diff] [blame] | 975 | In fact, <code>godoc</code> implements the full site at |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 976 | <a href="/">golang.org/</a>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 977 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 978 | |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 979 | <p> |
| 980 | A <code>godoc</code> instance may be configured to provide rich, |
| 981 | interactive static analyses of symbols in the programs it displays; details are |
| 982 | listed <a href="https://golang.org/lib/godoc/analysis/help.html">here</a>. |
| 983 | </p> |
| 984 | |
| 985 | <p> |
| 986 | For access to documentation from the command line, the |
| 987 | <a href="https://golang.org/pkg/cmd/go/">go</a> tool has a |
| 988 | <a href="https://golang.org/pkg/cmd/go/#hdr-Show_documentation_for_package_or_symbol">doc</a> |
| 989 | subcommand that provides a textual interface to the same information. |
| 990 | </p> |
| 991 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 992 | <h3 id="Is_there_a_Go_programming_style_guide"> |
| 993 | Is there a Go programming style guide?</h3> |
| 994 | |
| 995 | <p> |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 996 | Eventually, there may be a small number of rules to guide things |
| 997 | like naming, layout, and file organization. |
| 998 | The document <a href="effective_go.html">Effective Go</a> |
| 999 | contains some style advice. |
| 1000 | More directly, the program <code>gofmt</code> is a pretty-printer |
| 1001 | whose purpose is to enforce layout rules; it replaces the usual |
| 1002 | compendium of do's and don'ts that allows interpretation. |
| 1003 | 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] | 1004 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1005 | |
Rob Pike | 3a7fe36 | 2014-03-06 13:15:09 +1100 | [diff] [blame] | 1006 | <p> |
| 1007 | The document titled |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 1008 | <a href="//golang.org/s/comments">Go Code Review Comments</a> |
Rob Pike | 3a7fe36 | 2014-03-06 13:15:09 +1100 | [diff] [blame] | 1009 | is a collection of very short essays about details of Go idiom that are often |
| 1010 | missed by programmers. |
| 1011 | It is a handy reference for people doing code reviews for Go projects. |
| 1012 | </p> |
| 1013 | |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1014 | <h3 id="How_do_I_submit_patches_to_the_Go_libraries"> |
| 1015 | How do I submit patches to the Go libraries?</h3> |
| 1016 | |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 1017 | <p> |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1018 | The library sources are in the <code>src</code> directory of the repository. |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 1019 | 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] | 1020 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1021 | |
Rob Pike | 0c2a479 | 2009-11-01 20:50:42 -0800 | [diff] [blame] | 1022 | <p> |
| 1023 | See the document |
| 1024 | <a href="contribute.html">Contributing to the Go project</a> |
| 1025 | for more information about how to proceed. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1026 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1027 | |
Herbert Georg Fischer | 99021b7 | 2013-03-15 13:43:10 -0700 | [diff] [blame] | 1028 | <h3 id="git_https"> |
| 1029 | Why does "go get" use HTTPS when cloning a repository?</h3> |
| 1030 | |
| 1031 | <p> |
| 1032 | Companies often permit outgoing traffic only on the standard TCP ports 80 (HTTP) |
| 1033 | and 443 (HTTPS), blocking outgoing traffic on other ports, including TCP port 9418 |
| 1034 | (git) and TCP port 22 (SSH). |
| 1035 | When using HTTPS instead of HTTP, <code>git</code> enforces certificate validation by |
| 1036 | default, providing protection against man-in-the-middle, eavesdropping and tampering attacks. |
| 1037 | The <code>go get</code> command therefore uses HTTPS for safety. |
| 1038 | </p> |
| 1039 | |
| 1040 | <p> |
| 1041 | If you use <code>git</code> and prefer to push changes through SSH using your existing key |
| 1042 | it's easy to work around this. For GitHub, try one of these solutions: |
| 1043 | </p> |
| 1044 | <ul> |
| 1045 | <li>Manually clone the repository in the expected package directory: |
| 1046 | <pre> |
| 1047 | $ cd $GOPATH/src/github.com/username |
| 1048 | $ git clone git@github.com:username/package.git |
| 1049 | </pre> |
| 1050 | </li> |
| 1051 | <li>Force <code>git push</code> to use the <code>SSH</code> protocol by appending |
| 1052 | these two lines to <code>~/.gitconfig</code>: |
| 1053 | <pre> |
| 1054 | [url "git@github.com:"] |
| 1055 | pushInsteadOf = https://github.com/ |
| 1056 | </pre> |
| 1057 | </li> |
| 1058 | </ul> |
| 1059 | |
Russ Cox | dc8d903 | 2013-10-03 09:18:47 -0400 | [diff] [blame] | 1060 | <h3 id="get_version"> |
| 1061 | How should I manage package versions using "go get"?</h3> |
| 1062 | |
| 1063 | <p> |
| 1064 | "Go get" does not have any explicit concept of package versions. |
| 1065 | Versioning is a source of significant complexity, especially in large code bases, |
| 1066 | and we are unaware of any approach that works well at scale in a large enough |
| 1067 | variety of situations to be appropriate to force on all Go users. |
| 1068 | What "go get" and the larger Go toolchain do provide is isolation of |
| 1069 | packages with different import paths. |
| 1070 | For example, the standard library's <code>html/template</code> and <code>text/template</code> |
| 1071 | coexist even though both are "package template". |
| 1072 | This observation leads to some advice for package authors and package users. |
| 1073 | </p> |
| 1074 | |
| 1075 | <p> |
| 1076 | Packages intended for public use should try to maintain backwards compatibility as they evolve. |
| 1077 | The <a href="/doc/go1compat.html">Go 1 compatibility guidelines</a> are a good reference here: |
| 1078 | don't remove exported names, encourage tagged composite literals, and so on. |
| 1079 | If different functionality is required, add a new name instead of changing an old one. |
| 1080 | If a complete break is required, create a new package with a new import path.</p> |
| 1081 | |
| 1082 | <p> |
| 1083 | If you're using an externally supplied package and worry that it might change in |
| 1084 | unexpected ways, the simplest solution is to copy it to your local repository. |
| 1085 | (This is the approach Google takes internally.) |
| 1086 | Store the copy under a new import path that identifies it as a local copy. |
| 1087 | For example, you might copy "original.com/pkg" to "you.com/external/original.com/pkg". |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1088 | The <a href="https://godoc.org/golang.org/x/tools/cmd/gomvpkg">gomvpkg</a> |
| 1089 | program is one tool to help automate this process. |
| 1090 | </p> |
| 1091 | |
| 1092 | <p> |
| 1093 | The Go 1.5 release includes an experimental facility to the |
| 1094 | <a href="https://golang.org/cmd/go">go</a> command |
| 1095 | that makes it easier to manage external dependencies by "vendoring" |
| 1096 | them into a special directory near the package that depends upon them. |
| 1097 | See the <a href="https://golang.org/s/go15vendor">design |
| 1098 | document</a> for details. |
Russ Cox | dc8d903 | 2013-10-03 09:18:47 -0400 | [diff] [blame] | 1099 | </p> |
| 1100 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1101 | <h2 id="Pointers">Pointers and Allocation</h2> |
| 1102 | |
| 1103 | <h3 id="pass_by_value"> |
| 1104 | When are function parameters passed by value?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1105 | |
| 1106 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1107 | As in all languages in the C family, everything in Go is passed by value. |
| 1108 | That is, a function always gets a copy of the |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1109 | thing being passed, as if there were an assignment statement assigning the |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1110 | value to the parameter. For instance, passing an <code>int</code> value |
| 1111 | to a function makes a copy of the <code>int</code>, and passing a pointer |
| 1112 | value makes a copy of the pointer, but not the data it points to. |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1113 | (See a <a href="/doc/faq#methods_on_values_or_pointers">later |
| 1114 | section</a> for a discussion of how this affects method receivers.) |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1115 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1116 | |
| 1117 | <p> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1118 | Map and slice values behave like pointers: they are descriptors that |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1119 | contain pointers to the underlying map or slice data. Copying a map or |
| 1120 | slice value doesn't copy the data it points to. Copying an interface value |
| 1121 | makes a copy of the thing stored in the interface value. If the interface |
| 1122 | value holds a struct, copying the interface value makes a copy of the |
| 1123 | struct. If the interface value holds a pointer, copying the interface value |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1124 | makes a copy of the pointer, but again not the data it points to. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1125 | </p> |
| 1126 | |
Rob Pike | 09cd13c | 2013-03-15 11:38:50 -0700 | [diff] [blame] | 1127 | <h3 id="pointer_to_interface"> |
| 1128 | When should I use a pointer to an interface?</h3> |
| 1129 | |
| 1130 | <p> |
| 1131 | Almost never. Pointers to interface values arise only in rare, tricky situations involving |
| 1132 | disguising an interface value's type for delayed evaluation. |
| 1133 | </p> |
| 1134 | |
| 1135 | <p> |
| 1136 | It is however a common mistake to pass a pointer to an interface value |
| 1137 | to a function expecting an interface. The compiler will complain about this |
| 1138 | error but the situation can still be confusing, because sometimes a |
| 1139 | <a href="#different_method_sets">pointer |
| 1140 | is necessary to satisfy an interface</a>. |
| 1141 | The insight is that although a pointer to a concrete type can satisfy |
Rob Pike | a942265 | 2014-10-26 11:27:55 -0700 | [diff] [blame] | 1142 | an interface, with one exception <em>a pointer to an interface can never satisfy an interface</em>. |
Rob Pike | 09cd13c | 2013-03-15 11:38:50 -0700 | [diff] [blame] | 1143 | </p> |
| 1144 | |
| 1145 | <p> |
| 1146 | Consider the variable declaration, |
| 1147 | </p> |
| 1148 | |
| 1149 | <pre> |
| 1150 | var w io.Writer |
| 1151 | </pre> |
| 1152 | |
| 1153 | <p> |
| 1154 | The printing function <code>fmt.Fprintf</code> takes as its first argument |
| 1155 | a value that satisfies <code>io.Writer</code>—something that implements |
| 1156 | the canonical <code>Write</code> method. Thus we can write |
| 1157 | </p> |
| 1158 | |
| 1159 | <pre> |
| 1160 | fmt.Fprintf(w, "hello, world\n") |
| 1161 | </pre> |
| 1162 | |
| 1163 | <p> |
| 1164 | If however we pass the address of <code>w</code>, the program will not compile. |
| 1165 | </p> |
| 1166 | |
| 1167 | <pre> |
| 1168 | fmt.Fprintf(&w, "hello, world\n") // Compile-time error. |
| 1169 | </pre> |
| 1170 | |
| 1171 | <p> |
| 1172 | The one exception is that any value, even a pointer to an interface, can be assigned to |
| 1173 | a variable of empty interface type (<code>interface{}</code>). |
| 1174 | Even so, it's almost certainly a mistake if the value is a pointer to an interface; |
| 1175 | the result can be confusing. |
| 1176 | </p> |
| 1177 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1178 | <h3 id="methods_on_values_or_pointers"> |
| 1179 | Should I define methods on values or pointers?</h3> |
| 1180 | |
| 1181 | <pre> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1182 | func (s *MyStruct) pointerMethod() { } // method on pointer |
| 1183 | func (s MyStruct) valueMethod() { } // method on value |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1184 | </pre> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1185 | |
| 1186 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1187 | For programmers unaccustomed to pointers, the distinction between these |
| 1188 | two examples can be confusing, but the situation is actually very simple. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1189 | 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] | 1190 | examples) behaves exactly as if it were an argument to the method. |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1191 | Whether to define the receiver as a value or as a pointer is the same |
| 1192 | question, then, as whether a function argument should be a value or |
| 1193 | a pointer. |
| 1194 | There are several considerations. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1195 | </p> |
| 1196 | |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1197 | <p> |
| 1198 | First, and most important, does the method need to modify the |
| 1199 | receiver? |
| 1200 | If it does, the receiver <em>must</em> be a pointer. |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 1201 | (Slices and maps act as references, so their story is a little |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1202 | more subtle, but for instance to change the length of a slice |
| 1203 | in a method the receiver must still be a pointer.) |
| 1204 | In the examples above, if <code>pointerMethod</code> modifies |
| 1205 | the fields of <code>s</code>, |
| 1206 | the caller will see those changes, but <code>valueMethod</code> |
| 1207 | is called with a copy of the caller's argument (that's the definition |
| 1208 | of passing a value), so changes it makes will be invisible to the caller. |
| 1209 | </p> |
| 1210 | |
| 1211 | <p> |
| 1212 | By the way, pointer receivers are identical to the situation in Java, |
| 1213 | although in Java the pointers are hidden under the covers; it's Go's |
| 1214 | value receivers that are unusual. |
| 1215 | </p> |
| 1216 | |
| 1217 | <p> |
| 1218 | Second is the consideration of efficiency. If the receiver is large, |
| 1219 | a big <code>struct</code> for instance, it will be much cheaper to |
| 1220 | use a pointer receiver. |
| 1221 | </p> |
| 1222 | |
| 1223 | <p> |
| 1224 | Next is consistency. If some of the methods of the type must have |
| 1225 | pointer receivers, the rest should too, so the method set is |
| 1226 | consistent regardless of how the type is used. |
| 1227 | See the section on <a href="#different_method_sets">method sets</a> |
| 1228 | for details. |
| 1229 | </p> |
| 1230 | |
| 1231 | <p> |
| 1232 | For types such as basic types, slices, and small <code>structs</code>, |
| 1233 | a value receiver is very cheap so unless the semantics of the method |
| 1234 | requires a pointer, a value receiver is efficient and clear. |
| 1235 | </p> |
| 1236 | |
| 1237 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1238 | <h3 id="new_and_make"> |
| 1239 | What's the difference between new and make?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1240 | |
| 1241 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1242 | In short: <code>new</code> allocates memory, <code>make</code> initializes |
| 1243 | the slice, map, and channel types. |
| 1244 | </p> |
| 1245 | |
| 1246 | <p> |
| 1247 | See the <a href="/doc/effective_go.html#allocation_new">relevant section |
| 1248 | of Effective Go</a> for more details. |
| 1249 | </p> |
| 1250 | |
Andrew Gerrand | affd1ba | 2010-12-09 08:59:29 +1100 | [diff] [blame] | 1251 | <h3 id="q_int_sizes"> |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 1252 | What is the size of an <code>int</code> on a 64 bit machine?</h3> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1253 | |
| 1254 | <p> |
Rob Pike | 80e25fc | 2011-01-19 23:07:38 -0500 | [diff] [blame] | 1255 | The sizes of <code>int</code> and <code>uint</code> are implementation-specific |
| 1256 | but the same as each other on a given platform. |
Russ Cox | 10ea651 | 2012-09-24 20:57:01 -0400 | [diff] [blame] | 1257 | For portability, code that relies on a particular |
Rob Pike | 80e25fc | 2011-01-19 23:07:38 -0500 | [diff] [blame] | 1258 | size of value should use an explicitly sized type, like <code>int64</code>. |
Russ Cox | 10ea651 | 2012-09-24 20:57:01 -0400 | [diff] [blame] | 1259 | Prior to Go 1.1, the 64-bit Go compilers (both gc and gccgo) used |
| 1260 | a 32-bit representation for <code>int</code>. As of Go 1.1 they use |
| 1261 | a 64-bit representation. |
Rob Pike | 80e25fc | 2011-01-19 23:07:38 -0500 | [diff] [blame] | 1262 | On the other hand, floating-point scalars and complex |
| 1263 | numbers are always sized: <code>float32</code>, <code>complex64</code>, |
| 1264 | etc., because programmers should be aware of precision when using |
| 1265 | floating-point numbers. |
| 1266 | The default size of a floating-point constant is <code>float64</code>. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1267 | </p> |
| 1268 | |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1269 | <h3 id="stack_or_heap"> |
| 1270 | How do I know whether a variable is allocated on the heap or the stack?</h3> |
| 1271 | |
| 1272 | <p> |
| 1273 | From a correctness standpoint, you don't need to know. |
| 1274 | Each variable in Go exists as long as there are references to it. |
| 1275 | The storage location chosen by the implementation is irrelevant to the |
| 1276 | semantics of the language. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1277 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1278 | |
| 1279 | <p> |
| 1280 | The storage location does have an effect on writing efficient programs. |
| 1281 | When possible, the Go compilers will allocate variables that are |
| 1282 | local to a function in that function's stack frame. However, if the |
| 1283 | compiler cannot prove that the variable is not referenced after the |
| 1284 | function returns, then the compiler must allocate the variable on the |
| 1285 | garbage-collected heap to avoid dangling pointer errors. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1286 | Also, if a local variable is very large, it might make more sense |
| 1287 | to store it on the heap rather than the stack. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1288 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1289 | |
| 1290 | <p> |
Rob Pike | 8649444 | 2011-11-08 16:26:03 -0800 | [diff] [blame] | 1291 | In the current compilers, if a variable has its address taken, that variable |
| 1292 | is a candidate for allocation on the heap. However, a basic <em>escape |
| 1293 | analysis</em> recognizes some cases when such variables will not |
| 1294 | 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] | 1295 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1296 | |
Andrew Gerrand | 2100947 | 2012-10-11 14:21:19 +1100 | [diff] [blame] | 1297 | <h3 id="Why_does_my_Go_process_use_so_much_virtual_memory"> |
| 1298 | Why does my Go process use so much virtual memory?</h3> |
| 1299 | |
| 1300 | <p> |
| 1301 | The Go memory allocator reserves a large region of virtual memory as an arena |
| 1302 | for allocations. This virtual memory is local to the specific Go process; the |
| 1303 | reservation does not deprive other processes of memory. |
| 1304 | </p> |
| 1305 | |
| 1306 | <p> |
| 1307 | To find the amount of actual memory allocated to a Go process, use the Unix |
| 1308 | <code>top</code> command and consult the <code>RES</code> (Linux) or |
| 1309 | <code>RSIZE</code> (Mac OS X) columns. |
| 1310 | <!-- TODO(adg): find out how this works on Windows --> |
| 1311 | </p> |
| 1312 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1313 | <h2 id="Concurrency">Concurrency</h2> |
| 1314 | |
| 1315 | <h3 id="What_operations_are_atomic_What_about_mutexes"> |
| 1316 | What operations are atomic? What about mutexes?</h3> |
| 1317 | |
| 1318 | <p> |
| 1319 | We haven't fully defined it all yet, but some details about atomicity are |
Andrew Gerrand | 48ba6fe | 2013-10-04 09:45:06 +1000 | [diff] [blame] | 1320 | available in the <a href="/ref/mem">Go Memory Model specification</a>. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1321 | </p> |
| 1322 | |
| 1323 | <p> |
| 1324 | Regarding mutexes, the <a href="/pkg/sync">sync</a> |
| 1325 | package implements them, but we hope Go programming style will |
| 1326 | encourage people to try higher-level techniques. In particular, consider |
| 1327 | structuring your program so that only one goroutine at a time is ever |
| 1328 | responsible for a particular piece of data. |
| 1329 | </p> |
| 1330 | |
| 1331 | <p> |
| 1332 | Do not communicate by sharing memory. Instead, share memory by communicating. |
| 1333 | </p> |
| 1334 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1335 | <p> |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 1336 | See the <a href="/doc/codewalk/sharemem/">Share Memory By Communicating</a> code walk and its <a href="//blog.golang.org/2010/07/share-memory-by-communicating.html">associated article</a> for a detailed discussion of this concept. |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1337 | </p> |
| 1338 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1339 | <h3 id="Why_no_multi_CPU"> |
| 1340 | Why doesn't my multi-goroutine program use multiple CPUs?</h3> |
| 1341 | |
| 1342 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1343 | The number of CPUs available simultaneously to executing goroutines is |
| 1344 | controlled by the <code>GOMAXPROCS</code> shell environment variable. |
| 1345 | In earlier releases of Go, the default value was 1, but as of Go 1.5 the default |
| 1346 | value is the number of cores available. |
Rob Pike | c97e73d | 2015-06-29 15:43:42 +1000 | [diff] [blame] | 1347 | Therefore programs compiled after 1.5 should demonstrate parallel execution |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1348 | of multiple goroutines. |
| 1349 | To change the behavior, set the environment variable or use the similarly-named |
| 1350 | <a href="/pkg/runtime/#GOMAXPROCS">function</a> |
| 1351 | of the runtime package to configure the |
| 1352 | run-time support to utilize a different number of threads. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1353 | </p> |
| 1354 | |
| 1355 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1356 | Programs that perform parallel computation might benefit from a further increase in |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1357 | <code>GOMAXPROCS</code>. |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 1358 | However, be aware that |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 1359 | <a href="//blog.golang.org/2013/01/concurrency-is-not-parallelism.html">concurrency |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 1360 | is not parallelism</a>. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1361 | </p> |
| 1362 | |
| 1363 | <h3 id="Why_GOMAXPROCS"> |
| 1364 | Why does using <code>GOMAXPROCS</code> > 1 sometimes make my program |
| 1365 | slower?</h3> |
| 1366 | |
| 1367 | <p> |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1368 | It depends on the nature of your program. |
Rob Pike | 01afb79 | 2012-01-26 14:44:38 -0800 | [diff] [blame] | 1369 | Problems that are intrinsically sequential cannot be sped up by adding |
| 1370 | more goroutines. |
| 1371 | Concurrency only becomes parallelism when the problem is |
| 1372 | intrinsically parallel. |
| 1373 | </p> |
| 1374 | |
| 1375 | <p> |
| 1376 | In practical terms, programs that spend more time |
| 1377 | communicating on channels than doing computation |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1378 | may experience performance degradation when using |
Rob Pike | 01afb79 | 2012-01-26 14:44:38 -0800 | [diff] [blame] | 1379 | multiple OS threads. |
| 1380 | This is because sending data between threads involves switching |
| 1381 | contexts, which has significant cost. |
Andrew Gerrand | 48ba6fe | 2013-10-04 09:45:06 +1000 | [diff] [blame] | 1382 | 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] | 1383 | from the Go specification has no significant parallelism although it launches many |
| 1384 | goroutines; increasing <code>GOMAXPROCS</code> is more likely to slow it down than |
| 1385 | to speed it up. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1386 | </p> |
| 1387 | |
| 1388 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1389 | Go's goroutine scheduler is not as good as it needs to be, although it |
| 1390 | has improved in recent releases. |
| 1391 | In the future, it may better optimize its use of OS threads. |
| 1392 | For now, if there are performance issues, |
| 1393 | setting <code>GOMAXPROCS</code> on a per-application basis may help. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1394 | </p> |
| 1395 | |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 1396 | <p> |
| 1397 | For more detail on this topic see the talk entitled, |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 1398 | <a href="//blog.golang.org/2013/01/concurrency-is-not-parallelism.html">Concurrency |
Rob Pike | 48ecfc9 | 2013-03-27 15:26:57 -0700 | [diff] [blame] | 1399 | is not Parallelism</a>. |
| 1400 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1401 | <h2 id="Functions_methods">Functions and Methods</h2> |
| 1402 | |
| 1403 | <h3 id="different_method_sets"> |
| 1404 | Why do T and *T have different method sets?</h3> |
| 1405 | |
| 1406 | <p> |
Andrew Gerrand | 48ba6fe | 2013-10-04 09:45:06 +1000 | [diff] [blame] | 1407 | From the <a href="/ref/spec#Types">Go Spec</a>: |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1408 | </p> |
| 1409 | |
| 1410 | <blockquote> |
| 1411 | The method set of any other named type <code>T</code> consists of all methods |
| 1412 | with receiver type <code>T</code>. The method set of the corresponding pointer |
| 1413 | type <code>*T</code> is the set of all methods with receiver <code>*T</code> or |
| 1414 | <code>T</code> (that is, it also contains the method set of <code>T</code>). |
| 1415 | </blockquote> |
| 1416 | |
| 1417 | <p> |
| 1418 | If an interface value contains a pointer <code>*T</code>, |
| 1419 | a method call can obtain a value by dereferencing the pointer, |
| 1420 | but if an interface value contains a value <code>T</code>, |
| 1421 | there is no useful way for a method call to obtain a pointer. |
| 1422 | </p> |
| 1423 | |
| 1424 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1425 | Even in cases where the compiler could take the address of a value |
| 1426 | to pass to the method, if the method modifies the value the changes |
| 1427 | will be lost in the caller. |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1428 | As an example, if the <code>Write</code> method of |
| 1429 | <a href="/pkg/bytes/#Buffer"><code>bytes.Buffer</code></a> |
| 1430 | used a value receiver rather than a pointer, |
| 1431 | this code: |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1432 | </p> |
| 1433 | |
| 1434 | <pre> |
| 1435 | var buf bytes.Buffer |
| 1436 | io.Copy(buf, os.Stdin) |
| 1437 | </pre> |
| 1438 | |
| 1439 | <p> |
| 1440 | would copy standard input into a <i>copy</i> of <code>buf</code>, |
| 1441 | not into <code>buf</code> itself. |
| 1442 | This is almost never the desired behavior. |
| 1443 | </p> |
| 1444 | |
| 1445 | <h3 id="closures_and_goroutines"> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1446 | What happens with closures running as goroutines?</h3> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1447 | |
| 1448 | <p> |
| 1449 | Some confusion may arise when using closures with concurrency. |
| 1450 | Consider the following program: |
| 1451 | </p> |
| 1452 | |
| 1453 | <pre> |
| 1454 | func main() { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1455 | done := make(chan bool) |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1456 | |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1457 | values := []string{"a", "b", "c"} |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1458 | for _, v := range values { |
| 1459 | go func() { |
| 1460 | fmt.Println(v) |
| 1461 | done <- true |
| 1462 | }() |
| 1463 | } |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1464 | |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1465 | // wait for all goroutines to complete before exiting |
| 1466 | for _ = range values { |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1467 | <-done |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1468 | } |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1469 | } |
| 1470 | </pre> |
| 1471 | |
| 1472 | <p> |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1473 | One might mistakenly expect to see <code>a, b, c</code> as the output. |
| 1474 | 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] | 1475 | each iteration of the loop uses the same instance of the variable <code>v</code>, so |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1476 | each closure shares that single variable. When the closure runs, it prints the |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1477 | value of <code>v</code> at the time <code>fmt.Println</code> is executed, |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1478 | but <code>v</code> may have been modified since the goroutine was launched. |
Christian Himpel | 89ed40c | 2012-11-12 07:25:54 -0800 | [diff] [blame] | 1479 | To help detect this and other problems before they happen, run |
Dmitriy Vyukov | 2e1ddeb | 2014-05-07 18:49:13 +0400 | [diff] [blame] | 1480 | <a href="/cmd/go/#hdr-Run_go_tool_vet_on_packages"><code>go vet</code></a>. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1481 | </p> |
| 1482 | |
| 1483 | <p> |
Rob Pike | 0cab7d5 | 2012-09-07 09:11:39 -0700 | [diff] [blame] | 1484 | To bind the current value of <code>v</code> to each closure as it is launched, one |
| 1485 | must modify the inner loop to create a new variable each iteration. |
| 1486 | One way is to pass the variable as an argument to the closure: |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1487 | </p> |
| 1488 | |
| 1489 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1490 | for _, v := range values { |
| 1491 | go func(<b>u</b> string) { |
| 1492 | fmt.Println(<b>u</b>) |
| 1493 | done <- true |
| 1494 | }(<b>v</b>) |
| 1495 | } |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1496 | </pre> |
| 1497 | |
| 1498 | <p> |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1499 | In this example, the value of <code>v</code> is passed as an argument to the |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1500 | anonymous function. That value is then accessible inside the function as |
| 1501 | the variable <code>u</code>. |
| 1502 | </p> |
| 1503 | |
Rob Pike | 0cab7d5 | 2012-09-07 09:11:39 -0700 | [diff] [blame] | 1504 | <p> |
| 1505 | Even easier is just to create a new variable, using a declaration style that may |
| 1506 | seem odd but works fine in Go: |
| 1507 | </p> |
| 1508 | |
| 1509 | <pre> |
| 1510 | for _, v := range values { |
| 1511 | <b>v := v</b> // create a new 'v'. |
| 1512 | go func() { |
| 1513 | fmt.Println(<b>v</b>) |
| 1514 | done <- true |
| 1515 | }() |
| 1516 | } |
| 1517 | </pre> |
| 1518 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1519 | <h2 id="Control_flow">Control flow</h2> |
| 1520 | |
| 1521 | <h3 id="Does_Go_have_a_ternary_form"> |
| 1522 | Does Go have the <code>?:</code> operator?</h3> |
| 1523 | |
| 1524 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1525 | There is no ternary testing operation in Go. You may use the following to achieve the same |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1526 | result: |
| 1527 | </p> |
| 1528 | |
| 1529 | <pre> |
| 1530 | if expr { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1531 | n = trueVal |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1532 | } else { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1533 | n = falseVal |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1534 | } |
| 1535 | </pre> |
| 1536 | |
| 1537 | <h2 id="Packages_Testing">Packages and Testing</h2> |
| 1538 | |
| 1539 | <h3 id="How_do_I_create_a_multifile_package"> |
| 1540 | How do I create a multifile package?</h3> |
| 1541 | |
| 1542 | <p> |
| 1543 | Put all the source files for the package in a directory by themselves. |
| 1544 | Source files can refer to items from different files at will; there is |
| 1545 | no need for forward declarations or a header file. |
| 1546 | </p> |
| 1547 | |
| 1548 | <p> |
| 1549 | Other than being split into multiple files, the package will compile and test |
| 1550 | just like a single-file package. |
| 1551 | </p> |
| 1552 | |
| 1553 | <h3 id="How_do_I_write_a_unit_test"> |
| 1554 | How do I write a unit test?</h3> |
| 1555 | |
| 1556 | <p> |
| 1557 | Create a new file ending in <code>_test.go</code> in the same directory |
| 1558 | as your package sources. Inside that file, <code>import "testing"</code> |
| 1559 | and write functions of the form |
| 1560 | </p> |
| 1561 | |
| 1562 | <pre> |
| 1563 | func TestFoo(t *testing.T) { |
| 1564 | ... |
| 1565 | } |
| 1566 | </pre> |
| 1567 | |
| 1568 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1569 | Run <code>go test</code> in that directory. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1570 | That script finds the <code>Test</code> functions, |
| 1571 | builds a test binary, and runs it. |
| 1572 | </p> |
| 1573 | |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1574 | <p>See the <a href="/doc/code.html">How to Write Go Code</a> document, |
| 1575 | the <a href="/pkg/testing/"><code>testing</code></a> package |
Andrew Gerrand | 399a36a | 2013-01-15 19:25:16 +1100 | [diff] [blame] | 1576 | and the <a href="/cmd/go/#hdr-Test_packages"><code>go test</code></a> subcommand for more details. |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1577 | </p> |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1578 | |
Rob Pike | f6615f1 | 2011-11-09 13:19:23 -0800 | [diff] [blame] | 1579 | <h3 id="testing_framework"> |
| 1580 | Where is my favorite helper function for testing?</h3> |
| 1581 | |
| 1582 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1583 | 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] | 1584 | features provided in other language's testing frameworks such as assertion functions. |
| 1585 | An <a href="#assertions">earlier section</a> of this document explained why Go |
| 1586 | doesn't have assertions, and |
| 1587 | the same arguments apply to the use of <code>assert</code> in tests. |
| 1588 | Proper error handling means letting other tests run after one has failed, so |
| 1589 | that the person debugging the failure gets a complete picture of what is |
| 1590 | wrong. It is more useful for a test to report that |
| 1591 | <code>isPrime</code> gives the wrong answer for 2, 3, 5, and 7 (or for |
| 1592 | 2, 4, 8, and 16) than to report that <code>isPrime</code> gives the wrong |
| 1593 | answer for 2 and therefore no more tests were run. The programmer who |
| 1594 | triggers the test failure may not be familiar with the code that fails. |
| 1595 | Time invested writing a good error message now pays off later when the |
| 1596 | test breaks. |
| 1597 | </p> |
| 1598 | |
| 1599 | <p> |
| 1600 | A related point is that testing frameworks tend to develop into mini-languages |
| 1601 | of their own, with conditionals and controls and printing mechanisms, |
| 1602 | but Go already has all those capabilities; why recreate them? |
| 1603 | We'd rather write tests in Go; it's one fewer language to learn and the |
| 1604 | approach keeps the tests straightforward and easy to understand. |
| 1605 | </p> |
| 1606 | |
| 1607 | <p> |
| 1608 | If the amount of extra code required to write |
| 1609 | good errors seems repetitive and overwhelming, the test might work better if |
| 1610 | table-driven, iterating over a list of inputs and outputs defined |
| 1611 | in a data structure (Go has excellent support for data structure literals). |
| 1612 | The work to write a good test and good error messages will then be amortized over many |
| 1613 | test cases. The standard Go library is full of illustrative examples, such as in |
Russ Cox | 220a6de | 2014-09-08 00:06:45 -0400 | [diff] [blame] | 1614 | <a href="/src/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] | 1615 | </p> |
| 1616 | |
Brad Fitzpatrick | 8a28242 | 2015-07-05 10:00:14 -0700 | [diff] [blame] | 1617 | <h3 id="x_in_std"> |
| 1618 | Why isn't <i>X</i> in the standard library?</h3> |
| 1619 | |
| 1620 | <p> |
| 1621 | The standard library's purpose is to support the runtime, connect to |
| 1622 | the operating system, and provide key functionality that many Go |
| 1623 | programs require, such as formatted I/O and networking. |
| 1624 | It also contains elements important for web programming, including |
| 1625 | cryptography and support for standards like HTTP, JSON, and XML. |
| 1626 | </p> |
| 1627 | |
| 1628 | <p> |
| 1629 | There is no clear criterion that defines what is included because for |
| 1630 | a long time, this was the <i>only</i> Go library. |
| 1631 | There are criteria that define what gets added today, however. |
| 1632 | </p> |
| 1633 | |
| 1634 | <p> |
| 1635 | New additions to the standard library are rare and the bar for |
| 1636 | inclusion is high. |
| 1637 | Code included in the standard library bears a large ongoing maintenance cost |
| 1638 | (often borne by those other than the original author), |
| 1639 | is subject to the <a href="/doc/go1compat.html">Go 1 compatibility promise</a> |
| 1640 | (blocking fixes to any flaws in the API), |
| 1641 | and is subject to the Go |
| 1642 | <a href="https://golang.org/s/releasesched">release schedule</a>, |
| 1643 | preventing bug fixes from being available to users quickly. |
| 1644 | </p> |
| 1645 | |
| 1646 | <p> |
| 1647 | Most new code should live outside of the standard library and be accessible |
| 1648 | via the <a href="/cmd/go/"><code>go</code> tool</a>'s |
| 1649 | <code>go get</code> command. |
| 1650 | Such code can have its own maintainers, release cycle, |
| 1651 | and compatibility guarantees. |
| 1652 | Users can find packages and read their documentation at |
| 1653 | <a href="https://godoc.org/">godoc.org</a>. |
| 1654 | </p> |
| 1655 | |
| 1656 | <p> |
| 1657 | Although there are pieces in the standard library that don't really belong, |
| 1658 | such as <code>log/syslog</code>, we continue to maintain everything in the |
| 1659 | library because of the Go 1 compatibility promise. |
| 1660 | But we encourage most new code to live elsewhere. |
| 1661 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1662 | |
| 1663 | <h2 id="Implementation">Implementation</h2> |
| 1664 | |
| 1665 | <h3 id="What_compiler_technology_is_used_to_build_the_compilers"> |
| 1666 | What compiler technology is used to build the compilers?</h3> |
| 1667 | |
| 1668 | <p> |
Mike Rosset | b4afe88 | 2013-03-12 17:12:56 -0700 | [diff] [blame] | 1669 | <code>Gccgo</code> has a front end written in C++, with a recursive descent parser coupled to the |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1670 | standard GCC back end. <code>Gc</code> is written in Go using |
| 1671 | <code>yacc</code>/<code>bison</code> for the parser |
| 1672 | and uses a custom loader, also written in Go but |
| 1673 | based on the Plan 9 loader, to generate ELF/Mach-O/PE binaries. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1674 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1675 | |
| 1676 | <p> |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1677 | We considered using LLVM for <code>gc</code> but we felt it was too large and |
| 1678 | slow to meet our performance goals. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1679 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1680 | |
| 1681 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1682 | The original <code>gc</code>, the Go compiler, was written in C |
| 1683 | because of the difficulties of bootstrapping—you'd need a Go compiler to |
| 1684 | set up a Go environment. |
| 1685 | But things have advanced and as of Go 1.5 the compiler is written in Go. |
| 1686 | It was converted from C to Go using automatic translation tools, as |
| 1687 | described in <a href="/s/go13compiler">this design document</a> |
| 1688 | and <a href="https://talks.golang.org/2015/gogo.slide#1">a recent talk</a>. |
| 1689 | Thus the compiler is now "self-hosting", which means we must face |
| 1690 | the bootstrapping problem. |
| 1691 | The solution, naturally, is to have a working Go installation already, |
| 1692 | just as one normally has a working C installation in place. |
| 1693 | The story of how to bring up a new Go installation from source |
| 1694 | is described <a href="/s/go15bootstrap">separately</a>. |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1695 | </p> |
| 1696 | |
| 1697 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1698 | Go is a fine language in which to implement a Go compiler. |
| 1699 | Although <code>gc</code> does not use them (yet?), a native lexer and |
| 1700 | parser are available in the <a href="/pkg/go/"><code>go</code></a> package |
| 1701 | and there is also a <a href="/pkg/go/types">type checker</a>. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1702 | </p> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1703 | |
Rob Pike | 966bf71 | 2011-03-01 13:54:22 -0800 | [diff] [blame] | 1704 | <h3 id="How_is_the_run_time_support_implemented"> |
| 1705 | How is the run-time support implemented?</h3> |
Russ Cox | 3227445 | 2009-10-22 00:13:51 -0700 | [diff] [blame] | 1706 | |
| 1707 | <p> |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1708 | Again due to bootstrapping issues, the run-time code was originally written mostly in C (with a |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1709 | tiny bit of assembler) but it has since been translated to Go |
| 1710 | (except for some assembler bits). |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1711 | <code>Gccgo</code>'s run-time support uses <code>glibc</code>. |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1712 | The <code>gccgo</code> compiler implements goroutines using |
| 1713 | a technique called segmented stacks, |
Rob Pike | 287967f | 2014-03-21 13:59:30 +1100 | [diff] [blame] | 1714 | supported by recent modifications to the gold linker. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1715 | </p> |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1716 | |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1717 | <h3 id="Why_is_my_trivial_program_such_a_large_binary"> |
| 1718 | Why is my trivial program such a large binary?</h3> |
| 1719 | |
| 1720 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1721 | The linker in the <code>gc</code> tool chain |
| 1722 | creates statically-linked binaries by default. All Go binaries therefore include the Go |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1723 | run-time, along with the run-time type information necessary to support dynamic |
| 1724 | type checks, reflection, and even panic-time stack traces. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1725 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1726 | |
| 1727 | <p> |
Rob Pike | 0130a31 | 2012-03-07 15:29:26 +1100 | [diff] [blame] | 1728 | A simple C "hello, world" program compiled and linked statically using gcc |
| 1729 | on Linux is around 750 kB, |
| 1730 | including an implementation of <code>printf</code>. |
| 1731 | An equivalent Go program using <code>fmt.Printf</code> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1732 | is around 2.3 MB, but |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1733 | that includes more powerful run-time support and type information. |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1734 | </p> |
Andrew Gerrand | 4b0ecd3 | 2011-03-01 21:35:46 +1100 | [diff] [blame] | 1735 | |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 1736 | <h3 id="unused_variables_and_imports"> |
| 1737 | Can I stop these complaints about my unused variable/import?</h3> |
| 1738 | |
| 1739 | <p> |
| 1740 | The presence of an unused variable may indicate a bug, while |
Rob Pike | dba2faf | 2014-10-01 15:25:56 -0700 | [diff] [blame] | 1741 | unused imports just slow down compilation, |
| 1742 | an effect that can become substantial as a program accumulates |
| 1743 | code and programmers over time. |
| 1744 | For these reasons, Go refuses to compile programs with unused |
| 1745 | variables or imports, |
| 1746 | trading short-term convenience for long-term build speed and |
| 1747 | program clarity. |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 1748 | </p> |
| 1749 | |
| 1750 | <p> |
Rob Pike | dba2faf | 2014-10-01 15:25:56 -0700 | [diff] [blame] | 1751 | Still, when developing code, it's common to create these situations |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 1752 | temporarily and it can be annoying to have to edit them out before the |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1753 | program will compile. |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 1754 | </p> |
| 1755 | |
| 1756 | <p> |
| 1757 | Some have asked for a compiler option to turn those checks off |
| 1758 | or at least reduce them to warnings. |
| 1759 | Such an option has not been added, though, |
| 1760 | because compiler options should not affect the semantics of the |
| 1761 | language and because the Go compiler does not report warnings, only |
| 1762 | errors that prevent compilation. |
| 1763 | </p> |
| 1764 | |
| 1765 | <p> |
| 1766 | There are two reasons for having no warnings. First, if it's worth |
| 1767 | complaining about, it's worth fixing in the code. (And if it's not |
| 1768 | worth fixing, it's not worth mentioning.) Second, having the compiler |
| 1769 | generate warnings encourages the implementation to warn about weak |
| 1770 | cases that can make compilation noisy, masking real errors that |
| 1771 | <em>should</em> be fixed. |
| 1772 | </p> |
| 1773 | |
| 1774 | <p> |
| 1775 | It's easy to address the situation, though. Use the blank identifier |
| 1776 | to let unused things persist while you're developing. |
| 1777 | </p> |
| 1778 | |
| 1779 | <pre> |
| 1780 | import "unused" |
| 1781 | |
| 1782 | // This declaration marks the import as used by referencing an |
| 1783 | // item from the package. |
| 1784 | var _ = unused.Item // TODO: Delete before committing! |
| 1785 | |
| 1786 | func main() { |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1787 | debugData := debug.Profile() |
| 1788 | _ = debugData // Used only during debugging. |
| 1789 | .... |
Rob Pike | 7d87f3d | 2011-08-06 11:21:59 +1000 | [diff] [blame] | 1790 | } |
| 1791 | </pre> |
| 1792 | |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1793 | <p> |
| 1794 | Nowadays, most Go programmers use a tool, |
Andrew Gerrand | 7f0be1f | 2014-11-10 09:15:57 +1100 | [diff] [blame] | 1795 | <a href="http://godoc.org/golang.org/x/tools/cmd/goimports">goimports</a>, |
Rob Pike | 56c4d0a | 2014-09-27 11:56:54 -0700 | [diff] [blame] | 1796 | which automatically rewrites a Go source file to have the correct imports, |
| 1797 | eliminating the unused imports issue in practice. |
| 1798 | This program is easily connected to most editors to run automatically when a Go source file is written. |
| 1799 | </p> |
| 1800 | |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1801 | <h2 id="Performance">Performance</h2> |
| 1802 | |
| 1803 | <h3 id="Why_does_Go_perform_badly_on_benchmark_x"> |
| 1804 | Why does Go perform badly on benchmark X?</h3> |
| 1805 | |
| 1806 | <p> |
| 1807 | One of Go's design goals is to approach the performance of C for comparable |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1808 | programs, yet on some benchmarks it does quite poorly, including several |
| 1809 | in <a href="/test/bench/shootout/">test/bench/shootout</a>. The slowest depend on libraries |
| 1810 | for which versions of comparable performance are not available in Go. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1811 | For instance, <a href="/test/bench/shootout/pidigits.go">pidigits.go</a> |
| 1812 | depends on a multi-precision math package, and the C |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1813 | versions, unlike Go's, use <a href="http://gmplib.org/">GMP</a> (which is |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1814 | written in optimized assembler). |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1815 | Benchmarks that depend on regular expressions |
| 1816 | (<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] | 1817 | 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] | 1818 | mature, highly optimized regular expression libraries like PCRE. |
| 1819 | </p> |
| 1820 | |
| 1821 | <p> |
| 1822 | Benchmark games are won by extensive tuning and the Go versions of most |
| 1823 | of the benchmarks need attention. If you measure comparable C |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1824 | and Go programs |
| 1825 | (<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] | 1826 | languages are much closer in raw performance than this suite would |
| 1827 | indicate. |
| 1828 | </p> |
| 1829 | |
| 1830 | <p> |
| 1831 | 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] | 1832 | better, many libraries need major performance work, and the garbage collector |
Oling Cat | 018e89f | 2013-01-24 20:46:33 +1100 | [diff] [blame] | 1833 | isn't fast enough yet. (Even if it were, taking care not to generate unnecessary |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1834 | garbage can have a huge effect.) |
Andrew Gerrand | 0857573 | 2010-04-21 14:00:56 +1000 | [diff] [blame] | 1835 | </p> |
| 1836 | |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1837 | <p> |
Rob Pike | 5cff190 | 2012-02-20 12:42:50 +1100 | [diff] [blame] | 1838 | In any case, Go can often be very competitive. |
| 1839 | There has been significant improvement in the performance of many programs |
| 1840 | as the language and tools have developed. |
| 1841 | See the blog post about |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 1842 | <a href="//blog.golang.org/2011/06/profiling-go-programs.html">profiling |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1843 | Go programs</a> for an informative example. |
| 1844 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1845 | <h2 id="change_from_c">Changes from C</h2> |
| 1846 | |
| 1847 | <h3 id="different_syntax"> |
| 1848 | Why is the syntax so different from C?</h3> |
| 1849 | <p> |
| 1850 | Other than declaration syntax, the differences are not major and stem |
| 1851 | from two desires. First, the syntax should feel light, without too |
| 1852 | many mandatory keywords, repetition, or arcana. Second, the language |
| 1853 | has been designed to be easy to analyze |
| 1854 | and can be parsed without a symbol table. This makes it much easier |
| 1855 | to build tools such as debuggers, dependency analyzers, automated |
| 1856 | documentation extractors, IDE plug-ins, and so on. C and its |
| 1857 | descendants are notoriously difficult in this regard. |
| 1858 | </p> |
| 1859 | |
| 1860 | <h3 id="declarations_backwards"> |
| 1861 | Why are declarations backwards?</h3> |
| 1862 | <p> |
| 1863 | They're only backwards if you're used to C. In C, the notion is that a |
| 1864 | variable is declared like an expression denoting its type, which is a |
| 1865 | nice idea, but the type and expression grammars don't mix very well and |
| 1866 | the results can be confusing; consider function pointers. Go mostly |
| 1867 | separates expression and type syntax and that simplifies things (using |
| 1868 | prefix <code>*</code> for pointers is an exception that proves the rule). In C, |
| 1869 | the declaration |
| 1870 | </p> |
| 1871 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1872 | int* a, b; |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1873 | </pre> |
| 1874 | <p> |
| 1875 | declares <code>a</code> to be a pointer but not <code>b</code>; in Go |
| 1876 | </p> |
| 1877 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1878 | var a, b *int |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1879 | </pre> |
| 1880 | <p> |
| 1881 | declares both to be pointers. This is clearer and more regular. |
| 1882 | Also, the <code>:=</code> short declaration form argues that a full variable |
| 1883 | declaration should present the same order as <code>:=</code> so |
| 1884 | </p> |
| 1885 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1886 | var a uint64 = 1 |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1887 | </pre> |
Stefan Nilsson | c50074e | 2012-02-29 15:07:52 -0800 | [diff] [blame] | 1888 | <p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1889 | has the same effect as |
Stefan Nilsson | c50074e | 2012-02-29 15:07:52 -0800 | [diff] [blame] | 1890 | </p> |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1891 | <pre> |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1892 | a := uint64(1) |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1893 | </pre> |
| 1894 | <p> |
| 1895 | Parsing is also simplified by having a distinct grammar for types that |
| 1896 | is not just the expression grammar; keywords such as <code>func</code> |
| 1897 | and <code>chan</code> keep things clear. |
| 1898 | </p> |
| 1899 | |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1900 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1901 | See the article about |
Francisco Souza | 6033a48 | 2012-03-13 14:46:08 +1100 | [diff] [blame] | 1902 | <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] | 1903 | for more details. |
Andrew Gerrand | 5ec55c5 | 2010-09-30 11:23:39 +1000 | [diff] [blame] | 1904 | </p> |
| 1905 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1906 | <h3 id="no_pointer_arithmetic"> |
| 1907 | Why is there no pointer arithmetic?</h3> |
| 1908 | <p> |
| 1909 | Safety. Without pointer arithmetic it's possible to create a |
| 1910 | language that can never derive an illegal address that succeeds |
| 1911 | incorrectly. Compiler and hardware technology have advanced to the |
| 1912 | point where a loop using array indices can be as efficient as a loop |
| 1913 | using pointer arithmetic. Also, the lack of pointer arithmetic can |
| 1914 | simplify the implementation of the garbage collector. |
| 1915 | </p> |
| 1916 | |
| 1917 | <h3 id="inc_dec"> |
| 1918 | Why are <code>++</code> and <code>--</code> statements and not expressions? And why postfix, not prefix?</h3> |
| 1919 | <p> |
| 1920 | Without pointer arithmetic, the convenience value of pre- and postfix |
| 1921 | increment operators drops. By removing them from the expression |
| 1922 | hierarchy altogether, expression syntax is simplified and the messy |
| 1923 | issues around order of evaluation of <code>++</code> and <code>--</code> |
| 1924 | (consider <code>f(i++)</code> and <code>p[i] = q[++i]</code>) |
| 1925 | are eliminated as well. The simplification is |
| 1926 | significant. As for postfix vs. prefix, either would work fine but |
| 1927 | the postfix version is more traditional; insistence on prefix arose |
| 1928 | with the STL, a library for a language whose name contains, ironically, a |
| 1929 | postfix increment. |
| 1930 | </p> |
| 1931 | |
| 1932 | <h3 id="semicolons"> |
| 1933 | Why are there braces but no semicolons? And why can't I put the opening |
| 1934 | brace on the next line?</h3> |
| 1935 | <p> |
| 1936 | Go uses brace brackets for statement grouping, a syntax familiar to |
| 1937 | programmers who have worked with any language in the C family. |
| 1938 | Semicolons, however, are for parsers, not for people, and we wanted to |
| 1939 | eliminate them as much as possible. To achieve this goal, Go borrows |
| 1940 | a trick from BCPL: the semicolons that separate statements are in the |
| 1941 | formal grammar but are injected automatically, without lookahead, by |
| 1942 | the lexer at the end of any line that could be the end of a statement. |
| 1943 | This works very well in practice but has the effect that it forces a |
| 1944 | brace style. For instance, the opening brace of a function cannot |
| 1945 | appear on a line by itself. |
| 1946 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1947 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1948 | <p> |
| 1949 | Some have argued that the lexer should do lookahead to permit the |
| 1950 | brace to live on the next line. We disagree. Since Go code is meant |
| 1951 | to be formatted automatically by |
Shenghou Ma | 97b13ac | 2012-03-07 08:15:47 +1100 | [diff] [blame] | 1952 | <a href="/cmd/gofmt/"><code>gofmt</code></a>, |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1953 | <i>some</i> style must be chosen. That style may differ from what |
| 1954 | you've used in C or Java, but Go is a new language and |
| 1955 | <code>gofmt</code>'s style is as good as any other. More |
| 1956 | important—much more important—the advantages of a single, |
| 1957 | programmatically mandated format for all Go programs greatly outweigh |
| 1958 | any perceived disadvantages of the particular style. |
| 1959 | Note too that Go's style means that an interactive implementation of |
| 1960 | Go can use the standard syntax one line at a time without special rules. |
| 1961 | </p> |
| 1962 | |
| 1963 | <h3 id="garbage_collection"> |
| 1964 | Why do garbage collection? Won't it be too expensive?</h3> |
| 1965 | <p> |
| 1966 | One of the biggest sources of bookkeeping in systems programs is |
| 1967 | memory management. We feel it's critical to eliminate that |
| 1968 | programmer overhead, and advances in garbage collection |
| 1969 | technology in the last few years give us confidence that we can |
| 1970 | implement it with low enough overhead and no significant |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1971 | latency. |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1972 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1973 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1974 | <p> |
| 1975 | Another point is that a large part of the difficulty of concurrent |
| 1976 | and multi-threaded programming is memory management; |
| 1977 | as objects get passed among threads it becomes cumbersome |
| 1978 | to guarantee they become freed safely. |
| 1979 | Automatic garbage collection makes concurrent code far easier to write. |
| 1980 | Of course, implementing garbage collection in a concurrent environment is |
| 1981 | itself a challenge, but meeting it once rather than in every |
| 1982 | program helps everyone. |
| 1983 | </p> |
Evan Shaw | 64d8576 | 2011-05-22 14:56:12 +1000 | [diff] [blame] | 1984 | |
Andrew Gerrand | 4164d60 | 2010-09-29 16:52:22 +1000 | [diff] [blame] | 1985 | <p> |
| 1986 | Finally, concurrency aside, garbage collection makes interfaces |
| 1987 | simpler because they don't need to specify how memory is managed across them. |
| 1988 | </p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 1989 | |
| 1990 | <p> |
Rob Pike | 694b244 | 2015-06-29 13:27:41 +1000 | [diff] [blame] | 1991 | The current implementation is a parallel mark-and-sweep collector. |
| 1992 | Recent improvements, documented in |
| 1993 | <a href="/s/go14gc">this design document</a>, |
| 1994 | have introduced bounded pause times and improved the |
| 1995 | parallelism. |
| 1996 | Future versions might attempt new approaches. |
Rob Pike | fcfed14 | 2012-01-23 08:39:53 -0800 | [diff] [blame] | 1997 | </p> |
| 1998 | |
| 1999 | <p> |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 2000 | On the topic of performance, keep in mind that Go gives the programmer |
| 2001 | considerable control over memory layout and allocation, much more than |
| 2002 | is typical in garbage-collected languages. A careful programmer can reduce |
| 2003 | the garbage collection overhead dramatically by using the language well; |
| 2004 | see the article about |
Andrew Gerrand | 43ad89d | 2014-07-25 10:28:39 +1000 | [diff] [blame] | 2005 | <a href="//blog.golang.org/2011/06/profiling-go-programs.html">profiling |
Rob Pike | 93c4a24 | 2011-08-06 07:41:55 +1000 | [diff] [blame] | 2006 | Go programs</a> for a worked example, including a demonstration of Go's |
| 2007 | profiling tools. |
| 2008 | </p> |