blob: 9205c4507e1d6e24248504e85bc66ed3edbda312 [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">
82Why doesn't Go run on Windows?</h3>
83
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
87programs. However, the Go team is small and we don't have the
88resources to do a Windows port at the moment. We would be
89more than willing to answer questions and offer advice to anyone
90willing to develop a Windows version.
91</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 Pike0c2a4792009-11-01 20:50:42 -0800155Protocol buffers are supported. We plan to have the next release of the
156protocol buffer source code include Go code generators
157and a Go library for them. The implementation uses data reflection
158at run time so it is slow, but a new implementation is planned.
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.
165However, if choose to add the Google logo or branding to your site
166(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
Rob Pike0c2a4792009-11-01 20:50:42 -0800223<h2 id="Concurrent_programming">Concurrent programming</h2>
Russ Cox32274452009-10-22 00:13:51 -0700224
225<h3 id="What_operations_are_atomic_What_about_mutexes">
226What operations are atomic? What about mutexes?</h3>
227
228<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800229We haven't fully defined it all yet, but some details about atomicity are available in the
Russ Cox32274452009-10-22 00:13:51 -0700230<a href="go_mem.html">Go Memory Model specification</a>.
Rob Pike0c2a4792009-11-01 20:50:42 -0800231Also, some concurrency questions are answered in more detail in the
232<a href="go_lang_faq.html">language design FAQ</a>.
Russ Cox32274452009-10-22 00:13:51 -0700233
234<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800235Regarding mutexes, the <a href="/pkg/sync">sync</a>
236package implements them, but we hope Go programming style will
Russ Cox32274452009-10-22 00:13:51 -0700237encourage people to try higher-level techniques. In particular, consider
238structuring your program so that only one goroutine at a time is ever
239responsible for a particular piece of data.
240
241<p>
242Do not communicate by sharing memory. Instead, share memory by communicating.
243
244<h2 id="Writing_Code">Writing Code</h2>
245
246<h3 id="How_are_libraries_documented">
247How are libraries documented?</h3>
248
249<p>
250There is a program, <code>godoc</code>, written in Go, that extracts
251package documentation from the source code. It can be used on the
252command line or on the web. An instance is running at
Rob Pike0c2a4792009-11-01 20:50:42 -0800253<a href="http://golang.org/pkg/">http://golang.org/pkg/</a>.
Russ Coxe434f1a2009-11-07 17:31:22 -0800254In fact, <code>godoc</code> implements the full site at
Rob Pike0c2a4792009-11-01 20:50:42 -0800255<a href="http://golang.org/">http://golang.org/</a>.
Russ Cox32274452009-10-22 00:13:51 -0700256
257<h3 id="Is_there_a_Go_programming_style_guide">
258Is there a Go programming style guide?</h3>
259
260<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800261Eventually, there may be a small number of rules to guide things
262like naming, layout, and file organization.
263The document <a href="effective_go.html">Effective Go</a>
264contains some style advice.
265More directly, the program <code>gofmt</code> is a pretty-printer
266whose purpose is to enforce layout rules; it replaces the usual
267compendium of do's and don'ts that allows interpretation.
268All the Go code in the repository has been run through <code>gofmt</code>.
Russ Cox32274452009-10-22 00:13:51 -0700269
270<h3 id="How_do_I_submit_patches_to_the_Go_libraries">
271How do I submit patches to the Go libraries?</h3>
272
Rob Pike0c2a4792009-11-01 20:50:42 -0800273<p>
274The library sources are in <code>go/src/pkg</code>.
275If you want to make a significant change, please discuss on the mailing list before embarking.
Russ Cox32274452009-10-22 00:13:51 -0700276
Rob Pike0c2a4792009-11-01 20:50:42 -0800277<p>
278See the document
279<a href="contribute.html">Contributing to the Go project</a>
280for more information about how to proceed.
Russ Cox32274452009-10-22 00:13:51 -0700281
282<h3 id="How_do_I_create_a_multifile_package">
283How do I create a multifile package?</h3>
284
285<p>
286Put all the source files for the package in a directory by themselves.
287Source files can refer to items from different files at will; there is
288no header file or need for forward declarations.
289
290<p>
291Other than being split into multiple files, the package will compile and test
292just like a single-file package.
293
294<h3 id="How_do_I_write_a_unit_test">
295How do I write a unit test?</h3>
296
297<p>
298Create a new file ending in <code>_test.go</code> in the same directory
299as your package sources. Inside that file, <code>import "testing"</code>
300and write functions of the form
301
302<pre>
303func TestFoo(t *testing.T) {
304 ...
305}
306</pre>
307
308<p>
309Run <code>gotest</code> in that directory.
310That script finds the <code>Test</code> functions,
311builds a test binary, and runs it.
312
313<h3 id="Where_is_assert">
314Where is assert?</h3>
315
316<p>
317Go doesn't provide assertions. They are undeniably convenient, but our
318experience has been that programmers use them as a crutch to avoid thinking
319about proper error handling and reporting. Proper error handling means that
320servers continue operation after non-fatal errors instead of crashing.
321Proper error reporting means that errors are direct and to the point,
322saving the programmer from interpreting a large crash trace. Precise
323errors are particularly important when the programmer seeing the errors is
324not familiar with the code.
325
326<p>
327The same arguments apply to the use of <code>assert()</code> in test programs. Proper
328error handling means letting other tests run after one has failed, so
329that the person debugging the failure gets a complete picture of what is
330wrong. It is more useful for a test to report that
331<code>isPrime</code> gives the wrong answer for 2, 3, 5, and 7 (or for
3322, 4, 8, and 16) than to report that <code>isPrime</code> gives the wrong
333answer for 2 and therefore no more tests were run. The programmer who
Rob Pike0c2a4792009-11-01 20:50:42 -0800334triggers the test failure may not be familiar with the code that fails.
Russ Cox32274452009-10-22 00:13:51 -0700335Time invested writing a good error message now pays off later when the
336test breaks.
337
338<p>
339In testing, if the amount of extra code required to write
340good errors seems repetitive and overwhelming, it might work better as a
Rob Pike0c2a4792009-11-01 20:50:42 -0800341table-driven test instead.
Russ Cox32274452009-10-22 00:13:51 -0700342Go has excellent support for data structure literals.
343
344<p>
345We understand that this is a point of contention. There are many things in
346the Go language and libraries that differ from modern practices, simply
Rob Pike0c2a4792009-11-01 20:50:42 -0800347because we feel it's sometimes worth trying a different approach.
Russ Cox32274452009-10-22 00:13:51 -0700348
349<h2 id="Implementation">Implementation</h2>
350
351<h3 id="What_compiler_technology_is_used_to_build_the_compilers">
352What compiler technology is used to build the compilers?</h3>
353
354<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800355<code>Gccgo</code> has a C++ front-end with a recursive descent parser coupled to the
356standard GCC back end. <code>Gc</code> is written in C using
357<code>yacc</code>/<code>bison</code> for the parser.
Russ Cox32274452009-10-22 00:13:51 -0700358Although it's a new program, it fits in the Plan 9 C compiler suite
359(<a href="http://plan9.bell-labs.com/sys/doc/compiler.html">http://plan9.bell-labs.com/sys/doc/compiler.html</a>)
360and uses a variant of the Plan 9 loader to generate ELF binaries.
361
362<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800363We considered writing <code>6g</code>, the original Go compiler, in Go itself but
Russ Cox32274452009-10-22 00:13:51 -0700364elected not to do so because of the difficulties of bootstrapping and
Rob Pike0c2a4792009-11-01 20:50:42 -0800365especially of open source distribution&mdash;you'd need a Go compiler to
366set up a Go environment. <code>Gccgo</code>, which came later, makes it possible to
367consider writing a compiler in Go, which might well happen. (Go would be a
Russ Cox32274452009-10-22 00:13:51 -0700368fine language in which to implement a compiler; a native lexer and
Rob Pike0c2a4792009-11-01 20:50:42 -0800369parser are already available in <a href="/pkg/go/"><code>/pkg/go</code></a>.)
Russ Cox32274452009-10-22 00:13:51 -0700370
371<p>
Rob Pike0c2a4792009-11-01 20:50:42 -0800372We also considered using LLVM for <code>6g</code> but we felt it was too large and
Russ Cox32274452009-10-22 00:13:51 -0700373slow to meet our performance goals.
374
375<h3 id="How_is_the_runtime_implemented">
376How is the runtime implemented?</h3>
377
378<p>
379Again due to bootstrapping issues, the runtime is mostly in C (with a
380tiny bit of assembler) although Go is capable of implementing most of
Russ Coxe434f1a2009-11-07 17:31:22 -0800381it now. <code>Gccgo</code>'s runtime uses <code>glibc</code>.
Rob Pike0c2a4792009-11-01 20:50:42 -0800382<code>Gc</code> uses a custom library, to keep the footprint under
383control; it is
384compiled with a version of the Plan 9 C compiler that supports
385segmented stacks for goroutines.
Russ Coxe434f1a2009-11-07 17:31:22 -0800386Work is underway to provide the same stack management in
Rob Pike0c2a4792009-11-01 20:50:42 -0800387<code>gccgo</code>.