diff --git a/doc/go_for_cpp_programmers.html b/doc/go_for_cpp_programmers.html index 9920e016b09..6786b7cca6b 100644 --- a/doc/go_for_cpp_programmers.html +++ b/doc/go_for_cpp_programmers.html @@ -81,14 +81,14 @@ easily from left to right.
 Go                           C++
-var v1 int;               // int v1;
-var v2 string;            // const std::string v2;  (approximately)
-var v3 [10]int;           // int v3[10];
-var v4 []int;             // int* v4;  (approximately)
-var v5 struct { f int };  // struct { int f; } v5;
-var v6 *int;              // int* v6;  (but no pointer arithmetic)
-var v7 map[string]int;    // unordered_map<string, int>* v7;  (approximately)
-var v8 func(a int) int;   // int (*v8)(int a);
+var v1 int                // int v1;
+var v2 string             // const std::string v2;  (approximately)
+var v3 [10]int            // int v3[10];
+var v4 []int              // int* v4;  (approximately)
+var v5 struct { f int }   // struct { int f; } v5;
+var v6 *int               // int* v6;  (but no pointer arithmetic)
+var v7 map[string]int     // unordered_map<string, int>* v7;  (approximately)
+var v8 func(a int) int    // int (*v8)(int a);
 

@@ -106,7 +106,7 @@ parentheses.

 var (
-    i int;
+    i int
     m float
 )
 
@@ -117,7 +117,7 @@ or not provide a name for any parameter; you can't omit some names and provide others. You may group several names with the same type:
-func f(i, j, k int, s, t string);
+func f(i, j, k int, s, t string)
 

@@ -127,7 +127,7 @@ not specified, the type of the variable is the type of the initialization expression.

-var v = *p;
+var v = *p
 

@@ -142,21 +142,21 @@ Within a function, a short declaration syntax is available with := .

-v1 := v2;
+v1 := v2
 

This is equivalent to

-var v1 = v2;
+var v1 = v2
 

Go permits multiple assignments, which are done in parallel.

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

@@ -165,19 +165,49 @@ parentheses. The returned values can be stored by assignment to a list of variables.

-func f() (i int, j int);
-v1, v2 = f();
+func f() (i int, j int) { ... }
+v1, v2 = f()
 

-Go treats semicolons as separators, not terminators. Moreover, -semicolons may be omitted after the closing parenthesis of a declaration -block or after a closing brace that is not part of an expression -(e.g., var s struct {} or { x++ }). -Semicolons are never required at the -top level of a file (between global declarations). However, they are -always permitted at -the end of a statement, so you can continue using them as in C++. +Go code uses very few semicolons in practice. Technically, all Go +statements are terminated by a semicolon. However, Go treats the end +of a non-blank line as a semicolon unless the line is clearly +incomplete (the exact rules are +in the language specification). +A consequence of this is that in some cases Go does not permit you to +use a line break. For example, you may not write +

+func g()
+{                  // INVALID
+}
+
+A semicolon will be inserted after g(), causing it to be +a function declaration rather than a function definition. Similarly, +you may not write +
+if x {
+}
+else {             // INVALID
+}
+
+A semicolon will be inserted after the } preceding +the else, causing a syntax error. + +

+Since semicolons do end statements, you may continue using them as in +C++. However, that is not the recommended style. Idiomatic Go code +omits unnecessary semicolons, which in practice is all of them other +than the initial loop clause and cases where you want several +short statements on a single line. + +

+While we're on the topic, we recommend that rather than worry about +semicolons and brace placement, you format your code with +the gofmt program. That will produce a single standard +Go style, and let you worry about your code rather than your +formatting. While the style may initially seem odd, it is as good as +any other style, and familiarity will lead to comfort.

When using a pointer to a struct, you use . instead @@ -187,8 +217,8 @@ are used in the same way.

 type myStruct struct { i int }
-var v9 myStruct;             // v9 has structure type
-var p9 *myStruct;            // p9 is a pointer to a structure
+var v9 myStruct              // v9 has structure type
+var p9 *myStruct             // p9 is a pointer to a structure
 f(v9.i, p9.i)
 
@@ -199,11 +229,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 (condition is a parenthesized expression)
-if (a < b) f();           // INVALID
-for i = 0; i < 10; i++ {}    // Valid
-for (i = 0; i < 10; i++) {}  // INVALID
+if a < b { f() }             // Valid
+if (a < b) { f() }           // Valid (condition is a parenthesized expression)
+if (a < b) f()               // INVALID
+for i = 0; i < 10; i++ {}    // Valid
+for (i = 0; i < 10; i++) {}  // INVALID
 

@@ -272,8 +302,8 @@ The defer statement may be used to call a function after the function containing the defer statement returns.

-fd := open("filename");
-defer close(fd);        // fd will be closed when this function returns.
+fd := open("filename")
+defer close(fd)         // fd will be closed when this function returns.
 

Constants

@@ -289,7 +319,7 @@ requires a typed value. This permits constants to be used relatively freely without requiring general implicit type conversion.
-var a uint;
+var a uint
 f(a + 1)  // untyped numeric constant "1" becomes typed as uint
 
@@ -299,7 +329,7 @@ numeric constant or constant expression. A limit is only applied when a constant is used where a type is required.
-const huge = 1 << 100;
+const huge = 1 << 100
 f(huge >> 98)
 
@@ -312,8 +342,8 @@ it reuses the preceding expression.
 const (
-    red = iota;  // red == 0
-    blue;        // blue == 1
+    red = iota   // red == 0
+    blue         // blue == 1
     green        // green == 2
 )
 
@@ -344,7 +374,7 @@ capacity of the new slice is simply the capacity of a minus I. The capacity of an array is the length of the array. You may also assign an array pointer to a variable of slice type; given var s []int; var a[10] int, -the assignment s = &a is equivalent to +the assignment s = &a is equivalent to s = a[0:len(a)].

@@ -452,8 +482,8 @@ Given this interface:

 type myInterface interface {
-	get() int;
-	set(i int);
+	get() int
+	set(i int)
 }
 
@@ -472,8 +502,8 @@ variable of type *myType.
 func getAndSet(x myInterface) {}
 func f1() {
-	var p myType;
-	getAndSet(&p);
+	var p myType
+	getAndSet(&p)
 }
 
@@ -499,7 +529,7 @@ This effectively implements myChildType as a child of
 func f2() {
-	var p myChildType;
+	var p myChildType
 	getAndSet(&p)
 }
 
@@ -531,7 +561,7 @@ not need to be any declared relationship between the two interfaces.
 type myPrintInterface interface {
-  print();
+  print()
 }
 func f3(x myInterface) {
 	x.(myPrintInterface).print()  // type assertion to myPrintInterface
@@ -563,10 +593,10 @@ at runtime, but all operations will involve a function call.
 
 
 type iterator interface {
-	get() Any;
-	set(v Any);
-	increment();
-	equal(arg *iterator) bool;
+	get() Any
+	set(v Any)
+	increment()
+	equal(arg *iterator) bool
 }
 
@@ -587,12 +617,12 @@ about these details.
 func server(i int) {
     for {
-        print(i);
+        print(i)
         sys.sleep(10)
     }
 }
-go server(1);
-go server(2);
+go server(1)
+go server(2)
 

@@ -607,12 +637,12 @@ Function literals (which Go implements as closures) can be useful with the go statement.

-var g int;
+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

@@ -635,10 +665,10 @@ single value.
 type cmd struct { get bool; val int }
 func manager(ch chan cmd) {
-	var val int = 0;
+	var val int = 0
 	for {
 		c := <- ch
-		if c.get { c.val = val; ch <- c }
+		if c.get { c.val = val ch <- c }
 		else { val = c.val }
 	}
 }
@@ -653,9 +683,9 @@ instead.
 A solution is to pass in a channel.
 
 
-type cmd2 struct { get bool; val int; ch <- chan int; }
+type cmd2 struct { get bool; val int; ch <- chan int }
 func manager2(ch chan cmd2) {
-	var val int = 0;
+	var val int = 0
 	for {
 		c := <- ch
 		if c.get { c.ch <- val }
@@ -669,9 +699,9 @@ To use manager2, given a channel to it:
 
 
 func f4(ch <- chan cmd2) int {
-	myCh := make(chan int);
-	c := cmd2{ true, 0, myCh };  // Composite literal syntax.
-	ch <- c;
-	return <-myCh;
+	myCh := make(chan int)
+	c := cmd2{ true, 0, myCh }   // Composite literal syntax.
+	ch <- c
+	return <-myCh
 }