go-best-practiceslisted
Install: claude install-skill aiskillstore/marketplace
# Go Best Practices
## Type-First Development
Types define the contract before implementation. Follow this workflow:
1. **Define data structures** - structs and interfaces first
2. **Define function signatures** - parameters, return types, and error conditions
3. **Implement to satisfy types** - let the compiler guide completeness
4. **Validate at boundaries** - check inputs where data enters the system
### Make Illegal States Unrepresentable
Use Go's type system to prevent invalid states at compile time.
**Structs for domain models:**
```go
// Define the data model first
type User struct {
ID UserID
Email string
Name string
CreatedAt time.Time
}
type CreateUserRequest struct {
Email string
Name string
}
// Functions follow from the types
func CreateUser(req CreateUserRequest) (*User, error) {
// implementation
}
```
**Custom types for domain primitives:**
```go
// Distinct types prevent mixing up IDs
type UserID string
type OrderID string
func GetUser(id UserID) (*User, error) {
// Compiler prevents passing OrderID here
}
func NewUserID(raw string) UserID {
return UserID(raw)
}
// Methods attach behavior to the type
func (id UserID) String() string {
return string(id)
}
```
**Interfaces for behavior contracts:**
```go
// Define what you need, not what you have
type Reader interface {
Read(p []byte) (n int, err error)
}
type UserRepository interface {
GetByID(ctx context.Context, id UserID) (*User,