jpskill.com
🛠️ 開発・MCP コミュニティ

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本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
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
🪟 Windows (PowerShell)
$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. 1. 下の青いボタンを押して go-viper.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → go-viper フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

[Skill 名] go-viper

このスキルを使用すると、Viperの通常の優先順位に従って、ファイル、フラグ、および環境から構成を読み取る、クリーンなViperセットアップを作成できます。

  1. 明示的な Set
  2. フラグ
  3. 環境変数
  4. 構成ファイル
  5. リモート key/value ストア
  6. デフォルト

コアアプローチ

  • パッケージグローバル変数よりも viper.New() を優先します。構成済みのインスタンスを1つ作成し、必要な場所に渡します。
  • 型付きの Config 構造体を定義し、すべてのデフォルト、ファイルパス、envバインディング、およびフラグバインディングが構成された後で、それにアンマーシャルします。
  • internal/configpkg/config などの1つの場所に、Load(...) 関数を使用して構成ブートストラップを保持します。
  • アプリの設計で許可されている場合にのみ、構成ファイルをオプションとして扱います。それ以外の場合は、明確なエラーを返します。
  • 重要なキーについてはフラグとenvを明示的にバインドし、一般的なケースでは AutomaticEnv() と env キーリプレースを使用します。

クリーンなViperブートストラップ

Viperベースのローダーを作成する場合は、次の順序に従ってください。

  1. v := viper.New() でインスタンスを作成します。
  2. 構成ファイル名、必要に応じてタイプ、および検索パスを設定します。
  3. SetDefault でデフォルトを設定します。
  4. SetEnvPrefixSetEnvKeyReplacerAutomaticEnv、および特別なキーの BindEnv を使用して、envのロードを構成します。
  5. pflag または Cobra フラグを定義して解析し、BindPFlag または BindPFlags でバインドします。
  6. ReadInConfig() を呼び出します。
  7. ファイルがオプションの場合、viper.ConfigFileNotFoundError を解析エラーとは別に処理します。
  8. 型付きの構造体に Unmarshal(&cfg) を呼び出します。
  9. アプリに必要なフィールドまたは制約がある場合は、結果の構造体を検証します。

実装ルール

  • ファイルキーが Go のフィールド名と異なる場合は、構造体フィールドに mapstructure タグを使用します。
  • env変数にマップする必要があるネストされたキーの場合は、SetEnvKeyReplacerstrings.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:

  1. explicit Set
  2. flags
  3. environment variables
  4. config file
  5. remote key/value store
  6. defaults

Core approach

  • Prefer viper.New() over the package global; build one configured instance and pass it where needed.
  • Define a typed Config struct 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/config or pkg/config with a Load(...) 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:

  1. Create an instance with v := viper.New().
  2. Set config file name, type if needed, and search paths.
  3. Set defaults with SetDefault.
  4. Configure env loading with SetEnvPrefix, SetEnvKeyReplacer, AutomaticEnv, and BindEnv for special keys.
  5. Define and parse pflag or Cobra flags, then bind them with BindPFlag or BindPFlags.
  6. Call ReadInConfig().
  7. Handle viper.ConfigFileNotFoundError separately from parse errors when the file is optional.
  8. Call Unmarshal(&cfg) into a typed struct.
  9. Validate the resulting struct if the app has required fields or constraints.

Implementation rules

  • Use mapstructure tags on struct fields when file keys differ from Go field names.
  • For nested keys that should map to env vars, use strings.NewReplacer(".", "_", "-", "_") with SetEnvKeyReplacer.
  • 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 before WatchConfig().
  • 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.Viper only 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.md for the recommended package shape, loader order, validation strategy, testing guidance, and common pitfalls.
  • Read references/file-env-flag-precedence.md when the task is specifically about how file, env, and flags interact or why one source wins over another.
  • Read examples/clean_setup.go for a compact end-to-end loader example.