Skill站外收录 @skilllib/skill-2014279388051746818
golang-style
遵循清晰的代码结构与错误处理规范,确保 Go 代码具备良好的可读性与维护性。通过上下文包装错误、定义哨兵错误并合理使用注释,提升程序健壮性。避免使用预声明标识符命名变量,控制行长度,并在必要时查阅文档以保证正确使用标准库。
测试与安全 AI 技能
Skills.MD
# Go Coding Conventions
Follow these conventions strictly when writing Go code.
## Happy Path Coding
Structure code so the successful path flows straight down. Handle errors
immediately, then continue with main logic.
```go
// Correct: happy path flows down.
func ProcessUser(id string) (*User, error) {
user, err := db.GetUser(id)
if err != nil {
return nil, fmt.Errorf("get user %s: %w", id, err)
}
if err := user.Validate(); err != nil {
return nil, fmt.Errorf("validate user %s: %w", id, err)
}
return user, nil
}
// Wrong: main logic nested inside conditions.
func ProcessUser(id string) (*User, error) {
user, err := db.GetUser(id)
if err == nil {
if err := user.Validate(); err == nil {
return user, nil
} else {
return nil, err
}
}
return nil, err
}
```
## Error Wrapping
Always wrap errors with context using `%w`. Include the operation and relevant
identifiers.
```go
// Correct: wrapped with context.
if err != nil {
return fmt.Errorf("create order for customer %s: %w", customerID, err)
}
// Wrong: no context.
if err != nil {
return err
}
```
## Sentinel Errors
Define package-level sentinel errors for expected error conditions. Use
`errors.Is()` to check.
```go
// Define at package level.
var (
ErrNotFound = errors.New("not found")
ErrUnauthorized = errors.New("unauthorized")
ErrInvalidInput = errors.New("invalid input")
)
// Return sentinel errors.
func GetUser(id string) (*User, error) {
user := db.Find(id)
if user == nil {
return nil, ErrNotFound
}
return user, nil
}
// Check with errors.Is().
user, err := GetUser(id)
if errors.Is(err, ErrNotFound) {
// Handle not found case.
}
```
## Comments
All comments end with a period.
```go
// ProcessOrder handles order creation and validation.
func ProcessOrder(o *Order) error {
// Validate the order before processing.
if err := o.Validate(); err != nil {
return err
}
// Continue with order processing.
return nil
}
```
## Naming Conventions
Never use Go's predeclared identifiers as variable or parameter names. These
include built-in functions and constants that can be shadowed but should not be.
```go
// Wrong: shadows built-in identifiers.
func process(new string, len int, make bool) error {
copy := "data"
return nil
}
// Correct: use descriptive names instead.
func process(name string, length int, shouldCreate bool) error {
dataCopy := "data"
return nil
}
```
Predeclared identifiers to avoid:
- Functions: `new`, `make`, `len`, `cap`, `append`, `copy`, `delete`, `close`,
`panic`, `recover`, `print`, `println`, `complex`, `real`, `imag`, `clear`,
`min`, `max`
- Constants: `true`, `false`, `iota`, `nil`
- Types: `error`, `bool`, `string`, `int`, `int8`, `int16`, `int32`, `int64`,
`uint`, `uint8`, `uint16`, `uint32`, `uint64`, `uintptr`, `float32`,
`float64`, `complex64`, `complex128`, `byte`, `rune`, `any`, `comparable`
## Line Length
Maximum line length is 120 characters. Break long lines at logical points.
```go
// Correct: break at logical points.
func ProcessOrderWithValidation(
ctx context.Context,
order *Order,
validator OrderValidator,
) (*Result, error) {
return nil, fmt.Errorf(
"process order %s for customer %s: %w",
order.ID,
order.CustomerID,
err,
)
}
```
## Documentation Lookup
Use `go doc` to look up standard library and package documentation:
```bash
go doc fmt.Errorf
go doc errors.Is
go doc context
```
## Einride
If the project is under the Einride organization, always use the Makefiles in
the project which are generated by Sage (the `.sage` folder).
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「golang-style」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次