|
1 | 1 | # About |
2 | 2 |
|
3 | | -Go supports two types of [comments][comments]. |
4 | | -Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. |
| 3 | +Comments let you leave notes in code without affecting how it runs. |
| 4 | +A single-line comment starts with `//` anywhere on a line. |
| 5 | +The compiler ignores everything from `//` to the end of that line. |
| 6 | +When a comment comes after code on the same line, it is often called an inline comment. |
| 7 | +A multiline comment starts with `/*` and ends with `*/` anywhere on a line. |
| 8 | +The compiler ignores everything in between, spanning one or more lines. |
5 | 9 |
|
6 | | -## Documentation comments |
| 10 | +```go |
| 11 | +// This is a single-line comment on its own line |
| 12 | + |
| 13 | +x := 1 // This is an single-line comment inline with code |
| 14 | +fmt.Println(x) // prints 1 |
7 | 15 |
|
8 | | -In Go, comments play an important role in documenting code. |
9 | | -They are used by the [godoc][godoc] command, which extracts these comments to create documentation about Go packages (see [pkg.go.dev][go packages] for examples). |
10 | | -A documentation comment should be a complete sentence that starts with the name of the thing being described and ends with a period. |
| 16 | +/* |
| 17 | +This is a valid |
| 18 | +multiline comment. |
| 19 | +*/ |
11 | 20 |
|
12 | | -Documentation comments are about `what` the thing does and contains, whereas other types of comments such as code comments are about `why` something is done. |
13 | | -The `why` helps others and future you to understand the reason behind a particular piece of code. |
14 | | -However, if the `why` feels obvious, the code comment is not necessary. |
15 | | -A [good rule of thumb][less comments] and more sustainable solution is to write code that is easier to understand so that explanatory comments are hopefully not needed. |
| 21 | +/* This is also a valid multi-line comment. */ |
16 | 22 |
|
17 | | -## Comments for exported identifiers |
| 23 | +``` |
18 | 24 |
|
19 | | -Documentation comments should precede packages as well as [exported identifier][exported identifiers]s, for example exported functions, methods, package variables, constants, and structs, which you will learn more about in further concepts. |
20 | | -Comments written for packages and exported identifiers are useful for the users who import and use these packages. |
| 25 | +Comments serve different purposes depending on where they appear. |
21 | 26 |
|
22 | | -Note, however, that identifiers (such as variables) that are declared inside of functions and methods are private and do not necessarily require comments for the users of the packages. |
| 27 | +## Code comments |
23 | 28 |
|
24 | | -A package-level variable can look like this: |
| 29 | +Code comments explain why something is done, not what. |
| 30 | +Use them when the reason behind a decision isn't obvious from the code: |
25 | 31 |
|
26 | 32 | ```go |
27 | | -// TemperatureFahrenheit represents a certain temperature in degrees Fahrenheit. |
28 | | -var TemperatureFahrenheit float64 |
| 33 | +results = results[1:] // skipping the header row |
29 | 34 | ``` |
30 | 35 |
|
31 | | -Note that `TemperatureFahrenheit` is capitalized, which makes this identifier exported, and thus usable in any code which imports this package. |
| 36 | +In most cases, code that is clearly written is self-explanatory, and comments are not needed. |
| 37 | +Occasionally, it is necessary for the code to do something complex or unexpected; that type of code should be commented. |
| 38 | + |
| 39 | +## Doc comments |
32 | 40 |
|
33 | | -## Package comments |
| 41 | +Doc comments are used to document a package, variable or function. |
| 42 | +This documentation is intended for other coders that would like to know how to _use_ your code. |
| 43 | +It should explain, at a high level, what the code does and how to use it. |
| 44 | +It should not include details of how the code itself works. |
| 45 | +Doc comments sit directly before a declaration, with no blank line separating the comment from the declaration. |
| 46 | +[`godoc`][godoc] parses them to generate package documentation, as seen on [pkg.go.dev][go packages]. |
| 47 | +A doc comment should be a complete sentence starting with the name of the identifier and ending with a period. |
34 | 48 |
|
35 | | -Package comments should be written directly before a package clause (`package x`) and begin with `Package x ...` like this: |
| 49 | +Doc comments on exported identifiers (those that begin with an uppercase letter) appear in generated documentation. |
| 50 | +Doc comments on unexported identifiers (those that don't begin with an uppercase letter) won't appear, but they can still provide useful context within the code. |
| 51 | + |
| 52 | +Comments documenting packages begin with the package name and describe what the package does: |
36 | 53 |
|
37 | 54 | ```go |
38 | 55 | // Package kelvin provides tools to convert temperatures to and from Kelvin. |
39 | 56 | package kelvin |
40 | 57 | ``` |
41 | 58 |
|
42 | | -## Function comments |
43 | | - |
44 | | -A function comment should be written directly before the function declaration. |
45 | | -It should be a full sentence that starts with the function name. |
46 | | -For example, an exported comment for the function `Calculate` should take the form `Calculate ...`. |
47 | | -It should also explain what arguments the function takes, what it does with them, and what its return values mean, ending in a period): |
| 59 | +Comments documenting functions begin with the function name and describe what the function does, including its arguments and return values: |
48 | 60 |
|
49 | 61 | ```go |
50 | 62 | // CelsiusFreezingTemp returns an integer value equal to the temperature at which water freezes in degrees Celsius. |
51 | 63 | func CelsiusFreezingTemp() int { |
52 | | - return 0 |
| 64 | + return 0 |
53 | 65 | } |
54 | 66 | ``` |
55 | 67 |
|
56 | | -## Golint |
| 68 | +Comments documenting variables begin with the variable name and describe what the variable represents: |
57 | 69 |
|
58 | | -[Golint][golint] is a great tool to check for missing comments and other common stylistic issues, which you can read more about at [Effective Go][effective go]. |
59 | | - |
60 | | -You can install `golint` on your machine with the following command: |
61 | | - |
62 | | -```sh |
63 | | -go get -u golang.org/x/lint/golint |
64 | | -``` |
65 | | - |
66 | | -It's a good idea to configure your editor to run `golint` for you, otherwise you can invoke it like this: |
67 | | - |
68 | | -```sh |
69 | | -golint weather.go |
| 70 | +```go |
| 71 | +// TemperatureFahrenheit represents a certain temperature in degrees Fahrenheit. |
| 72 | +var TemperatureFahrenheit float64 |
70 | 73 | ``` |
71 | 74 |
|
72 | | -To use the `golint` command globally, make sure that it is in your `$PATH`. |
| 75 | +For the full doc comment specification, see the [Go documentation guide][doc-comments]. |
73 | 76 |
|
74 | | -[godoc]: https://golang.org/cmd/go/#hdr-Show_documentation_for_package_or_symbol |
| 77 | +[godoc]: https://go.dev/blog/godoc |
75 | 78 | [go packages]: https://pkg.go.dev/ |
76 | | -[less comments]: https://dave.cheney.net/practical-go/presentations/qcon-china.html#_dont_comment_bad_code_rewrite_it |
77 | | -[exported identifiers]: https://www.ardanlabs.com/blog/2014/03/exportedunexported-identifiers-in-go.html |
78 | | -[golint]: https://github.com/golang/lint |
79 | | -[effective go]: https://golang.org/doc/effective_go.html |
80 | | -[comments]: https://golang.org/ref/spec#Comments |
| 79 | +[doc-comments]: https://go.dev/doc/comment |
0 commit comments