go-viper
Go言語でViperライブラリを使った設定ファイルの読み込み、構造体への反映、環境変数やフラグとの優先順位付け、Cobraやpflagとの連携など、柔軟で使いやすい設定管理を支援するSkill。
📜 元の英語説明(参考)
Write, debug, and explain Go configuration code using github.com/spf13/viper. Use this skill whenever the user wants Viper-based config loading, config structs, file plus env plus flag precedence, Cobra or pflag integration, unmarshaling config into structs, env key replacers, or a clean application config bootstrap that follows Viper's README behavior.
🇯🇵 日本人クリエイター向け解説
Go言語でViperライブラリを使った設定ファイルの読み込み、構造体への反映、環境変数やフラグとの優先順位付け、Cobraやpflagとの連携など、柔軟で使いやすい設定管理を支援するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o go-viper.zip https://jpskill.com/download/8677.zip && unzip -o go-viper.zip && rm go-viper.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/8677.zip -OutFile "$d\go-viper.zip"; Expand-Archive "$d\go-viper.zip" -DestinationPath $d -Force; ri "$d\go-viper.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
go-viper.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
go-viperフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
[Skill 名] go-viper
このスキルを使用すると、Viperの通常の優先順位に従って、ファイル、フラグ、および環境から構成を読み取る、クリーンなViperセットアップを作成できます。
- 明示的な
Set - フラグ
- 環境変数
- 構成ファイル
- リモート key/value ストア
- デフォルト
コアアプローチ
- パッケージグローバル変数よりも
viper.New()を優先します。構成済みのインスタンスを1つ作成し、必要な場所に渡します。 - 型付きの
Config構造体を定義し、すべてのデフォルト、ファイルパス、envバインディング、およびフラグバインディングが構成された後で、それにアンマーシャルします。 internal/configやpkg/configなどの1つの場所に、Load(...)関数を使用して構成ブートストラップを保持します。- アプリの設計で許可されている場合にのみ、構成ファイルをオプションとして扱います。それ以外の場合は、明確なエラーを返します。
- 重要なキーについてはフラグとenvを明示的にバインドし、一般的なケースでは
AutomaticEnv()と env キーリプレースを使用します。
クリーンなViperブートストラップ
Viperベースのローダーを作成する場合は、次の順序に従ってください。
v := viper.New()でインスタンスを作成します。- 構成ファイル名、必要に応じてタイプ、および検索パスを設定します。
SetDefaultでデフォルトを設定します。SetEnvPrefix、SetEnvKeyReplacer、AutomaticEnv、および特別なキーのBindEnvを使用して、envのロードを構成します。pflagまたは Cobra フラグを定義して解析し、BindPFlagまたはBindPFlagsでバインドします。ReadInConfig()を呼び出します。- ファイルがオプションの場合、
viper.ConfigFileNotFoundErrorを解析エラーとは別に処理します。 - 型付きの構造体に
Unmarshal(&cfg)を呼び出します。 - アプリに必要なフィールドまたは制約がある場合は、結果の構造体を検証します。
実装ルール
- ファイルキーが Go のフィールド名と異なる場合は、構造体フィールドに
mapstructureタグを使用します。 - env変数にマップする必要があるネストされたキーの場合は、
SetEnvKeyReplacerでstrings.NewReplacer(".", "_", "-", "_")を使用します。 - env変数は大文字と小文字が区別され、バインド時にキャッシュされず、アクセス時に読み取られることに注意してください。
- Viper は複雑な値をディープマージしないことに注意してください。後のソースは値全体を置き換えます。
ReadInConfig()の前、およびWatchConfig()の前に、すべての構成パスを追加します。- ユーザーが Cobra を使用する場合は、コマンドのフラグセットからコマンドフラグを直接バインドします。
- ユーザーがテスト可能なコードを必要とする場合は、型付きの構成と構成済みの
*viper.Viperの両方を、呼び出し元が本当に両方を必要とする場合にのみ返します。それ以外の場合は、型付きの構成のみを返します。
出力に関する期待
- 分散したスニペットではなく、小規模で本番環境に適した構成パッケージを提供します。
- ファイル、env、およびフラグが1つの例でどのように連携するかを示します。
- 説明の中で優先順位を明示的にします。
- 生成またはバインドされた正確な env 変数名とフラグ名を記述します。
参考文献
- 推奨されるパッケージの形状、ローダーの順序、検証戦略、テストのガイダンス、および一般的な落とし穴については、
references/clean-config-pattern.mdを参照してください。 - ファイル、env、およびフラグがどのように相互作用するか、またはあるソースが別のソースよりも優先される理由についてタスクが具体的に説明されている場合は、
references/file-env-flag-precedence.mdを参照してください。 - コンパクトなエンドツーエンドのローダーの例については、
examples/clean_setup.goを参照してください。
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Use this skill to produce a clean Viper setup that reads configuration from file, flags, and environment with Viper's normal precedence:
- explicit
Set - flags
- environment variables
- config file
- remote key/value store
- defaults
Core approach
- Prefer
viper.New()over the package global; build one configured instance and pass it where needed. - Define a typed
Configstruct and unmarshal into it after all defaults, file paths, env bindings, and flag bindings are configured. - Keep configuration bootstrap in one place such as
internal/configorpkg/configwith aLoad(...)function. - Treat the config file as optional only when the app design allows it; otherwise return a clear error.
- Bind flags and env explicitly for important keys, and use
AutomaticEnv()plus an env key replacer for the general case.
Clean Viper bootstrap
When writing a Viper-based loader, follow this order:
- Create an instance with
v := viper.New(). - Set config file name, type if needed, and search paths.
- Set defaults with
SetDefault. - Configure env loading with
SetEnvPrefix,SetEnvKeyReplacer,AutomaticEnv, andBindEnvfor special keys. - Define and parse
pflagor Cobra flags, then bind them withBindPFlagorBindPFlags. - Call
ReadInConfig(). - Handle
viper.ConfigFileNotFoundErrorseparately from parse errors when the file is optional. - Call
Unmarshal(&cfg)into a typed struct. - Validate the resulting struct if the app has required fields or constraints.
Implementation rules
- Use
mapstructuretags on struct fields when file keys differ from Go field names. - For nested keys that should map to env vars, use
strings.NewReplacer(".", "_", "-", "_")withSetEnvKeyReplacer. - Remember that env vars are case-sensitive and are read when accessed, not cached at bind time.
- Remember that Viper does not deep-merge complex values; later sources replace the whole value.
- Add all config paths before
ReadInConfig()and beforeWatchConfig(). - If the user uses Cobra, bind command flags directly from the command's flag set.
- If the user wants testable code, return both the typed config and the configured
*viper.Viperonly when the caller truly needs both; otherwise return just the typed config.
Output expectations
- Give a small, production-leaning config package, not scattered snippets.
- Show how file, env, and flags work together in one example.
- Make precedence explicit in the explanation.
- Mention the exact env var names and flag names generated or bound.
References
- Read
references/clean-config-pattern.mdfor the recommended package shape, loader order, validation strategy, testing guidance, and common pitfalls. - Read
references/file-env-flag-precedence.mdwhen the task is specifically about how file, env, and flags interact or why one source wins over another. - Read
examples/clean_setup.gofor a compact end-to-end loader example.