blob: 0445188062c90c91bf85f4d1dbf5c99ff899a7d2 [file] [log] [blame]
Rob Pike6898f952009-11-09 23:54:35 -08001<!-- FAQ -->
Russ Cox32274452009-10-22 00:13:51 -07002
3<h2 id="Origins">Origins</h2>
4
5<h3 id="What_is_the_purpose_of_the_project">
6What is the purpose of the project?</h3>
7
8<p>
9No major systems language has emerged in over a decade, but over that time
10the computing landscape has changed tremendously. There are several trends:
11
12<ul>
13<li>
14Computers are enormously quicker but software development is not faster.
15<li>
16Dependency management is a big part of software development today but the
Russ Coxe434f1a2009-11-07 17:31:22 -080017&ldquo;header files&rdquo; of languages in the C tradition are antithetical to clean
Russ Cox32274452009-10-22 00:13:51 -070018dependency analysis&mdash;and fast compilation.
19<li>
20There is a growing rebellion against cumbersome type systems like those of
21Java and C++, pushing people towards dynamically typed languages such as
Rob Pike0c2a4792009-11-01 20:50:42 -080022Python and JavaScript.
Russ Cox32274452009-10-22 00:13:51 -070023<li>
24Some fundamental concepts such as garbage collection and parallel computation
25are not well supported by popular systems languages.
26<li>
27The emergence of multicore computers has generated worry and confusion.
28</ul>
29
30<p>
31We believe it's worth trying again with a new language, a concurrent,
32garbage-collected language with fast compilation. Regarding the points above:
33
34<ul>
35<li>
Rob Pike0c2a4792009-11-01 20:50:42 -080036It is possible to compile a large Go program in a few seconds on a single computer.
Russ Cox32274452009-10-22 00:13:51 -070037<li>
38Go provides a model for software construction that makes dependency
39analysis easy and avoids much of the overhead of C-style include files and
40libraries.
41<li>
Rob Pike0c2a4792009-11-01 20:50:42 -080042Go's type system has no hierarchy, so no time is spent defining the
43relationships between types. Also, although Go has static types the language
Russ Cox32274452009-10-22 00:13:51 -070044attempts to make types feel lighter weight than in typical OO languages.
45<li>
46Go is fully garbage-collected and provides fundamental support for
47concurrent execution and communication.
48<li>
49By its design, Go proposes an approach for the construction of system
50software on multicore machines.
51</ul>
52
53<h3 id="What_is_the_origin_of_the_name">
54What is the origin of the name?</h3>
55
56<p>
Russ Coxe434f1a2009-11-07 17:31:22 -080057&ldquo;Ogle&rdquo; would be a good name for a Go debugger.
Russ Cox32274452009-10-22 00:13:51 -070058
Rob Pike0c2a4792009-11-01 20:50:42 -080059<h3 id="What_kind_of_a_name_is_6g">
60What kind of a name is 6g?</h3>
Russ Cox32274452009-10-22 00:13:51 -070061
62<p>
Rob Pike0c2a4792009-11-01 20:50:42 -080063The <code>6g</code> (and <code>8g</code> and <code>5g</code>) compiler is named in the
Russ Cox32274452009-10-22 00:13:51 -070064tradition of the Plan 9 C compilers, described in
Russ Coxe434f1a2009-11-07 17:31:22 -080065<a href="http://plan9.bell-labs.com/sys/doc/compiler.html">
Russ Cox32274452009-10-22 00:13:51 -070066http://plan9.bell-labs.com/sys/doc/compiler.html</a>
67(see the table in section 2).
68
Rob Pike0c2a4792009-11-01 20:50:42 -080069<code>6</code> is the architecture letter for amd64 (or x86-64, if you prefer), while
70<code>g</code> stands for Go.
Russ Cox32274452009-10-22 00:13:51 -070071
72<h3 id="Why_not_just_write_some_libraries_for_Cpp_to_do_communication">
73Why not just write some libraries for C++ to do communication?</h3>
74
75<p>We considered doing that, but too many of the problems&mdash;lack of
76garbage collection, long dependency chains, nested include files,
Rob Pike0c2a4792009-11-01 20:50:42 -080077lack of concurrency awareness&mdash;are rooted in the design of
78the C and C++ languages themselves.
Russ Cox32274452009-10-22 00:13:51 -070079We felt a viable solution required a more complete approach.
80
Rob Piked8134e72009-11-11 11:44:27 -080081<h3 id="Why_doesnt_Go_run_on_Windows">
Andrew Gerrand1d5af152010-03-04 12:56:08 +110082Why doesn't Go run on Windows yet?</h3>
Rob Piked8134e72009-11-11 11:44:27 -080083
84<p>
85We understand that a significant fraction of computers in the world
86run Windows and it would be great if those computers could run Go
Andrew Gerrand1d5af152010-03-04 12:56:08 +110087programs. A group of volunteers has made significant progress toward
88porting Go to <a href="http://www.mingw.org/">MinGW</a>.
89You can follow their progress on the
90<a href="http://groups.google.com/group/golang-nuts">mailing list</a>.
Rob Piked8134e72009-11-11 11:44:27 -080091</p>
92
Rob Pikebdecae92009-11-23 17:34:23 -080093<h3 id="Whats_the_origin_of_the_mascot">
94What's the origin of the mascot?</h3>
95
96<p>
97The mascot and logo were designed by
98<a href="http://reneefrench.blogspot.com">Renée French</a>, who also designed
99<a href="http://plan9.bell-labs.com/plan9/glenda.html">Glenda</a>,
100the Plan 9 bunny.
101The gopher is derived from one she used for an <a href="http://wfmu.org/">WFMU</a>
102T-shirt design some years ago.
103The logo and mascot are covered by the
104<a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0</a>
105license.
106</p>
107
Russ Cox32274452009-10-22 00:13:51 -0700108<h2 id="Usage">Usage</h2>
109
Rob Pike0c2a4792009-11-01 20:50:42 -0800110<h3 id="Who_should_use_the_language">
111Who should use the language?</h3>
Russ Cox32274452009-10-22 00:13:51 -0700112
113<p>
114Go is an experiment. We hope adventurous users will give it a try and see
Rob Pike0c2a4792009-11-01 20:50:42 -0800115if they enjoy it. Not every programmer
Russ Coxe434f1a2009-11-07 17:31:22 -0800116will, but we hope enough will find satisfaction in the approach it
Russ Cox32274452009-10-22 00:13:51 -0700117offers to justify further development.
118
Rob Pike7685a672009-11-09 20:25:45 -0800119<h3 id="Is_Google_using_go_internally"> Is Google using Go
120internally?</h3>
121
122<p> The Go project was conceived to make it easier to write the kind
123of servers and other software Google uses internally, but the
124implementation isn't quite mature enough yet for large-scale
125production use. While we continue development we are also doing
126experiments with the language as a candidate server environment. It's
127getting there. For instance, the server behind <a
128href="http://golang.org">http://golang.org</a> is a Go program; in
129fact it's just the <a href="/cmd/godoc"><code>godoc</code></a> document server running in a
130production configuration.
131
132
Russ Cox32274452009-10-22 00:13:51 -0700133<h3 id="Do_Go_programs_link_with_Cpp_programs">
134Do Go programs link with C/C++ programs?</h3>
135
136<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800137There are two Go compiler implementations, <code>6g</code> and friends, generically called
138<code>gc</code>, and <code>gccgo</code>.
139<code>Gc</code> uses a different calling convention and linker and can
Russ Cox32274452009-10-22 00:13:51 -0700140therefore only be linked with C programs using the same convention.
141There is such a C compiler but no C++ compiler. <code>Gccgo</code> is a
Rob Pike0c2a4792009-11-01 20:50:42 -0800142GCC front-end that can, with care, be linked with GCC-compiled
Russ Cox32274452009-10-22 00:13:51 -0700143C or C++ programs. However, because Go is garbage-collected it will be
144unwise to do so, at least naively.
145
146<p>
Russ Coxe434f1a2009-11-07 17:31:22 -0800147There is a &ldquo;foreign function interface&rdquo; to allow safe calling of C-written
Rob Pike0c2a4792009-11-01 20:50:42 -0800148libraries from Go code. We expect to use SWIG to extend this capability
149to C++ libraries. There is no safe way to call Go code from C or C++ yet.
Russ Cox32274452009-10-22 00:13:51 -0700150
Rob Pike0c2a4792009-11-01 20:50:42 -0800151<h3 id="Does_Go_support_Google_protocol_buffers">
152Does Go support Google's protocol buffers?</h3>
Russ Cox32274452009-10-22 00:13:51 -0700153
154<p>
Rob Pike6b3031b2010-03-23 17:03:28 -0700155A separate open source project provides the necessary compiler plugin and library.
156It is available at
157<a href="http://code.google.com/p/goprotobuf/">http://code.google.com/p/goprotobuf/</a>
158</p>
Russ Cox32274452009-10-22 00:13:51 -0700159
Russ Cox6301fb42009-12-03 17:23:33 -0800160<h3 id="Can_I_translate_the_Go_home_page">
161Can I translate the Go home page into another language?</h3>
162
163<p>
164Absolutely. We encourage developers to make Go Language sites in their own languages.
Andrew Gerrand08575732010-04-21 14:00:56 +1000165However, if you choose to add the Google logo or branding to your site
Russ Cox6301fb42009-12-03 17:23:33 -0800166(it does not appear on <a href="http://golang.org/">golang.org</a>),
167you will need to abide by the guidelines at
168<a href="http://www.google.com/permissions/guidelines.html">http://www.google.com/permissions/guidelines.html</a>
169</p>
170
Russ Cox32274452009-10-22 00:13:51 -0700171<h2 id="Design">Design</h2>
172
Rob Pike0c2a4792009-11-01 20:50:42 -0800173<h3 id="Why_doesnt_Go_have_feature_X">Why doesn't Go have feature X?</h3>
Russ Cox32274452009-10-22 00:13:51 -0700174
175<p>
176Every language contains novel features and omits someone's favorite
177feature. Go was designed with an eye on felicity of programming, speed of
178compilation, orthogonality of concepts, and the need to support features
179such as concurrency and garbage collection. Your favorite feature may be
180missing because it doesn't fit, because it affects compilation speed or
181clarity of design, or because it would make the fundamental system model
182too difficult.
183
184<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800185If it bothers you that Go is missing feature <var>X</var>,
186please forgive us and investigate the features that Go does have. You might find that
Russ Cox32274452009-10-22 00:13:51 -0700187they compensate in interesting ways for the lack of <var>X</var>.
188
189<h3 id="Why_is_the_syntax_so_different_from_Cpp">
190Why is the syntax so different from C++?</h3>
191
192<p>
193This and other language design questions are answered in
Rob Pike0c2a4792009-11-01 20:50:42 -0800194the separate <a href="go_lang_faq.html">language design FAQ</a>.
Russ Cox32274452009-10-22 00:13:51 -0700195
196<h2 id="Object_Oriented_Programming">
197Object-Oriented Programming</h2>
198
199<h3 id="Is_Go_an_object-oriented_language">
200Is Go an object-oriented language?</h3>
201
202<p>
203Yes and no. Although Go has types and methods and allows an
204object-oriented style of programming, there is no type hierarchy.
Russ Coxe434f1a2009-11-07 17:31:22 -0800205The concept of &ldquo;interface&rdquo; in Go provides a different approach that
Russ Cox32274452009-10-22 00:13:51 -0700206we believe is easy to use and in some ways more general. There are
207also ways to embed types in other types to provide something
208analogous&mdash;but not identical&mdash;to subclassing.
Rob Pike0c2a4792009-11-01 20:50:42 -0800209Moreover, methods in Go are more general than in C++ or Java:
210they can be defined for any sort of data, not just structs.
Russ Cox32274452009-10-22 00:13:51 -0700211
212<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800213Also, the lack of type hierarchy makes &ldquo;objects&rdquo; in Go feel much more
Russ Coxe434f1a2009-11-07 17:31:22 -0800214lightweight than in languages such as C++ or Java.
Russ Cox32274452009-10-22 00:13:51 -0700215
216<h3 id="How_do_I_get_dynamic_dispatch_of_methods">
217How do I get dynamic dispatch of methods?</h3>
218
219<p>
220The only way to have dynamically dispatched methods is through an
221interface. Methods on structs or other types are always resolved statically.
222
Russ Cox32274452009-10-22 00:13:51 -0700223
224<h2 id="Writing_Code">Writing Code</h2>
225
226<h3 id="How_are_libraries_documented">
227How are libraries documented?</h3>
228
229<p>
230There is a program, <code>godoc</code>, written in Go, that extracts
231package documentation from the source code. It can be used on the
232command line or on the web. An instance is running at
Rob Pike0c2a4792009-11-01 20:50:42 -0800233<a href="http://golang.org/pkg/">http://golang.org/pkg/</a>.
Russ Coxe434f1a2009-11-07 17:31:22 -0800234In fact, <code>godoc</code> implements the full site at
Rob Pike0c2a4792009-11-01 20:50:42 -0800235<a href="http://golang.org/">http://golang.org/</a>.
Russ Cox32274452009-10-22 00:13:51 -0700236
237<h3 id="Is_there_a_Go_programming_style_guide">
238Is there a Go programming style guide?</h3>
239
240<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800241Eventually, there may be a small number of rules to guide things
242like naming, layout, and file organization.
243The document <a href="effective_go.html">Effective Go</a>
244contains some style advice.
245More directly, the program <code>gofmt</code> is a pretty-printer
246whose purpose is to enforce layout rules; it replaces the usual
247compendium of do's and don'ts that allows interpretation.
248All the Go code in the repository has been run through <code>gofmt</code>.
Russ Cox32274452009-10-22 00:13:51 -0700249
250<h3 id="How_do_I_submit_patches_to_the_Go_libraries">
251How do I submit patches to the Go libraries?</h3>
252
Rob Pike0c2a4792009-11-01 20:50:42 -0800253<p>
254The library sources are in <code>go/src/pkg</code>.
255If you want to make a significant change, please discuss on the mailing list before embarking.
Russ Cox32274452009-10-22 00:13:51 -0700256
Rob Pike0c2a4792009-11-01 20:50:42 -0800257<p>
258See the document
259<a href="contribute.html">Contributing to the Go project</a>
260for more information about how to proceed.
Russ Cox32274452009-10-22 00:13:51 -0700261
Russ Cox32274452009-10-22 00:13:51 -0700262<h3 id="Where_is_assert">
263Where is assert?</h3>
264
265<p>
266Go doesn't provide assertions. They are undeniably convenient, but our
267experience has been that programmers use them as a crutch to avoid thinking
268about proper error handling and reporting. Proper error handling means that
269servers continue operation after non-fatal errors instead of crashing.
270Proper error reporting means that errors are direct and to the point,
271saving the programmer from interpreting a large crash trace. Precise
272errors are particularly important when the programmer seeing the errors is
273not familiar with the code.
274
275<p>
276The same arguments apply to the use of <code>assert()</code> in test programs. Proper
277error handling means letting other tests run after one has failed, so
278that the person debugging the failure gets a complete picture of what is
279wrong. It is more useful for a test to report that
280<code>isPrime</code> gives the wrong answer for 2, 3, 5, and 7 (or for
2812, 4, 8, and 16) than to report that <code>isPrime</code> gives the wrong
282answer for 2 and therefore no more tests were run. The programmer who
Rob Pike0c2a4792009-11-01 20:50:42 -0800283triggers the test failure may not be familiar with the code that fails.
Russ Cox32274452009-10-22 00:13:51 -0700284Time invested writing a good error message now pays off later when the
285test breaks.
286
287<p>
288In testing, if the amount of extra code required to write
289good errors seems repetitive and overwhelming, it might work better as a
Rob Pike0c2a4792009-11-01 20:50:42 -0800290table-driven test instead.
Russ Cox32274452009-10-22 00:13:51 -0700291Go has excellent support for data structure literals.
292
293<p>
294We understand that this is a point of contention. There are many things in
295the Go language and libraries that differ from modern practices, simply
Rob Pike0c2a4792009-11-01 20:50:42 -0800296because we feel it's sometimes worth trying a different approach.
Russ Cox32274452009-10-22 00:13:51 -0700297
298<h2 id="Implementation">Implementation</h2>
299
300<h3 id="What_compiler_technology_is_used_to_build_the_compilers">
301What compiler technology is used to build the compilers?</h3>
302
303<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800304<code>Gccgo</code> has a C++ front-end with a recursive descent parser coupled to the
305standard GCC back end. <code>Gc</code> is written in C using
306<code>yacc</code>/<code>bison</code> for the parser.
Russ Cox32274452009-10-22 00:13:51 -0700307Although it's a new program, it fits in the Plan 9 C compiler suite
308(<a href="http://plan9.bell-labs.com/sys/doc/compiler.html">http://plan9.bell-labs.com/sys/doc/compiler.html</a>)
309and uses a variant of the Plan 9 loader to generate ELF binaries.
310
311<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800312We considered writing <code>6g</code>, the original Go compiler, in Go itself but
Russ Cox32274452009-10-22 00:13:51 -0700313elected not to do so because of the difficulties of bootstrapping and
Rob Pike0c2a4792009-11-01 20:50:42 -0800314especially of open source distribution&mdash;you'd need a Go compiler to
315set up a Go environment. <code>Gccgo</code>, which came later, makes it possible to
316consider writing a compiler in Go, which might well happen. (Go would be a
Russ Cox32274452009-10-22 00:13:51 -0700317fine language in which to implement a compiler; a native lexer and
Rob Pike0c2a4792009-11-01 20:50:42 -0800318parser are already available in <a href="/pkg/go/"><code>/pkg/go</code></a>.)
Russ Cox32274452009-10-22 00:13:51 -0700319
320<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800321We also considered using LLVM for <code>6g</code> but we felt it was too large and
Russ Cox32274452009-10-22 00:13:51 -0700322slow to meet our performance goals.
323
324<h3 id="How_is_the_runtime_implemented">
325How is the runtime implemented?</h3>
326
327<p>
328Again due to bootstrapping issues, the runtime is mostly in C (with a
329tiny bit of assembler) although Go is capable of implementing most of
Russ Coxe434f1a2009-11-07 17:31:22 -0800330it now. <code>Gccgo</code>'s runtime uses <code>glibc</code>.
Rob Pike0c2a4792009-11-01 20:50:42 -0800331<code>Gc</code> uses a custom library, to keep the footprint under
332control; it is
333compiled with a version of the Plan 9 C compiler that supports
334segmented stacks for goroutines.
Russ Coxe434f1a2009-11-07 17:31:22 -0800335Work is underway to provide the same stack management in
Rob Pike0c2a4792009-11-01 20:50:42 -0800336<code>gccgo</code>.
Andrew Gerrand08575732010-04-21 14:00:56 +1000337
338<h2 id="Performance">Performance</h2>
339
340<h3 id="Why_does_Go_perform_badly_on_benchmark_x">
341Why does Go perform badly on benchmark X?</h3>
342
343<p>
344One of Go's design goals is to approach the performance of C for comparable
345programs, yet on some benchmarks it does quite poorly, including several
346in <a href="/test/bench/">test/bench</a>. The slowest depend on libraries
347for which versions of comparable performance are not available in Go.
348For instance, pidigits depends on a multi-precision math package, and the C
349versions, unlike Go's, use <a href="http://gmplib.org/">GMP</a> (which is
350written in optimized assembler).
351Benchmarks that depend on regular expressions (regex-dna, for instance) are
352essentially comparing Go's stopgap <a href="/pkg/regexp">regexp package</a> to
353mature, highly optimized regular expression libraries like PCRE.
354</p>
355
356<p>
357Benchmark games are won by extensive tuning and the Go versions of most
358of the benchmarks need attention. If you measure comparable C
359and Go programs (reverse-complement is one example), you'll see the two
360languages are much closer in raw performance than this suite would
361indicate.
362</p>
363
364<p>
365Still, there is room for improvement. The compilers are good but could be
366better, many librarise need major performance work, and the garbage collector
367isn't fast enough yet (even if it were, taking care not to generate unnecessary
368garbage can have a huge effect).
369</p>
370