OLD | NEW |
1 <!-- FAQ --> | 1 <!-- FAQ --> |
2 | 2 |
3 <h2 id="Origins">Origins</h2> | 3 <h2 id="Origins">Origins</h2> |
4 | 4 |
5 <h3 id="What_is_the_purpose_of_the_project"> | 5 <h3 id="What_is_the_purpose_of_the_project"> |
6 What is the purpose of the project?</h3> | 6 What is the purpose of the project?</h3> |
7 | 7 |
8 <p> | 8 <p> |
9 No major systems language has emerged in over a decade, but over that time | 9 No major systems language has emerged in over a decade, but over that time |
10 the computing landscape has changed tremendously. There are several trends: | 10 the computing landscape has changed tremendously. There are several trends: |
| 11 </p> |
11 | 12 |
12 <ul> | 13 <ul> |
13 <li> | 14 <li> |
14 Computers are enormously quicker but software development is not faster. | 15 Computers are enormously quicker but software development is not faster. |
15 <li> | 16 <li> |
16 Dependency management is a big part of software development today but the | 17 Dependency management is a big part of software development today but the |
17 “header files” of languages in the C tradition are antithetical to c
lean | 18 “header files” of languages in the C tradition are antithetical to c
lean |
18 dependency analysis—and fast compilation. | 19 dependency analysis—and fast compilation. |
19 <li> | 20 <li> |
20 There is a growing rebellion against cumbersome type systems like those of | 21 There is a growing rebellion against cumbersome type systems like those of |
21 Java and C++, pushing people towards dynamically typed languages such as | 22 Java and C++, pushing people towards dynamically typed languages such as |
22 Python and JavaScript. | 23 Python and JavaScript. |
23 <li> | 24 <li> |
24 Some fundamental concepts such as garbage collection and parallel computation | 25 Some fundamental concepts such as garbage collection and parallel computation |
25 are not well supported by popular systems languages. | 26 are not well supported by popular systems languages. |
26 <li> | 27 <li> |
27 The emergence of multicore computers has generated worry and confusion. | 28 The emergence of multicore computers has generated worry and confusion. |
28 </ul> | 29 </ul> |
29 </p> | |
30 | 30 |
31 <p> | 31 <p> |
32 We believe it's worth trying again with a new language, a concurrent, | 32 We believe it's worth trying again with a new language, a concurrent, |
33 garbage-collected language with fast compilation. Regarding the points above: | 33 garbage-collected language with fast compilation. Regarding the points above: |
| 34 </p> |
34 | 35 |
35 <ul> | 36 <ul> |
36 <li> | 37 <li> |
37 It is possible to compile a large Go program in a few seconds on a single comput
er. | 38 It is possible to compile a large Go program in a few seconds on a single comput
er. |
38 <li> | 39 <li> |
39 Go provides a model for software construction that makes dependency | 40 Go provides a model for software construction that makes dependency |
40 analysis easy and avoids much of the overhead of C-style include files and | 41 analysis easy and avoids much of the overhead of C-style include files and |
41 libraries. | 42 libraries. |
42 <li> | 43 <li> |
43 Go's type system has no hierarchy, so no time is spent defining the | 44 Go's type system has no hierarchy, so no time is spent defining the |
44 relationships between types. Also, although Go has static types the language | 45 relationships between types. Also, although Go has static types the language |
45 attempts to make types feel lighter weight than in typical OO languages. | 46 attempts to make types feel lighter weight than in typical OO languages. |
46 <li> | 47 <li> |
47 Go is fully garbage-collected and provides fundamental support for | 48 Go is fully garbage-collected and provides fundamental support for |
48 concurrent execution and communication. | 49 concurrent execution and communication. |
49 <li> | 50 <li> |
50 By its design, Go proposes an approach for the construction of system | 51 By its design, Go proposes an approach for the construction of system |
51 software on multicore machines. | 52 software on multicore machines. |
52 </ul> | 53 </ul> |
53 </p> | |
54 | 54 |
55 <h3 id="What_is_the_origin_of_the_name"> | 55 <h3 id="What_is_the_origin_of_the_name"> |
56 What is the origin of the name?</h3> | 56 What is the origin of the name?</h3> |
57 | 57 |
58 <p> | 58 <p> |
59 “Ogle” would be a good name for a Go debugger. | 59 “Ogle” would be a good name for a Go debugger. |
60 </p> | 60 </p> |
61 | 61 |
62 <h3 id="Whats_the_origin_of_the_mascot"> | 62 <h3 id="Whats_the_origin_of_the_mascot"> |
63 What's the origin of the mascot?</h3> | 63 What's the origin of the mascot?</h3> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 parallel with unrelated work. By January 2008, Ken had started work | 98 parallel with unrelated work. By January 2008, Ken had started work |
99 on a compiler with which to explore ideas; it generated C code as its | 99 on a compiler with which to explore ideas; it generated C code as its |
100 output. By mid-year the language had become a full-time project and | 100 output. By mid-year the language had become a full-time project and |
101 had settled enough to attempt a production compiler. In May 2008, | 101 had settled enough to attempt a production compiler. In May 2008, |
102 Ian Taylor independently started on a GCC front end for Go using the | 102 Ian Taylor independently started on a GCC front end for Go using the |
103 draft specification. Russ Cox joined in late 2008 and helped move the language | 103 draft specification. Russ Cox joined in late 2008 and helped move the language |
104 and libraries from prototype to reality. | 104 and libraries from prototype to reality. |
105 </p> | 105 </p> |
106 | 106 |
107 <p> | 107 <p> |
108 Many others have contributed ideas, discussions, and code. | 108 Go became a public open source project on November 10, 2009. |
| 109 Many people from the community have contributed ideas, discussions, and code. |
109 </p> | 110 </p> |
110 | 111 |
111 <h3 id="creating_a_new_language"> | 112 <h3 id="creating_a_new_language"> |
112 Why are you creating a new language?</h3> | 113 Why are you creating a new language?</h3> |
113 <p> | 114 <p> |
114 Go was born out of frustration with existing languages and | 115 Go was born out of frustration with existing languages and |
115 environments for systems programming. Programming had become too | 116 environments for systems programming. Programming had become too |
116 difficult and the choice of languages was partly to blame. One had to | 117 difficult and the choice of languages was partly to blame. One had to |
117 choose either efficient compilation, efficient execution, or ease of | 118 choose either efficient compilation, efficient execution, or ease of |
118 programming; all three were not available in the same mainstream | 119 programming; all three were not available in the same mainstream |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 Why does Go not have exceptions?</h3> | 308 Why does Go not have exceptions?</h3> |
308 <p> | 309 <p> |
309 We believe that coupling exceptions to a control | 310 We believe that coupling exceptions to a control |
310 structure, as in the <code>try-catch-finally</code> idiom, results in | 311 structure, as in the <code>try-catch-finally</code> idiom, results in |
311 convoluted code. It also tends to encourage programmers to label | 312 convoluted code. It also tends to encourage programmers to label |
312 too many ordinary errors, such as failing to open a file, as | 313 too many ordinary errors, such as failing to open a file, as |
313 exceptional. | 314 exceptional. |
314 </p> | 315 </p> |
315 | 316 |
316 <p> | 317 <p> |
317 Go takes a different approach. Instead of exceptions, it has a couple | 318 Go takes a different approach. For plain error handling, Go's multi-value |
| 319 returns make it easy to report an error without overloading the return value. |
| 320 <a href="http://blog.golang.org/2011/07/error-handling-and-go.html">A |
| 321 canonical error type, coupled |
| 322 with Go's other features</a>, makes error |
| 323 handling pleasant but quite different from that in other languages. |
| 324 </p> |
| 325 |
| 326 <p> |
| 327 Go also has a couple |
318 of built-in functions to signal and recover from truly exceptional | 328 of built-in functions to signal and recover from truly exceptional |
319 conditions. The recovery mechanism is executed only as part of a | 329 conditions. The recovery mechanism is executed only as part of a |
320 function's state being torn down after an error, which is sufficient | 330 function's state being torn down after an error, which is sufficient |
321 to handle catastrophe but requires no extra control structures and, | 331 to handle catastrophe but requires no extra control structures and, |
322 when used well, can result in clean error-handling code. | 332 when used well, can result in clean error-handling code. |
323 </p> | 333 </p> |
324 | 334 |
325 <p> | 335 <p> |
326 See the <a href="http://blog.golang.org/2010/08/defer-panic-and-recover.html">De
fer, Panic, and Recover</a> article for details. | 336 See the <a href="http://blog.golang.org/2010/08/defer-panic-and-recover.html">De
fer, Panic, and Recover</a> article for details. |
327 </p> | 337 </p> |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 the Go language and libraries that differ from modern practices, simply | 375 the Go language and libraries that differ from modern practices, simply |
366 because we feel it's sometimes worth trying a different approach. | 376 because we feel it's sometimes worth trying a different approach. |
367 </p> | 377 </p> |
368 | 378 |
369 <h3 id="csp"> | 379 <h3 id="csp"> |
370 Why build concurrency on the ideas of CSP?</h3> | 380 Why build concurrency on the ideas of CSP?</h3> |
371 <p> | 381 <p> |
372 Concurrency and multi-threaded programming have a reputation | 382 Concurrency and multi-threaded programming have a reputation |
373 for difficulty. We believe the problem is due partly to complex | 383 for difficulty. We believe the problem is due partly to complex |
374 designs such as pthreads and partly to overemphasis on low-level details | 384 designs such as pthreads and partly to overemphasis on low-level details |
375 such as mutexes, condition variables, and even memory barriers. | 385 such as mutexes, condition variables, and memory barriers. |
376 Higher-level interfaces enable much simpler code, even if there are still | 386 Higher-level interfaces enable much simpler code, even if there are still |
377 mutexes and such under the covers. | 387 mutexes and such under the covers. |
378 </p> | 388 </p> |
379 | 389 |
380 <p> | 390 <p> |
381 One of the most successful models for providing high-level linguistic support | 391 One of the most successful models for providing high-level linguistic support |
382 for concurrency comes from Hoare's Communicating Sequential Processes, or CSP. | 392 for concurrency comes from Hoare's Communicating Sequential Processes, or CSP. |
383 Occam and Erlang are two well known languages that stem from CSP. | 393 Occam and Erlang are two well known languages that stem from CSP. |
384 Go's concurrency primitives derive from a different part of the family tree | 394 Go's concurrency primitives derive from a different part of the family tree |
385 whose main contribution is the powerful notion of channels as first class object
s. | 395 whose main contribution is the powerful notion of channels as first class object
s. |
386 </p> | 396 </p> |
387 | 397 |
388 <h3 id="goroutines"> | 398 <h3 id="goroutines"> |
389 Why goroutines instead of threads?</h3> | 399 Why goroutines instead of threads?</h3> |
390 <p> | 400 <p> |
391 Goroutines are part of making concurrency easy to use. The idea, which has | 401 Goroutines are part of making concurrency easy to use. The idea, which has |
392 been around for a while, is to multiplex independently executing | 402 been around for a while, is to multiplex independently executing |
393 functions—coroutines, really—onto a set of threads. | 403 functions—coroutines—onto a set of threads. |
394 When a coroutine blocks, such as by calling a blocking system call, | 404 When a coroutine blocks, such as by calling a blocking system call, |
395 the run-time automatically moves other coroutines on the same operating | 405 the run-time automatically moves other coroutines on the same operating |
396 system thread to a different, runnable thread so they won't be blocked. | 406 system thread to a different, runnable thread so they won't be blocked. |
397 The programmer sees none of this, which is the point. | 407 The programmer sees none of this, which is the point. |
398 The result, which we call goroutines, can be very cheap: unless they spend a lot
of time | 408 The result, which we call goroutines, can be very cheap: unless they spend a lot
of time |
399 in long-running system calls, they cost little more than the memory | 409 in long-running system calls, they cost little more than the memory |
400 for the stack. | 410 for the stack, which is just a few kilobytes. |
401 </p> | 411 </p> |
402 | 412 |
403 <p> | 413 <p> |
404 To make the stacks small, Go's run-time uses segmented stacks. A newly | 414 To make the stacks small, Go's run-time uses segmented stacks. A newly |
405 minted goroutine is given a few kilobytes, which is almost always enough. | 415 minted goroutine is given a few kilobytes, which is almost always enough. |
406 When it isn't, the run-time allocates (and frees) extension segments automatical
ly. | 416 When it isn't, the run-time allocates (and frees) extension segments automatical
ly. |
407 The overhead averages about three cheap instructions per function call. | 417 The overhead averages about three cheap instructions per function call. |
408 It is practical to create hundreds of thousands of goroutines in the same | 418 It is practical to create hundreds of thousands of goroutines in the same |
409 address space. If goroutines were just threads, system resources would | 419 address space. If goroutines were just threads, system resources would |
410 run out at a much smaller number. | 420 run out at a much smaller number. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 different approach. | 476 different approach. |
467 </p> | 477 </p> |
468 | 478 |
469 <p> | 479 <p> |
470 Rather than requiring the programmer to declare ahead of time that two | 480 Rather than requiring the programmer to declare ahead of time that two |
471 types are related, in Go a type automatically satisfies any interface | 481 types are related, in Go a type automatically satisfies any interface |
472 that specifies a subset of its methods. Besides reducing the | 482 that specifies a subset of its methods. Besides reducing the |
473 bookkeeping, this approach has real advantages. Types can satisfy | 483 bookkeeping, this approach has real advantages. Types can satisfy |
474 many interfaces at once, without the complexities of traditional | 484 many interfaces at once, without the complexities of traditional |
475 multiple inheritance. | 485 multiple inheritance. |
476 Interfaces can be very lightweight—having one or even zero methods | 486 Interfaces can be very lightweight—an interface with |
477 in an interface can express useful concepts. | 487 one or even zero methods can express a useful concept. |
478 Interfaces can be added after the fact if a new idea comes along | 488 Interfaces can be added after the fact if a new idea comes along |
479 or for testing—without annotating the original types. | 489 or for testing—without annotating the original types. |
480 Because there are no explicit relationships between types | 490 Because there are no explicit relationships between types |
481 and interfaces, there is no type hierarchy to manage or discuss. | 491 and interfaces, there is no type hierarchy to manage or discuss. |
482 </p> | 492 </p> |
483 | 493 |
484 <p> | 494 <p> |
485 It's possible to use these ideas to construct something analogous to | 495 It's possible to use these ideas to construct something analogous to |
486 type-safe Unix pipes. For instance, see how <code>fmt.Fprintf</code> | 496 type-safe Unix pipes. For instance, see how <code>fmt.Fprintf</code> |
487 enables formatted printing to any output, not just a file, or how the | 497 enables formatted printing to any output, not just a file, or how the |
488 <code>bufio</code> package can be completely separate from file I/O, | 498 <code>bufio</code> package can be completely separate from file I/O, |
489 or how the <code>crypto</code> packages stitch together block and | 499 or how the <code>crypto</code> packages stitch together block and |
490 stream ciphers. All these ideas stem from a single interface | 500 stream ciphers. All these ideas stem from a single interface |
491 (<code>io.Writer</code>) representing a single method | 501 (<code>io.Writer</code>) representing a single method |
492 (<code>Write</code>). And that's only scratching the surface. | 502 (<code>Write</code>). And that's only scratching the surface. |
493 </p> | 503 </p> |
494 | 504 |
495 <p> | 505 <p> |
496 It takes some getting used to but this implicit style of type | 506 It takes some getting used to but this implicit style of type |
497 dependency is one of the most exciting things about Go. | 507 dependency is one of the most productive things about Go. |
498 </p> | 508 </p> |
499 | 509 |
500 <h3 id="methods_on_basics"> | 510 <h3 id="methods_on_basics"> |
501 Why is <code>len</code> a function and not a method?</h3> | 511 Why is <code>len</code> a function and not a method?</h3> |
502 <p> | 512 <p> |
503 We debated this issue but decided | 513 We debated this issue but decided |
504 implementing <code>len</code> and friends as functions was fine in practice and | 514 implementing <code>len</code> and friends as functions was fine in practice and |
505 didn't complicate questions about the interface (in the Go type sense) | 515 didn't complicate questions about the interface (in the Go type sense) |
506 of basic types. | 516 of basic types. |
507 </p> | 517 </p> |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 func (b Bar) ImplementsFooer() {} | 591 func (b Bar) ImplementsFooer() {} |
582 func (b Bar) Foo() {} | 592 func (b Bar) Foo() {} |
583 </pre> | 593 </pre> |
584 | 594 |
585 <p> | 595 <p> |
586 Most code doesn't make use of such constraints, since they limit the utility of· | 596 Most code doesn't make use of such constraints, since they limit the utility of· |
587 the interface idea. Sometimes, though, they're necessary to resolve ambiguities | 597 the interface idea. Sometimes, though, they're necessary to resolve ambiguities |
588 among similar interfaces. | 598 among similar interfaces. |
589 </p> | 599 </p> |
590 | 600 |
| 601 <h3 id="t_and_equal_interface"> |
| 602 Why doesn't type T satisfy the Equal interface?</h3> |
| 603 |
| 604 <p> |
| 605 Consider this simple interface to represent an object that can compare |
| 606 itself with another value: |
| 607 </p> |
| 608 |
| 609 <pre> |
| 610 type Equaler interface { |
| 611 Equal(Equaler) bool |
| 612 } |
| 613 </pre> |
| 614 |
| 615 <p> |
| 616 and this type, <code>T</code>: |
| 617 </p> |
| 618 |
| 619 <pre> |
| 620 type T int |
| 621 func (t T) Equal(u T) bool { return t == u } // does not satisfy Equaler |
| 622 </pre> |
| 623 |
| 624 <p> |
| 625 Unlike the analogous situation in some polymorphic type systems, |
| 626 <code>T</code> does not implement <code>Equaler</code>. |
| 627 The argument type of <code>T.Equal</code> is <code>T</code>, |
| 628 not literally the required type <code>Equaler</code>. |
| 629 </p> |
| 630 |
| 631 <p> |
| 632 In Go, the type system does not promote the argument of |
| 633 <code>Equal</code>; that is the programmer's responsibility, as |
| 634 illustrated by the type <code>T2</code>, which does implement |
| 635 <code>Equaler</code>: |
| 636 </p> |
| 637 |
| 638 <pre> |
| 639 type T2 int |
| 640 func (t T2) Equal(u Equaler) bool { return t == u.(T2) } // satisfies Equaler |
| 641 </pre> |
| 642 |
| 643 <p> |
| 644 Even this isn't like other type systems, though, because in Go <em>any</em> |
| 645 type that satisfies <code>Equaler</code> could be passed as the |
| 646 argument to <code>T2.Equal</code>, and at run time we must |
| 647 check that the argument is of type <code>T2</code>. |
| 648 Some languages arrange to make that guarantee at compile time. |
| 649 </p> |
| 650 |
| 651 <p> |
| 652 A related example goes the other way: |
| 653 </p> |
| 654 |
| 655 <pre> |
| 656 type Opener interface { |
| 657 Open(name) Reader |
| 658 } |
| 659 |
| 660 func (t T3) Open() *os.File |
| 661 </pre> |
| 662 |
| 663 <p> |
| 664 In Go, <code>T3</code> does not satisfy <code>Opener</code>, |
| 665 although it might in another language. |
| 666 </p> |
| 667 |
| 668 <p> |
| 669 While it is true that Go's type system does less for the programmer |
| 670 in such cases, the lack of subtyping makes the rules about |
| 671 interface satisfaction very easy to state: are the function's names |
| 672 and signatures exactly those of the interface? |
| 673 Go's rule is also easy to implement efficiently. |
| 674 We feel these benefits offset the lack of |
| 675 automatic type promotion. Should Go one day adopt some form of generic |
| 676 typing, we expect there would be a way to express the idea of these |
| 677 examples and also have them be statically checked. |
| 678 </p> |
| 679 |
591 <h3 id="convert_slice_of_interface"> | 680 <h3 id="convert_slice_of_interface"> |
592 Can I convert a []T to an []interface{}?</h3> | 681 Can I convert a []T to an []interface{}?</h3> |
593 | 682 |
594 <p> | 683 <p> |
595 Not directly because they do not have the same representation in memory. | 684 Not directly because they do not have the same representation in memory. |
596 It is necessary to copy the elements individually to the destination | 685 It is necessary to copy the elements individually to the destination |
597 slice. This example converts a slice of <code>int</code> to a slice of | 686 slice. This example converts a slice of <code>int</code> to a slice of |
598 <code>interface{}</code>: | 687 <code>interface{}</code>: |
599 </p> | 688 </p> |
600 | 689 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 makes a copy of the thing stored in the interface value. If the interface | 818 makes a copy of the thing stored in the interface value. If the interface |
730 value holds a struct, copying the interface value makes a copy of the | 819 value holds a struct, copying the interface value makes a copy of the |
731 struct. If the interface value holds a pointer, copying the interface value | 820 struct. If the interface value holds a pointer, copying the interface value |
732 makes a copy of the pointer, but again not the data it points to.· | 821 makes a copy of the pointer, but again not the data it points to.· |
733 </p> | 822 </p> |
734 | 823 |
735 <h3 id="methods_on_values_or_pointers"> | 824 <h3 id="methods_on_values_or_pointers"> |
736 Should I define methods on values or pointers?</h3> | 825 Should I define methods on values or pointers?</h3> |
737 | 826 |
738 <pre> | 827 <pre> |
739 func (s *MyStruct) someMethod() { } // method on pointer | 828 func (s *MyStruct) pointerMethod() { } // method on pointer |
740 func (s MyStruct) someMethod() { } // method on value | 829 func (s MyStruct) valueMethod() { } // method on value |
741 </pre> | 830 </pre> |
742 | 831 |
743 <p> | 832 <p> |
| 833 For programmers unaccustomed to pointers, the distinction between these |
| 834 two examples can be confusing, but the situation is actually very simple. |
744 When defining a method on a type, the receiver (<code>s</code> in the above | 835 When defining a method on a type, the receiver (<code>s</code> in the above |
745 example) behaves exactly is if it were an argument to the method. Define the | 836 example) behaves exactly as if it were an argument to the method. |
746 method on a pointer type if you need the method to modify the data the receiver | 837 Whether to define the receiver as a value or as a pointer is the same |
747 points to. Otherwise, it is often cleaner to define the method on a value type. | 838 question, then, as whether a function argument should be a value or |
| 839 a pointer. |
| 840 There are several considerations. |
748 </p> | 841 </p> |
749 | 842 |
| 843 <p> |
| 844 First, and most important, does the method need to modify the |
| 845 receiver? |
| 846 If it does, the receiver <em>must</em> be a pointer. |
| 847 (Slices and maps are reference types, so their story is a little |
| 848 more subtle, but for instance to change the length of a slice |
| 849 in a method the receiver must still be a pointer.) |
| 850 In the examples above, if <code>pointerMethod</code> modifies |
| 851 the fields of <code>s</code>, |
| 852 the caller will see those changes, but <code>valueMethod</code> |
| 853 is called with a copy of the caller's argument (that's the definition |
| 854 of passing a value), so changes it makes will be invisible to the caller. |
| 855 </p> |
| 856 |
| 857 <p> |
| 858 By the way, pointer receivers are identical to the situation in Java, |
| 859 although in Java the pointers are hidden under the covers; it's Go's |
| 860 value receivers that are unusual. |
| 861 </p> |
| 862 |
| 863 <p> |
| 864 Second is the consideration of efficiency. If the receiver is large, |
| 865 a big <code>struct</code> for instance, it will be much cheaper to |
| 866 use a pointer receiver. |
| 867 </p> |
| 868 |
| 869 <p> |
| 870 Next is consistency. If some of the methods of the type must have |
| 871 pointer receivers, the rest should too, so the method set is |
| 872 consistent regardless of how the type is used. |
| 873 See the section on <a href="#different_method_sets">method sets</a> |
| 874 for details. |
| 875 </p> |
| 876 |
| 877 <p> |
| 878 For types such as basic types, slices, and small <code>structs</code>, |
| 879 a value receiver is very cheap so unless the semantics of the method |
| 880 requires a pointer, a value receiver is efficient and clear. |
| 881 </p> |
| 882 |
| 883 |
750 <h3 id="new_and_make"> | 884 <h3 id="new_and_make"> |
751 What's the difference between new and make?</h3> | 885 What's the difference between new and make?</h3> |
752 | 886 |
753 <p> | 887 <p> |
754 In short: <code>new</code> allocates memory, <code>make</code> initializes | 888 In short: <code>new</code> allocates memory, <code>make</code> initializes |
755 the slice, map, and channel types. | 889 the slice, map, and channel types. |
756 </p> | 890 </p> |
757 | 891 |
758 <p> | 892 <p> |
759 See the <a href="/doc/effective_go.html#allocation_new">relevant section | 893 See the <a href="/doc/effective_go.html#allocation_new">relevant section |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1104 indicate. | 1238 indicate. |
1105 </p> | 1239 </p> |
1106 | 1240 |
1107 <p> | 1241 <p> |
1108 Still, there is room for improvement. The compilers are good but could be | 1242 Still, there is room for improvement. The compilers are good but could be |
1109 better, many libraries need major performance work, and the garbage collector | 1243 better, many libraries need major performance work, and the garbage collector |
1110 isn't fast enough yet (even if it were, taking care not to generate unnecessary· | 1244 isn't fast enough yet (even if it were, taking care not to generate unnecessary· |
1111 garbage can have a huge effect). | 1245 garbage can have a huge effect). |
1112 </p> | 1246 </p> |
1113 | 1247 |
| 1248 <p> |
| 1249 In any case, Go can often be very competitive. See the blog post about |
| 1250 <a href="http://blog.golang.org/2011/06/profiling-go-programs.html">profiling |
| 1251 Go programs</a> for an informative example. |
| 1252 |
1114 <h2 id="change_from_c">Changes from C</h2> | 1253 <h2 id="change_from_c">Changes from C</h2> |
1115 | 1254 |
1116 <h3 id="different_syntax"> | 1255 <h3 id="different_syntax"> |
1117 Why is the syntax so different from C?</h3> | 1256 Why is the syntax so different from C?</h3> |
1118 <p> | 1257 <p> |
1119 Other than declaration syntax, the differences are not major and stem | 1258 Other than declaration syntax, the differences are not major and stem |
1120 from two desires. First, the syntax should feel light, without too | 1259 from two desires. First, the syntax should feel light, without too |
1121 many mandatory keywords, repetition, or arcana. Second, the language | 1260 many mandatory keywords, repetition, or arcana. Second, the language |
1122 has been designed to be easy to analyze | 1261 has been designed to be easy to analyze |
1123 and can be parsed without a symbol table. This makes it much easier | 1262 and can be parsed without a symbol table. This makes it much easier |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1158 <pre> | 1297 <pre> |
1159 a := uint64(1); | 1298 a := uint64(1); |
1160 </pre> | 1299 </pre> |
1161 <p> | 1300 <p> |
1162 Parsing is also simplified by having a distinct grammar for types that | 1301 Parsing is also simplified by having a distinct grammar for types that |
1163 is not just the expression grammar; keywords such as <code>func</code> | 1302 is not just the expression grammar; keywords such as <code>func</code> |
1164 and <code>chan</code> keep things clear. | 1303 and <code>chan</code> keep things clear. |
1165 </p> | 1304 </p> |
1166 | 1305 |
1167 <p> | 1306 <p> |
1168 See the <a href="http://blog.golang.org/2010/07/gos-declaration-syntax.html">Go'
s Declaration Syntax</a> article for more details. | 1307 See the article about |
| 1308 <a href="http://blog.golang.org/2010/07/gos-declaration-syntax.html">Go's Declar
ation Syntax</a> |
| 1309 for more details. |
1169 </p> | 1310 </p> |
1170 | 1311 |
1171 <h3 id="no_pointer_arithmetic"> | 1312 <h3 id="no_pointer_arithmetic"> |
1172 Why is there no pointer arithmetic?</h3> | 1313 Why is there no pointer arithmetic?</h3> |
1173 <p> | 1314 <p> |
1174 Safety. Without pointer arithmetic it's possible to create a | 1315 Safety. Without pointer arithmetic it's possible to create a |
1175 language that can never derive an illegal address that succeeds | 1316 language that can never derive an illegal address that succeeds |
1176 incorrectly. Compiler and hardware technology have advanced to the | 1317 incorrectly. Compiler and hardware technology have advanced to the |
1177 point where a loop using array indices can be as efficient as a loop | 1318 point where a loop using array indices can be as efficient as a loop |
1178 using pointer arithmetic. Also, the lack of pointer arithmetic can | 1319 using pointer arithmetic. Also, the lack of pointer arithmetic can |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1245 Automatic garbage collection makes concurrent code far easier to write. | 1386 Automatic garbage collection makes concurrent code far easier to write. |
1246 Of course, implementing garbage collection in a concurrent environment is | 1387 Of course, implementing garbage collection in a concurrent environment is |
1247 itself a challenge, but meeting it once rather than in every | 1388 itself a challenge, but meeting it once rather than in every |
1248 program helps everyone. | 1389 program helps everyone. |
1249 </p> | 1390 </p> |
1250 | 1391 |
1251 <p> | 1392 <p> |
1252 Finally, concurrency aside, garbage collection makes interfaces | 1393 Finally, concurrency aside, garbage collection makes interfaces |
1253 simpler because they don't need to specify how memory is managed across them. | 1394 simpler because they don't need to specify how memory is managed across them. |
1254 </p> | 1395 </p> |
| 1396 |
| 1397 <p> |
| 1398 On the topic of performance, keep in mind that Go gives the programmer |
| 1399 considerable control over memory layout and allocation, much more than |
| 1400 is typical in garbage-collected languages. A careful programmer can reduce |
| 1401 the garbage collection overhead dramatically by using the language well; |
| 1402 see the article about |
| 1403 <a href="http://blog.golang.org/2011/06/profiling-go-programs.html">profiling |
| 1404 Go programs</a> for a worked example, including a demonstration of Go's |
| 1405 profiling tools. |
| 1406 </p> |
OLD | NEW |