From: Rob Pike Date: Sat, 1 Aug 2009 00:54:00 +0000 (-0700) Subject: more info about comments X-Git-Tag: weekly.2009-11-06~996 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d951ce4e457075fee56a61a8f589ede7647d778b;p=gostls13.git more info about comments R=rsc DELTA=100 (82 added, 4 deleted, 14 changed) OCL=32609 CL=32615 --- diff --git a/doc/effective_go.html b/doc/effective_go.html index 222284069d..dc49ca9688 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -153,6 +153,51 @@ reserving block comments for top-level package comments and commenting out large swaths of code.

+

Write package comments

+ +

+Every package should have a package comment, a block +comment preceding the package clause. +It should introduce the package and +provide information relevant to the package as a whole. +

+ +
+/*
+	The regexp package implements a simple library for
+	regular expressions.
+
+	The syntax of the regular expressions accepted is:
+
+	regexp:
+		concatenation { '|' concatenation }
+	concatenation:
+		{ closure }
+	closure:
+		term [ '*' | '+' | '?' ]
+	term:
+		'^'
+		'$'
+		'.'
+		character
+		'[' [ '^' ] character-ranges ']'
+		'(' regexp ')'
+*/
+package regexp
+
+ +

+Consider how the package comment contributes to the appearance +of the godoc page for the package. Don't just +echo the doc comments for the components. The package comment +can be brief. +

+ +
+// The path package implements utility routines for
+// manipulating slash-separated filename paths.
+
+

Write doc comments

@@ -193,7 +238,6 @@ Use of complete English sentences admits a wider variety of automated presentations.

-

Avoid ASCII Art

@@ -208,14 +252,15 @@ sure the columns are lined up properly in the output.

-If you must use comments to separate +If you need comments to separate sections in a file, use a simple block comment:

 /*
- * Helper routines for simplifying the fetching of optional fields of basic type.
- * If the field is missing, they return the zero for the type.
+ * Helper routines for simplifying the fetching of optional
+ * fields of basic type. If the field is missing, they return
+ * the zero for the type.
  */
 
@@ -223,8 +268,9 @@ or
 /*
-    Helper routines for simplifying the fetching of optional fields of basic type.
-    If the field is missing, they return the zero for the type.
+    Helper routines for simplifying the fetching of optional
+    fields of basic type. If the field is missing, they return
+    the zero for the type.
  */
 
@@ -233,6 +279,39 @@ Comments are text, not HTML; they contain no markup. Refrain from ASCII embellishment like *this* or /this/.

+

Use grouping to organize declarations

+ +

+Go's declaration syntax allows grouping of declarations. +A comment can introduce a group of related constants or variables. +

+ +
+// Flags to Open wrapping those of the underlying system.
+// Not all flags may be implemented on a given system.
+const (
+	O_RDONLY = syscall.O_RDONLY;	// open the file read-only.
+	O_WRONLY = syscall.O_WRONLY;	// open the file write-only.
+	...
+)
+
+ +

+A grouping can also indicate relationships between items, +such as the fact that a set of variables is controlled by +a mutex. +

+ +
+// Variables protected by counterLock.
+var (
+	counterLock	sync.Mutex;
+	inputCount	uint32;
+	outputCount	uint32;
+	errorCount	uint32;
+)
+
+

Names

Use MixedCaps

@@ -328,11 +407,23 @@ Use these expressions to avoid the repetition of filling out a data structure.

+
+// Prepare RPCMessage to send to server
+rpc := &RPCMessage {
+	Version: 1,
+	Header: &RPCHeader {
+		Id: nextId(),
+		Signature: sign(body),
+		Method: method,
+	},
+	Body: body,
+};
+

Use parallel assignment to slice a buffer

-hdr, body, checksum := buf[0:20], buf[20:len(buf)-4], buf[len(buf)-4:len(buf)];
+header, body, checksum := buf[0:20], buf[20:n-4], buf[n-4:n];
 

Control Flow

@@ -390,7 +481,8 @@ func shouldEscape(c byte) bool { go/src/pkg/bytes/bytes.go:
-// Compare returns an integer comparing the two byte arrays lexicographically.
+// Compare returns an integer comparing the two byte arrays
+// lexicographically.
 // The result will be 0 if a==b, -1 if a < b, and +1 if a > b
 func Compare(a, b []byte) int {
     for i := 0; i < len(a) && i < len(b); i++ {