Compare commits
1 Commits
main
...
add-no-typ
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97f5d8a91f |
@ -227,31 +227,16 @@ last_modified: 2026-02-22
|
||||
}
|
||||
```
|
||||
|
||||
1. Do not create packages just to hold types (e.g. `types`, `domain`,
|
||||
`models` that contain only type definitions). Define types alongside the
|
||||
implementations that use them. A type-only package is a sign that the types
|
||||
are in the wrong place — move them to the package that actually operates on
|
||||
them. This also avoids awkward import aliases (e.g.
|
||||
`domaintypes "myapp/internal/domain"`) which are a code smell indicating
|
||||
poor package naming.
|
||||
|
||||
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
|
||||
multiple conformant types, put the interface(s) in their own file.
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user