Compare commits
1 Commits
8fc81c3774
...
cfa5d4e174
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cfa5d4e174 |
@ -423,35 +423,35 @@ last_modified: 2026-02-22
|
|||||||
[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.
|
||||||
|
|
||||||
1. When defining custom string-based wrapper types (e.g. `type ImageID string`,
|
1. Once you have a working program, run `go mod tidy` to clean up your `go.mod`
|
||||||
`type ContainerID string`), always implement the `fmt.Stringer` interface.
|
and `go.sum` files. Tag a v0.0.1 or v1.0.0. Push your `main` branch and
|
||||||
At SDK and library boundaries, use `.String()` instead of `string(v)` to
|
tag(s). Subsequent work should happen on branches so that `main` is "always
|
||||||
convert the value to a plain string. This makes the conversion explicit,
|
releasable". "Releasable" in this context means that it builds and functions
|
||||||
grep-able, and ensures a consistent pattern across the codebase.
|
as expected, and that all tests and linting passes.
|
||||||
|
|
||||||
|
1. Custom string types (e.g. `type ImageID string`) should implement the
|
||||||
|
`fmt.Stringer` interface by adding a `String() string` method. When passing
|
||||||
|
these types to library code or standard library functions that expect a
|
||||||
|
`string`, use the `.String()` method instead of a `string()` cast. This
|
||||||
|
keeps the code consistent with Go's interface conventions and makes it
|
||||||
|
easier to change the underlying representation later without updating every
|
||||||
|
call site.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type ImageID string
|
type ImageID string
|
||||||
|
|
||||||
func (id ImageID) String() string {
|
// String implements fmt.Stringer.
|
||||||
return string(id)
|
func (id ImageID) String() string { return string(id) }
|
||||||
}
|
|
||||||
|
|
||||||
// At SDK/library boundaries:
|
// Right:
|
||||||
// Good:
|
client.RemoveImage(ctx, imageID.String())
|
||||||
client.PullImage(ctx, imageID.String())
|
|
||||||
|
|
||||||
// Bad:
|
// Wrong:
|
||||||
client.PullImage(ctx, string(imageID))
|
client.RemoveImage(ctx, string(imageID))
|
||||||
```
|
```
|
||||||
|
|
||||||
1. 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. For any internet-facing http server, set appropriate timeouts and limits to
|
1. For any internet-facing http server, set appropriate timeouts and limits to
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user