开源项目 GitHub Trending 2026-08-27T12:01 正文 ok 881 words

JetBrains 发布 go-modern-guidelines,帮助 AI 编程 Agent 编写现代 Go 代码

英文标题:🔥 JetBrains/go-modern-guidelines

JetBrains 推出 go-modern-guidelines,为 AI 编程 Agent 提供现代 Go 编码指南,覆盖 Go 1.0 至 Go 1.27 的实用语言和标准库特性。该指南可根据项目 `go.mod` 识别 Go 版本,并让 Agent 优先采用与该版本兼容的现代习惯用法,例如 `max`、`slices.Contains` 与 `cmp.Or`。项目旨在解决模型训练数据滞后和旧模式高频偏置导致的过时代码生成问题,并与 Go 团队的 `modernize` 分析器方向一致。指南现已支持 Junie、Claude Code、Codex、Cursor,并可通过 skills.sh 用于其他 Agent。
打开原文相关 6 · 机会 3
media1.3 KB media57.0 KB

中文

JetBrains 官方项目

该仓库包含面向代码 Agent 的指南,帮助它们编写现代 Go 代码。

例如,使用这些指南的 Agent 会使用 max(a, b) 替代 if-else 代码块,使用 slices.Contains 替代手动循环,使用 cmp.Or(a, b, c) 替代一连串 nil 检查。它还了解近期新增的功能,例如通过 new(42) 获取值的指针,以及使用 errors.AsType[T](err) 进行类型安全的错误匹配——两者均来自 Go 1.26。

这些指南覆盖了从 Go 1.0 到 Go 1.27 最实用的功能,包括 modernize 分析器所针对的全部内容。Agent 将会:

  • 从 go.mod 检测项目的 Go 版本
  • 使用该版本及此前版本可用的语言特性和标准库新增功能
  • 优先采用现代惯用写法,而非旧有模式

所有编程 Agent 都倾向于生成过时的 Go 代码,原因有二:

  1. 训练数据滞后。 模型不了解其训练截止日期后新增的功能。如果它们从未见过 errors.AsType[T](Go 1.26),就无法使用这一功能。

  2. 频率偏置。 即使模型知道某项功能,也常会选择较旧的模式。训练数据中的 for i := 0; i < n; i++ 比 for i := range n 更多,因此模型往往会输出前者。

这些指南通过为 Agent 提供明确的参考资料,解决了这两个问题。

这与 Go 团队的方向一致。modernize 分析器旨在自动更新现有代码,使其采用较新的惯用写法(参见 Go 团队的这场演讲)。这些指南则为新代码服务于相同目标:让 Agent 从一开始就编写现代 Go 代码,从而减少后续需要修复的内容。

各市场集成会运行一个小型 CLI,该工具会在首次使用时通过 go install 安装。因此,必须安装 Go 工具链,并确保其可通过你的 PATH 访问。

CLI 会安装到本地缓存中(例如 ~/.cache/go-modern-guidelines),且绝不会修改你的项目。它面向 Go 1.25 或更新版本;在较旧的 Go 版本上,只要启用了自动工具链切换功能(默认设置为 GOTOOLCHAIN=auto),它仍可运行。这使 Go 能够在首次运行时获取兼容的工具链。

这些指南可用于 Junie、Claude Code、Codex 和 Cursor,也可通过 skills.sh 用于其他 Agent。

在 Junie CLI 会话中运行以下命令。

  1. 将此仓库添加为市场:

/extensions marketplace add JetBrains/go-modern-guidelines

  1. 安装扩展:

/extensions install modern-go-guidelines

当其与 Go 任务相关时,Junie 会自动调用该技能。

在 Junie CLI 会话中更新已安装的扩展:

/extensions update modern-go-guidelines

在 Claude Code 会话中运行以下命令。

  1. 将此仓库添加为市场:

/plugin marketplace add JetBrains/go-modern-guidelines

  1. 安装插件:

/plugin install modern-go-guidelines@goland-claude-marketplace

当其与 Go 任务相关时,Claude Code 会自动调用该技能。

如需显式调用:

/modern-go-guidelines:use-modern-go

Claude Code 可在启动时自动更新市场和已安装的插件。第三方市场默认禁用自动更新,因此需执行一次以下操作:

  1. 运行 /plugin。
  2. 打开 Marketplaces,并选择 goland-claude-marketplace。
  3. 选择 Enable auto-update。

当 Claude Code 报告插件已更新时,将新版本应用到当前会话:

如需手动更新,请在终端中运行以下命令:

claude plugin marketplace update goland-claude-marketplace claude plugin update modern-go-guidelines@goland-claude-marketplace

在终端中运行以下命令。

  1. 将此仓库添加为市场:

codex plugin marketplace add JetBrains/go-modern-guidelines

  1. 安装插件:

codex plugin add modern-go-guidelines@goland-codex-marketplace

刷新市场并重新安装插件,使 Codex 替换其缓存副本:

codex plugin marketplace upgrade goland-codex-marketplace codex plugin remove modern-go-guidelines@goland-codex-marketplace codex plugin add modern-go-guidelines@goland-codex-marketplace

为方便使用,这些指南以 Cursor 插件形式发布。

  1. 在终端中运行以下命令,将此仓库添加为市场:

cursor-agent plugin marketplace add https://github.com/JetBrains/go-modern-guidelines

  1. 在 Cursor 会话中使用 /plugins 命令安装插件。

从 Git 刷新市场,然后重新打开 Cursor,以便其获取新版本插件:

cursor-agent plugin marketplace update goland-cursor-marketplace

如果已安装的插件仍是旧版本,请使用 /plugins 命令重新安装。Cursor 目前未提供用于更新已安装插件的非交互式 CLI 命令。

同一个技能包也适用于 OpenCode 等其他 Agent。可使用以下命令安装:

npx skills add JetBrains/go-modern-guidelines

(--skill use-modern-go 仅安装此技能。)

使用以下命令更新安装在项目中的技能:

npx skills update use-modern-go -p -y

对于全局安装的技能,请将 -p 替换为 -g。

如需在 Agent 中试用对 CLI 的修改,请将当前检出的代码构建到该工具的缓存中:

然后在 Agent 运行所使用的环境中设置 GO_MODERN_GUIDELINES_DEV=1。设置后,任何使用该插件的 Agent 都会运行你的本地构建版本,而非已发布版本;Claude Code、Codex 和 Cursor 的行为一致。请在启动 Agent 前导出该变量,以便 Agent 进程继承它:

export GO_MODERN_GUIDELINES_DEV=1

编辑 CLI 后,再次运行 make dev-install 以重新构建;下一次调用时便会使用新版本。若要恢复为已发布版本,请取消设置该变量(或运行 make dev-uninstall 以移除构建版本):

这需要 Go 工具链。开发构建会存储在工具的缓存目录中($XDG_CACHE_HOME/go-modern-guidelines 或 ~/.cache/go-modern-guidelines)。

构建由 scripts/dev-install.sh 驱动;该脚本刻意与面向 Agent 的包装器分离,因此 Agent 永远无法触发构建。没有 make 时(例如在 Windows 上),可以直接运行它:

sh scripts/dev-install.sh install # 或:uninstall pwsh scripts/dev-install.ps1 install # PowerShell 等效命令

English / 原文

official JetBrains project

This repository contains guidelines for code agents that help them write modern Go code.

For example, an agent with these guidelines uses max(a, b) instead of an if-else block, slices.Contains instead of a manual loop, cmp.Or(a, b, c) instead of a chain of nil checks. It also knows about recent additions like new(42) to get a pointer to a value and errors.AsType[T](err) for type-safe error matching—both from Go 1.26.

The guidelines cover the most useful features from Go 1.0 through Go 1.27, including everything targeted by the modernize analyzer. An agent will:

  • Detect the project's Go version from go.mod
  • Use language features and stdlib additions available up to and including that version
  • Prefer modern idioms over older patterns

All coding agents tend to generate outdated Go. Two reasons:

  1. Training data lag. Models don't know about features added after their training cutoff. They can't use errors.AsType[T] (Go 1.26) if they've never seen it.

  2. Frequency bias. Even for features the model knows, it often picks older patterns. There's more for i := 0; i < n; i++ in the training data than for i := range n, so that's what comes out.

These guidelines fix both problems by giving the agent an explicit reference.

This aligns with the Go team's direction. The modernize analyzer exists to automatically update existing code to use newer idioms (see this talk from the Go team). These guidelines serve the same goal for new code: agents write modern Go from the start, so there's less to fix later.

The marketplace integrations run a small CLI that is installed on first use with go install. Because of that, the Go toolchain must be installed and available on your PATH.

The CLI is installed into a local cache (for example ~/.cache/go-modern-guidelines) and never modifies your project. It targets Go 1.25 or newer ; on an older Go it still works as long as automatic toolchain switching is enabled (GOTOOLCHAIN=auto, the default), which lets Go fetch a compatible toolchain on first run.

The guidelines are available for Junie, Claude Code, Codex, and Cursor, and for other agents via skills.sh.

Run the following commands inside a Junie CLI session.

  1. Add this repository as a marketplace:
/extensions marketplace add JetBrains/go-modern-guidelines
  1. Install the extension:
/extensions install modern-go-guidelines

Junie invokes the skill automatically when it is relevant to a Go task.

Update the installed extension from inside a Junie CLI session:

/extensions update modern-go-guidelines

Run the following commands inside a Claude Code session.

  1. Add this repository as a marketplace:
/plugin marketplace add JetBrains/go-modern-guidelines
  1. Install the plugin:
/plugin install modern-go-guidelines@goland-claude-marketplace

Claude Code invokes the skill automatically when it is relevant to a Go task.

To invoke it explicitly:

/modern-go-guidelines:use-modern-go

Claude Code can update the marketplace and installed plugin automatically at startup. Automatic updates are disabled by default for third-party marketplaces, so enable them once:

  1. Run /plugin.
  2. Open Marketplaces and select goland-claude-marketplace.
  3. Select Enable auto-update.

When Claude Code reports that the plugin was updated, apply the new version to the current session with:

To update it manually instead, run these commands in a terminal:

claude plugin marketplace update goland-claude-marketplace
claude plugin update modern-go-guidelines@goland-claude-marketplace

Run the following commands in a terminal.

  1. Add this repository as a marketplace:
codex plugin marketplace add JetBrains/go-modern-guidelines
  1. Install the plugin:
codex plugin add modern-go-guidelines@goland-codex-marketplace

Refresh the marketplace and reinstall the plugin so Codex replaces its cached copy:

codex plugin marketplace upgrade goland-codex-marketplace
codex plugin remove modern-go-guidelines@goland-codex-marketplace
codex plugin add modern-go-guidelines@goland-codex-marketplace

For convenience, the guidelines are distributed as a Cursor plugin.

  1. Add this repository as a marketplace by running the following command in a terminal:
cursor-agent plugin marketplace add https://github.com/JetBrains/go-modern-guidelines
  1. Install the plugin with the /plugins command inside a Cursor session.

Refresh the marketplace from Git and reopen Cursor so it can pick up the new plugin version:

cursor-agent plugin marketplace update goland-cursor-marketplace

If the installed plugin is still on the previous version, reinstall it with the /plugins command. Cursor does not currently provide a non-interactive CLI command for updating an installed plugin.

The same skill package works across other agents such as OpenCode. Install it with:

npx skills add JetBrains/go-modern-guidelines

(--skill use-modern-go installs only this skill.)

Update the project-installed skill with:

npx skills update use-modern-go -p -y

For a globally installed skill, replace -p with -g.

To try changes to the CLI in your agent, build this checkout into the tool's cache:

Then set GO_MODERN_GUIDELINES_DEV=1 in the environment your agent runs in. With it set, any agent using the plugin runs your local build instead of the released version, the same way across Claude Code, Codex, and Cursor. Export it before launching the agent so the agent process inherits it:

export GO_MODERN_GUIDELINES_DEV=1

After editing the CLI, run make dev-install again to rebuild; the next call picks it up. To go back to the released version, unset the variable (or run make dev-uninstall to remove the build):

This requires the Go toolchain. The dev build is stored in the tool's cache directory ($XDG_CACHE_HOME/go-modern-guidelines or ~/.cache/go-modern-guidelines).

The build is driven by scripts/dev-install.sh, which is intentionally separate from the agent-facing wrapper so an agent can never trigger a build. Without make (for example on Windows) you can run it directly:

sh scripts/dev-install.sh install       # or: uninstall
pwsh scripts/dev-install.ps1 install    # PowerShell equivalent