Skip to content

Commit 976b50a

Browse files
authored
Update comments concept docs (#3054)
1 parent 9a5d7f5 commit 976b50a

File tree

3 files changed

+99
-51
lines changed

3 files changed

+99
-51
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
2-
"blurb": "Comments are human-readable annotations on the code that make programs easier to understand by other programmers.",
3-
"authors": ["nikimanoledaki"],
4-
"contributors": []
2+
"blurb": "Go uses comments to annotate code and generate package documentation.",
3+
"authors": [
4+
"nikimanoledaki"
5+
],
6+
"contributors": [
7+
"BNAndras"
8+
]
59
}

concepts/comments/about.md

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,79 @@
11
# About
22

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.
59

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
715

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+
*/
1120

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. */
1622

17-
## Comments for exported identifiers
23+
```
1824

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.
2126

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
2328

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:
2531

2632
```go
27-
// TemperatureFahrenheit represents a certain temperature in degrees Fahrenheit.
28-
var TemperatureFahrenheit float64
33+
results = results[1:] // skipping the header row
2934
```
3035

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
3240

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.
3448

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:
3653

3754
```go
3855
// Package kelvin provides tools to convert temperatures to and from Kelvin.
3956
package kelvin
4057
```
4158

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:
4860

4961
```go
5062
// CelsiusFreezingTemp returns an integer value equal to the temperature at which water freezes in degrees Celsius.
5163
func CelsiusFreezingTemp() int {
52-
return 0
64+
return 0
5365
}
5466
```
5567

56-
## Golint
68+
Comments documenting variables begin with the variable name and describe what the variable represents:
5769

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
7073
```
7174

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].
7376

74-
[godoc]: https://golang.org/cmd/go/#hdr-Show_documentation_for_package_or_symbol
77+
[godoc]: https://go.dev/blog/godoc
7578
[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

concepts/comments/introduction.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
11
# Introduction
22

3-
In the previous concept, we saw that there are two ways to write comments in Go: single-line comments that are preceded by `//`, and multiline comment blocks that are wrapped with `/*` and `*/`.
3+
Comments let you leave notes in code without affecting how it runs.
4+
Single-line comments start with `//`, and multiline comments are wrapped in `/*` and `*/`:
5+
6+
```go
7+
// This is a single-line comment.
8+
9+
/*
10+
This is a
11+
multiline comment.
12+
*/
13+
```
14+
15+
Comments serve different purposes depending on where they appear.
16+
17+
## Code comments
18+
19+
Code comments explain why something is done, not what.
20+
Use them when the reason behind a decision isn't obvious from the code:
21+
22+
```go
23+
results = results[1:] // skipping the header row
24+
```
25+
26+
## Doc comments
27+
28+
Doc comments sit directly before a declaration, with no blank line separating the comment from the declaration.
29+
[`godoc`][godoc] parses them to generate package documentation.
30+
A doc comment should be a complete sentence starting with the name of the identifier and ending with a period.
31+
32+
Package comments begin with the package name:
33+
34+
```go
35+
// Package kelvin provides tools to convert temperatures to and from Kelvin.
36+
package kelvin
37+
```
38+
39+
Function comments begin with the function name:
40+
41+
```go
42+
// CelsiusFreezingTemp returns an integer value equal to the temperature at which water freezes in degrees Celsius.
43+
func CelsiusFreezingTemp() int {
44+
return 0
45+
}
46+
```
47+
48+
[godoc]: https://go.dev/blog/godoc

0 commit comments

Comments
 (0)