Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a9e4fbcdc |
@@ -519,9 +519,9 @@ exported name, and reads them as one name: `http.Server`, `list.New`.
|
|||||||
1. Spell a word out unless its short form is the one everybody already uses
|
1. Spell a word out unless its short form is the one everybody already uses
|
||||||
(`fmt`, `strconv`), because any other abbreviation has to be decoded.
|
(`fmt`, `strconv`), because any other abbreviation has to be decoded.
|
||||||
|
|
||||||
1. Never give a package a name that callers want for a variable (`buf`) or that
|
1. Lengthen a package name that callers want for a variable or that a widely
|
||||||
a widely used standard package has (`log`), because importers must rename
|
used standard package has (`bufio`, not `buf`; `auditlog`, not `log`),
|
||||||
one of the two.
|
because importers must otherwise rename one of the two.
|
||||||
|
|
||||||
1. Give a package's directory the package's name, package `main` excepted, so
|
1. Give a package's directory the package's name, package `main` excepted, so
|
||||||
that the import path ends in the name the code uses.
|
that the import path ends in the name the code uses.
|
||||||
@@ -554,16 +554,19 @@ exported name, and reads them as one name: `http.Server`, `list.New`.
|
|||||||
difference between two functions (`ParseInt`, `ParseInt64`), because Go
|
difference between two functions (`ParseInt`, `ParseInt64`), because Go
|
||||||
needs a different name for each.
|
needs a different name for each.
|
||||||
|
|
||||||
1. Use a noun for a function that returns something and a verb for one that
|
1. Use a noun for a function that only reports a value (`Owner()`,
|
||||||
does something (`JobName(key)`, `WriteDetail(w)`), so that a call reads as
|
`JobName(key)`), so that a call reads as the value it gives.
|
||||||
what it gives or does.
|
|
||||||
|
1. Use a verb for a function that does work, even when it also returns a result
|
||||||
|
(`Parse`, `WriteDetail(w)`), so that a call reads as the work it does.
|
||||||
|
|
||||||
1. Never start a getter with `Get` (`Owner()`, not `GetOwner()`; the setter is
|
1. Never start a getter with `Get` (`Owner()`, not `GetOwner()`; the setter is
|
||||||
`SetOwner()`), because the capital letter already tells the method from the
|
`SetOwner()`), because the capital letter already tells the method from the
|
||||||
field.
|
field.
|
||||||
|
|
||||||
1. Begin the name of a database or network call with a verb that says so
|
1. Begin a function's name with `Fetch` or `Load` when it returns a value from
|
||||||
(`Fetch`, `Load`), because the reader must know it can be slow or fail.
|
a database or the network (`FetchOwner`, never `GetOwner` or `Owner`),
|
||||||
|
because the reader must know the call can be slow or fail.
|
||||||
|
|
||||||
1. Name a one-method interface for its method plus `er` (`Reader`,
|
1. Name a one-method interface for its method plus `er` (`Reader`,
|
||||||
`Formatter`), because the name then says the one thing a value of it does.
|
`Formatter`), because the name then says the one thing a value of it does.
|
||||||
@@ -572,9 +575,9 @@ exported name, and reads them as one name: `http.Server`, `list.New`.
|
|||||||
thing (`String()`, never `ToString()`), because readers and the standard
|
thing (`String()`, never `ToString()`), because readers and the standard
|
||||||
interfaces expect them.
|
interfaces expect them.
|
||||||
|
|
||||||
1. Never reuse a well-known method name (`Read`, `Write`, `Close`, `String`)
|
1. Give another name to a method that differs in meaning or signature from a
|
||||||
for a method with another meaning or signature, because readers rely on each
|
well-known method of the same name (`Read`, `Write`, `Close`, `String`),
|
||||||
name meaning one thing.
|
because readers rely on each name meaning one thing.
|
||||||
|
|
||||||
1. Write an initialism in one case throughout (`ServeHTTP`, `appID`, `URL` or
|
1. Write an initialism in one case throughout (`ServeHTTP`, `appID`, `URL` or
|
||||||
`url`; never `Url`, `Id` or `Http`), because that is how the standard
|
`url`; never `Url`, `Id` or `Http`), because that is how the standard
|
||||||
@@ -594,9 +597,11 @@ exported name, and reads them as one name: `http.Server`, `list.New`.
|
|||||||
in a short loop), because a reader far from the declaration has only the
|
in a short loop), because a reader far from the declaration has only the
|
||||||
name.
|
name.
|
||||||
|
|
||||||
1. Name a receiver with one or two letters, the same in every method of its
|
1. Name a receiver with one or two letters, never `this`, `self` or `me`,
|
||||||
type, never `this`, `self` or `me`, because its role is plain from the
|
because its role is plain from the method.
|
||||||
method.
|
|
||||||
|
1. Use the same receiver name in every method of a type, so that the name
|
||||||
|
always means the receiver.
|
||||||
|
|
||||||
# Other Golang Tips and Best Practices (Optional)
|
# Other Golang Tips and Best Practices (Optional)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user