mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
Alphabetize
parent
05b57aad6b
commit
b32469168d
@ -12,8 +12,8 @@ You can view this as a supplement to [Effective Go](https://golang.org/doc/effec
|
|||||||
* [Comment Sentences](#comment-sentences)
|
* [Comment Sentences](#comment-sentences)
|
||||||
* [Contexts](#contexts)
|
* [Contexts](#contexts)
|
||||||
* [Copying](#copying)
|
* [Copying](#copying)
|
||||||
* [Declaring Empty Slices](#declaring-empty-slices)
|
|
||||||
* [Crypto Rand](#crypto-rand)
|
* [Crypto Rand](#crypto-rand)
|
||||||
|
* [Declaring Empty Slices](#declaring-empty-slices)
|
||||||
* [Doc Comments](#doc-comments)
|
* [Doc Comments](#doc-comments)
|
||||||
* [Don't Panic](#dont-panic)
|
* [Don't Panic](#dont-panic)
|
||||||
* [Error Strings](#error-strings)
|
* [Error Strings](#error-strings)
|
||||||
@ -104,28 +104,6 @@ calls to have surprising effects.
|
|||||||
In general, do not copy a value of type `T` if its methods are associated with the
|
In general, do not copy a value of type `T` if its methods are associated with the
|
||||||
pointer type, `*T`.
|
pointer type, `*T`.
|
||||||
|
|
||||||
## Declaring Empty Slices
|
|
||||||
|
|
||||||
When declaring an empty slice, prefer
|
|
||||||
|
|
||||||
```go
|
|
||||||
var t []string
|
|
||||||
```
|
|
||||||
|
|
||||||
over
|
|
||||||
|
|
||||||
```go
|
|
||||||
t := []string{}
|
|
||||||
```
|
|
||||||
|
|
||||||
The former declares a nil slice value, while the latter is non-nil but zero-length. They are functionally equivalent—their `len` and `cap` are both zero—but the nil slice is the preferred style.
|
|
||||||
|
|
||||||
Note that there are limited circumstances where a non-nil but zero-length slice is preferred, such as when encoding JSON objects (a `nil` slice encodes to `null`, while `[]string{}` encodes to the JSON array `[]`).
|
|
||||||
|
|
||||||
When designing interfaces, avoid making a distinction between a nil slice and a non-nil, zero-length slice, as this can lead to subtle programming errors.
|
|
||||||
|
|
||||||
For more discussion about nil in Go see Francesc Campoy's talk [Understanding Nil](https://www.youtube.com/watch?v=ynoY2xz-F8s).
|
|
||||||
|
|
||||||
## Crypto Rand
|
## Crypto Rand
|
||||||
|
|
||||||
Do not use package `math/rand` to generate keys, even throwaway ones.
|
Do not use package `math/rand` to generate keys, even throwaway ones.
|
||||||
@ -153,6 +131,28 @@ func Key() string {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Declaring Empty Slices
|
||||||
|
|
||||||
|
When declaring an empty slice, prefer
|
||||||
|
|
||||||
|
```go
|
||||||
|
var t []string
|
||||||
|
```
|
||||||
|
|
||||||
|
over
|
||||||
|
|
||||||
|
```go
|
||||||
|
t := []string{}
|
||||||
|
```
|
||||||
|
|
||||||
|
The former declares a nil slice value, while the latter is non-nil but zero-length. They are functionally equivalent—their `len` and `cap` are both zero—but the nil slice is the preferred style.
|
||||||
|
|
||||||
|
Note that there are limited circumstances where a non-nil but zero-length slice is preferred, such as when encoding JSON objects (a `nil` slice encodes to `null`, while `[]string{}` encodes to the JSON array `[]`).
|
||||||
|
|
||||||
|
When designing interfaces, avoid making a distinction between a nil slice and a non-nil, zero-length slice, as this can lead to subtle programming errors.
|
||||||
|
|
||||||
|
For more discussion about nil in Go see Francesc Campoy's talk [Understanding Nil](https://www.youtube.com/watch?v=ynoY2xz-F8s).
|
||||||
|
|
||||||
## Doc Comments
|
## Doc Comments
|
||||||
|
|
||||||
All top-level, exported names should have doc comments, as should non-trivial unexported type or function declarations. See https://golang.org/doc/effective_go.html#commentary for more information about commentary conventions.
|
All top-level, exported names should have doc comments, as should non-trivial unexported type or function declarations. See https://golang.org/doc/effective_go.html#commentary for more information about commentary conventions.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user