go mod init と go.mod&go.sum
go mod init とは
go mod init は Go言語のモジュールシステムを初期化するコマンドです。
go mod init は新しいGoモジュールを作成し、現在のディレクトリに go.mod ファイルを生成するコマンドです。
go.mod ファイルの内容
module course-go: このモジュールの名前を定義go 1.25.3: 使用しているGoのバージョンを指定
なぜ go mod init が必要だったのか
- モジュールシステム: Go 1.11以降、Goは依存関係管理にモ ジュールシステムを使用
- エラーの原因: go.mod ファイルがないと、Goは現在のディレクトリをモジュールとして認識できない
- 標準ライブラリの混同: エラーメッセージ「package main is not in std」は、Goが
mainパッケージを標準ライブラリ内で探そうとしていたことを示している
Go モジュールの利点
- 依存関係管理: 外部パッケージの管理が簡単
- バージョン管理: 特定のバージョンの依存関係を指定可能
- 再現性: どこでも同じ環境でビルド可能
使用例
# 新しいモジュールを作成
go mod init course-go
# 依存関係を追加
go get github.com/gin-gonic/gin
# 不要な依存関係を削除
go mod tidy
これで、あなたのGoプロジェクトは適切なモジュール構造を持ち、正常に動作するようになりました!
go get の動作
go get は外部ライブラリ(モジュール)をダウンロードして、プロジェクトの依存関係として追加するコマンドです。
go get が行ったこと
-
ダウンロード:
- GitHubから
gin-gonic/ginライブラリをダウンロード - 依存する他のライブラリも自動的にダウンロード
- GitHubから
-
go.mod の更新:
requireセクションに依存関係を追加- バージョン情報も記録
-
go.sumファイルの作成:
まとめ
go get はまさに「ライブラリからモジュールをダウンロードする」という意味です!
具体的には:
- リモートリポジトリ(GitHub等)からソースコードをダウンロード
- 依存関係の解決: 必要な他のライブラリも自動的にダウンロード
- go.mod: プロジェクトの依存関係リストに追加
- go.sum: セキュリティ用のチェックサムファイル作成
go.modの内容例
module course-go
go 1.25.3
require (
github.com/bytedance/sonic v1.14.0 // indirect
github.com/bytedance/sonic/loader v0.3.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/gin-gonic/gin v1.11.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.27.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-yaml v1.18.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.54.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
go.uber.org/mock v0.5.0 // indirect
golang.org/x/arch v0.20.0 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/mod v0.25.0 // indirect
golang.org/x/net v0.42.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.27.0 // indirect
golang.org/x/tools v0.34.0 // indirect
google.golang.org/protobuf v1.36.9 // indirect
)
go.sum の役割
go.sum ファイルは Go のセキュリティと整合性を保つために非常に重要なファイルです。
1. セキュリティ保証
各依存関係のチェックサム(ハッシュ値)を記録して、ダウンロードしたコードが改ざんされていないことを確認します。
go.sum の構造解説
各行の意味:
github.com/gin-gonic/gin v1.11.0 h1:abc123...
│ │ │ │
│ │ │ └─ チェックサム(SHA-256ハッシュ)
│ │ └─ ハッシュタイプ(h1 = SHA-256)
│ └─ バージョン
└─ モジュール名
なぜ必要なのか?
1. 改ざん防止
# 例:悪意のあるコードが注入された場合
# チェックサム が一致しないため、Goが検出してエラーを出す
2. 再現可能なビルド
- 同じ go.mod と go.sum があれば、どこでも同じ依存関係でビルド可能
- チームメンバー全員が同じライブラリバージョンを使用
ベストプラクティス
1. バージョン管理に含める
# git add で go.sum も追加する
git add go.mod go.sum
2. チーム開発での重要性
- go.sum をコミットすることで、全メンバーが同じ依存関係を使用
- セキュリティリスクを防止
3. 自動更新
go getやgo mod tidyを実行すると自動的に更新される- 手動で編集する必要はない