add some stuff
This commit is contained in:
parent
d11270ff9d
commit
e7548045be
66
README.md
66
README.md
@ -227,7 +227,8 @@
|
|||||||
|
|
||||||
- `fmt.Stringer` is an interface for types that can convert
|
- `fmt.Stringer` is an interface for types that can convert
|
||||||
themselves to a string. Any type that implements the `String()
|
themselves to a string. Any type that implements the `String()
|
||||||
string` method satisfies this interface.
|
|
||||||
|
string` method satisfies this interface.
|
||||||
|
|
||||||
9. **`error`** instead of custom error types:
|
9. **`error`** instead of custom error types:
|
||||||
|
|
||||||
@ -305,19 +306,72 @@ string` method satisfies this interface.
|
|||||||
42. Use `iota` to define enumerations in a type-safe way. This ensures that
|
42. Use `iota` to define enumerations in a type-safe way. This ensures that
|
||||||
the constants are properly grouped and reduces the risk of errors.
|
the constants are properly grouped and reduces the risk of errors.
|
||||||
|
|
||||||
43. Don't hardcode big lists in your code. Either isolate lists in their own
|
Example:
|
||||||
module/package, or use a third party library. For example, if you need a
|
|
||||||
list of country codes, you can use
|
```go
|
||||||
|
|
||||||
|
type HandScore int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ScoreHighCard = HandScore(iota * 100_000_000_000)
|
||||||
|
ScorePair
|
||||||
|
ScoreTwoPair
|
||||||
|
ScoreThreeOfAKind
|
||||||
|
ScoreStraight
|
||||||
|
ScoreFlush
|
||||||
|
ScoreFullHouse
|
||||||
|
ScoreFourOfAKind
|
||||||
|
ScoreStraightFlush
|
||||||
|
ScoreRoyalFlush
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Example 2:
|
||||||
|
|
||||||
|
```go
|
||||||
|
type ByteSize float64
|
||||||
|
|
||||||
|
const (
|
||||||
|
_ = iota // ignore first value by assigning to blank identifier
|
||||||
|
KB ByteSize = 1 << (10 * iota)
|
||||||
|
MB
|
||||||
|
GB
|
||||||
|
TB
|
||||||
|
PB
|
||||||
|
EB
|
||||||
|
ZB
|
||||||
|
YB
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
43. Don't hardcode big lists of things in your normal code. Either isolate
|
||||||
|
lists in their own module/package and write some getters, or use a third
|
||||||
|
party library. For example, if you need a list of country codes, you can
|
||||||
|
use
|
||||||
[https://github.com/emvi/iso-639-1](https://github.com/emvi/iso-639-1).
|
[https://github.com/emvi/iso-639-1](https://github.com/emvi/iso-639-1).
|
||||||
|
It's okay to embed a data file (use `go embed`) in your binary if you
|
||||||
|
need to, but make sure you parse it once as a singleton and don't read
|
||||||
|
it from disk every time you need it. Don't use too much memory for
|
||||||
|
this, embedding anything more than perhaps 25MiB (uncompressed) is
|
||||||
|
probably too much. Compress the file before embedding and uncompress
|
||||||
|
during the reading/parsing step for efficiency.
|
||||||
|
|
||||||
44. When storing numeric values that represent a number of units, either
|
44. When storing numeric values that represent a number of units, either
|
||||||
include the unit in the name, or use a type alias, or use a 3p library
|
include the unit in the variable name (e.g. `uptimeSeconds`,
|
||||||
such as
|
`delayMsec`, `coreTemperatureCelsius`), or use a type alias (that
|
||||||
|
includes the unit name), or use a 3p library such as
|
||||||
[github.com/alecthomas/units](https://github.com/alecthomas/units) for
|
[github.com/alecthomas/units](https://github.com/alecthomas/units) for
|
||||||
SI/IEC byte units, or
|
SI/IEC byte units, or
|
||||||
[github.com/bcicen/go-units](https://github.com/bcicen/go-units) for
|
[github.com/bcicen/go-units](https://github.com/bcicen/go-units) for
|
||||||
temperatures (and others). The type system is your friend, use it.
|
temperatures (and others). The type system is your friend, use it.
|
||||||
|
|
||||||
|
45. Once you have a working program, run `go mod tidy` to clean up your
|
||||||
|
`go.mod` and `go.sum` files. Tag a v0.0.1 or v1.0.0. Push your `main`
|
||||||
|
branch and tag(s). Subsequent work should happen on branches so that
|
||||||
|
`main` is "always releasable". "Releasable" in this context means that
|
||||||
|
it builds and functions as expected, and that all tests and linting
|
||||||
|
passes.
|
||||||
|
|
||||||
## Other Golang Tips and Best Practices (Optional)
|
## Other Golang Tips and Best Practices (Optional)
|
||||||
|
|
||||||
1. When passing channels to goroutines, use read-only (`<-chan`) or
|
1. When passing channels to goroutines, use read-only (`<-chan`) or
|
||||||
|
Loading…
Reference in New Issue
Block a user