重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
npx skills add https://github.com/fredrikaverpil/dotfiles --skill golang-style请尽量遵循以下箴言:
Go 箴言 简洁、诗意、精辟 不要通过共享内存来通信,而应通过通信来共享内存。 并发不是并行。 通道用于编排;互斥锁用于序列化。 接口越大,抽象越弱。 让零值变得有用。 interface{} 什么也没说。 Gofmt 的风格不是任何人的最爱,但 gofmt 是每个人的最爱。 少量复制优于少量依赖。 Syscall 必须始终用构建标签保护。 Cgo 必须始终用构建标签保护。 Cgo 不是 Go。 使用 unsafe 包无法提供任何保证。 清晰胜于巧妙。 反射永远不清晰。 错误就是值。 不要只是检查错误,要优雅地处理它们。 设计架构,命名组件,记录细节。 文档是为用户准备的。 不要 panic。
编写 Go 代码时请严格遵守这些规范。
构建代码,使成功路径直线向下。立即处理错误,然后继续主逻辑。
// 正确:愉快路径直线向下。
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
}
// 错误:主逻辑嵌套在条件语句中。
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
}
始终使用 %w 为错误添加上下文。包含操作和相关的标识符。
// 正确:用上下文包装。
if err != nil {
return fmt.Errorf("create order for customer %s: %w", customerID, err)
}
// 错误:没有上下文。
if err != nil {
return err
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
为预期的错误情况定义包级别的哨兵错误。使用 errors.Is() 进行检查。
// 在包级别定义。
var (
ErrNotFound = errors.New("not found")
ErrUnauthorized = errors.New("unauthorized")
ErrInvalidInput = errors.New("invalid input")
)
// 返回哨兵错误。
func GetUser(id string) (*User, error) {
user := db.Find(id)
if user == nil {
return nil, ErrNotFound
}
return user, nil
}
// 使用 errors.Is() 检查。
user, err := GetUser(id)
if errors.Is(err, ErrNotFound) {
// 处理未找到的情况。
}
所有注释都以句号结尾。
// ProcessOrder 处理订单创建和验证。
func ProcessOrder(o *Order) error {
// 在处理前验证订单。
if err := o.Validate(); err != nil {
return err
}
// 继续处理订单。
return nil
}
切勿使用 Go 的预声明标识符作为变量或参数名称。这些包括可以被遮蔽但不应该被遮蔽的内置函数和常量。
// 错误:遮蔽了内置标识符。
func process(new string, len int, make bool) error {
copy := "data"
return nil
}
// 正确:使用描述性名称。
func process(name string, length int, shouldCreate bool) error {
dataCopy := "data"
return nil
}
需要避免的预声明标识符:
new, make, len, cap, append, copy, delete, close, panic, recover, print, println, complex, real, imag, clear, min, maxtrue, false, iota, nilerror, bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, , , , , , , , , , 最大行长度为 120 个字符。在逻辑点处断行。
// 正确:在逻辑点处断行。
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,
)
}
使用 go doc 查阅标准库和包的文档:
go doc fmt.Errorf
go doc errors.Is
go doc context
如果项目属于 Einride 组织,请始终使用项目中由 Sage(.sage 文件夹)生成的 Makefiles。
每周安装次数
57
代码仓库
GitHub 星标数
213
首次出现
2026年1月21日
安全审计
安装于
opencode52
gemini-cli49
codex49
claude-code48
github-copilot46
cursor45
Try to follow the proverbs:
Go Proverbs Simple, Poetic, Pithy Don't communicate by sharing memory, share memory by communicating. Concurrency is not parallelism. Channels orchestrate; mutexes serialize. The bigger the interface, the weaker the abstraction. Make the zero value useful. interface{} says nothing. Gofmt's style is no one's favorite, yet gofmt is everyone's favorite. A little copying is better than a little dependency. Syscall must always be guarded with build tags. Cgo must always be guarded with build tags. Cgo is not Go. With the unsafe package there are no guarantees. Clear is better than clever. Reflection is never clear. Errors are values. Don't just check errors, handle them gracefully. Design the architecture, name the components, document the details. Documentation is for users. Don't panic.
Follow these conventions strictly when writing Go code.
Structure code so the successful path flows straight down. Handle errors immediately, then continue with main logic.
// 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
}
Always wrap errors with context using %w. Include the operation and relevant identifiers.
// 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
}
Define package-level sentinel errors for expected error conditions. Use errors.Is() to check.
// 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.
}
All comments end with a period.
// 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
}
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.
// 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:
new, make, len, cap, append, copy, delete, close, panic, recover, print, println, , , , , , Maximum line length is 120 characters. Break long lines at logical points.
// 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,
)
}
Use go doc to look up standard library and package documentation:
go doc fmt.Errorf
go doc errors.Is
go doc context
If the project is under the Einride organization, always use the Makefiles in the project which are generated by Sage (the .sage folder).
Weekly Installs
57
Repository
GitHub Stars
213
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode52
gemini-cli49
codex49
claude-code48
github-copilot46
cursor45
Android 整洁架构指南:模块化设计、依赖注入与数据层实现
1,600 周安装
uint64uintptrfloat32float64complex64complex128byteruneanycomparablecomplexrealimagclearminmaxtrue, false, iota, nilerror, bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, byte, rune, any, comparable