diff --git a/doc/go_for_cpp_programmers.html b/doc/go_for_cpp_programmers.html index ccd7db56269..b6990c3625a 100644 --- a/doc/go_for_cpp_programmers.html +++ b/doc/go_for_cpp_programmers.html @@ -8,14 +8,15 @@ to nothing about the similarities.

For a more general introduction to Go, see the -Go tutorial. +Go tutorial and +Effective Go.

For a detailed description of the Go language, see the Go spec.

-There is more documentation about go. +There is more documentation about go.

Conceptual Differences

@@ -56,8 +57,8 @@ There is more documentation about go.
  • Go does not use header files. Instead, each source file is part of a defined package. When a package defines an object - (type, constant, variable, function) with a name which starts with an - uppercase letter, than object is visible to any other file which + (type, constant, variable, function) with a name starting with an + upper case letter, that object is visible to any other file which imports that package.
  • Go does not support implicit type conversion. Operations that mix @@ -151,15 +152,16 @@ var v1 = v2;

    -Go permits multiple assignments which are done in parallel. +Go permits multiple assignments, which are done in parallel.

     i, j = j, i;   // Swap i and j.
     

    -Functions may have multiple return values, indicating by a list in -parentheses. +Functions may have multiple return values, indicated by a list in +parentheses. The returned values can be stored by assignment +to a list of variables.

     func f() (i int, j int);
    @@ -195,9 +197,11 @@ statement, or the expressions of a for statement, or the value of a
     around the body of an if or for statement.
     
     
    -if a < b { f() }    // Valid
    -if (a < b) { f() }  // Valid
    -if (a < b) f();     // INVALID
    +if a < b { f() }          // Valid
    +if (a < b) { f() }        // Valid (condition is parenthesized expression)
    +if (a < b) f();           // INVALID
    +for i = 0; i < 10; i++ {}    // Valid
    +for (i = 0; i < 10; i++) {}  // INVALID
     

    @@ -263,7 +267,8 @@ In Go constants may be untyped. This applies even to constants named with a const declaration if no type is given in the declaration and the initializer expression uses only untyped constants. -An untyped constant becomes typed when it is used within a context that +A value derived from an untyped constant becomes typed when it +is used within a context that requires a typed value. This permits constants to be used relatively freely without requiring general implicit type conversion. @@ -309,7 +314,7 @@ Given an array, or another slice, a new slice is created via creates a new slice which refers to a, starts at index I, and ends at index J - 1. It has length J - I. -If a is itself a slice, the new slice refers to the same array +The new slice refers to the same array to which a refers. That is, changes made using the new slice may be seen using a. The @@ -335,6 +340,8 @@ necessary to pass the length of the buffer; it is efficiently accessible via

    The slice syntax may also be used with a string. It returns a new string, whose value is a substring of the original string. +Because strings are immutable, string slices can be implemented +without allocating new storage for the slices's contents.

    Making values

    @@ -342,10 +349,10 @@ whose value is a substring of the original string. Go has a builtin function new which takes a type and allocates space on the heap. The allocated space will be zero-initialized for the type. -For example, new(int) returns a new object of type -*int, -allocated on the heap and initialized with the value 0. -Unlike C++, new is a function, not an operator; +For example, new(int) allocates a new int on the heap, +initializes it with the value 0, +and returns its address, which has type *int. +Unlike in C++, new is a function, not an operator; new int is a syntax error.

    @@ -361,8 +368,8 @@ the fact that map and channel values are passed by reference. Calling make with a map type takes an optional argument which is the expected capacity of the map. Calling make with a channel type takes an optional -argument which is the -buffering capacity of the channel. +argument which sets the +buffering capacity of the channel; the default is 0 (unbuffered).

    The make function may also be used to allocate a slice. @@ -378,7 +385,8 @@ sometime after there are no references to the returned slice.

    Interfaces

    -Where C++ provides classes and templates, Go provides interfaces. A +Where C++ provides classes, subclasses and templates, +Go provides interfaces. A Go interface is similar to a C++ pure abstract class: a class with no data members, with methods which are all pure virtual. However, in Go, any type which provides the methods named in the interface may be @@ -441,7 +449,7 @@ will accept a variable of type *myType.

    -func getAndSet(x myInterface);
    +func getAndSet(x myInterface) {}
     func f1() {
     	var p myType;
     	getAndSet(&p);
    @@ -495,22 +503,23 @@ you want the equivalent of a virtual function, use an interface.
     
     

    A variable which has an interface type may be converted to have a -different interface type. This conversion is implemented dynamically +different interface type using a special construct called a type assertion. +This is implemented dynamically at runtime, like C++ dynamic_cast. Unlike dynamic_cast, there does not need to be any declared relationship between the two interfaces.

    -type myCompareInterface interface {
    +type myPrintInterface interface {
       print();
     }
     func f3(x myInterface) {
    -	x.(myCompareInterface).print()
    +	x.(myPrintInterface).print()  // type assertion to myPrintInterface
     }
     

    -The conversion to myCompareInterface is entirely dynamic. +The conversion to myPrintInterface is entirely dynamic. It will work as long as the underlying type of x (the dynamic type) defines a print method. @@ -525,8 +534,9 @@ type Any interface { }

    -Containers may be written in terms of Any, and the caller may cast -the values back to the desired type. As the typing is dynamic rather +Containers may be written in terms of Any, but the caller +must unbox using a type assertion to recover +values of the contained type. As the typing is dynamic rather than static, there is no equivalent of the way that a C++ template may inline the relevant operations. The operations are fully type-checked at runtime, but all operations will involve a function call. @@ -561,21 +571,22 @@ go server(1); go server(2);

    (Note that the for statement in the server -function is equivalent to a C++ while (true) loop). +function is equivalent to a C++ while (true) loop.)

    Goroutines are (intended to be) cheap.

    -Function literals can be useful with the go statement. +Function literals (which Go implements as closures) +can be useful with the go statement.

    -var g int // global variable
    +var g int;
     go func(i int) {
     	s := 0
     	for j := 0; j < i; j++ { s += j }
    -	g = s
    -} (1000) // Passes argument 1000 to the function literal.
    +	g = s;
    +} (1000); // Passes argument 1000 to the function literal.
     

    Channels

    @@ -627,7 +638,7 @@ func manager2(ch chan cmd2) {

    -To use manager2, given a channel to it: +To use manager2, given a channel to it:

     func f4(ch <- chan cmd2) int {