This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_style_guide.txt
68 lines (45 loc) · 3.02 KB
/
go_style_guide.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Please read this guide:
https://golang.org/doc/effective_go.html
We mostly follow it when possible.
All code must conform to the gofmt default style. You can easily run gofmt using the format.sh script. gofmt will find many common syntax errors.
Indentation
We use tabs for indentation and gofmt emits them by default. If you insist on having your editor indent with spaces, be sure to run the code through gofmt before checking it in.
Line length:
Go has no line length limit. Don't worry about overflowing a punched card. If a line feels too long, wrap it and indent with an extra tab.
Parentheses:
Go needs fewer parentheses than C and Java: control structures (if, for, switch) do not have parentheses in their syntax. Also, the operator precedence hierarchy is shorter and clearer, so
Function names:
Public functions: PascalCase
Private functions: camelCase
Variable names:
Globals and properties:
Public: PascalCase
Private: camelCase
Parameters and scoped:
lowercase_with_underscores // Non-standard
Constants:
UPPERCASE_WITH_UNDERSCORES // Non-standard
Comments:
All variables and functions which are not obvious should have doc comments. Doc comments work best as complete sentences, which allow a wide variety of automated presentations. The first sentence should be a one-sentence summary that starts with the name being declared (case sensitive).
If the function of a cluster of statements is not immediately obvious, add a comment describing what it does. If why it needs to be done or why it needs to be done a specific way is not obvious, include that too.
Line comments:
Commented out code lines without a space between the // and the start of the code to distinguish it from a real comment.
Line comments which are actually comments should have a single space between the // and the start of the comment.
Line comments on the same line as code should have a single space between the end of the code and the //.
Spacing:
gofmt will remove groups of more than one blank line. Group together clusters of statements which do the same thing with a blank line. Example: function call, error handling for that function call, logging the result of the function call.
Whitespace:
Trim trailing whitespace.
Logging:
Use the log package instead of fmt. The log package adds timestamps and allows us to easily redirect the logging output.
Log everything useful for debugging or tracing the code path. If a comment is made redundant by a logging statement, omit the comment.
If a logging statement indicates that something is about to happen (but has not happened yet), end the text in an ellipsis.
Changes to this guide:
All changes to this guide must be reflected in the code. If you don't want to update the code, don't change the guide.
Imports:
Never use imports to connect code files which are not in the same directory.
Ordering:
Standard library
3rd party libraries (always use go get)
Your packages
Separate the above mentioned types of imports with a blank line. Always use the import (\n...\n)\n style imports.