Compare commits

..

1 Commits

Author SHA1 Message Date
user
cfa5d4e174 style(go): add Stringer rule for custom string types
Custom string types should implement fmt.Stringer and use .String()
instead of string() casts when passing to library code.
2026-02-23 11:56:11 -08:00

View File

@ -229,29 +229,6 @@ last_modified: 2026-02-22
1. Define your struct types near their constructors. 1. Define your struct types near their constructors.
1. Do not create packages whose sole purpose is to hold type definitions.
Packages named `types`, `domain`, or `models` that contain only structs and
interfaces (with no behavior) are a code smell. Define types alongside the
code that uses them. Type-only packages force consuming packages into alias
imports and circular-dependency gymnastics, and indicate that the package
boundaries were drawn around nouns instead of responsibilities. If multiple
packages need the same type, put it in the package that owns the behavior,
or in a small, focused interface package — not in a grab-bag types package.
1. When defining custom string-based types (e.g. `type ImageID string`),
implement `fmt.Stringer`. Use `.String()` at SDK and library boundaries
instead of `string(v)`. This makes type conversions explicit, grep-able,
and consistent across the codebase. Example:
```go
type ContainerID string
func (id ContainerID) String() string { return string(id) }
// At the Docker SDK boundary:
resp, err := c.docker.ContainerStart(ctx, id.String(), opts)
```
1. Define your interface types near the functions that use them, or if you have 1. Define your interface types near the functions that use them, or if you have
multiple conformant types, put the interface(s) in their own file. multiple conformant types, put the interface(s) in their own file.
@ -452,6 +429,29 @@ last_modified: 2026-02-22
releasable". "Releasable" in this context means that it builds and functions releasable". "Releasable" in this context means that it builds and functions
as expected, and that all tests and linting passes. 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:
```go
type ImageID string
// String implements fmt.Stringer.
func (id ImageID) String() string { return string(id) }
// Right:
client.RemoveImage(ctx, imageID.String())
// Wrong:
client.RemoveImage(ctx, string(imageID))
```
# 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