# 【最新版】Goの環境構築とLintの活用

> Goの環境構築からLintツールの設定、GitHub Actionsとの統合などを解説。

- 公開日: 2025-12-25
- 著者: Matsu
- タグ: GO
- URL: https://tech.anycloud.co.jp/articles/go-environment-construction

---

## **Goをインストール**

<div class="link-card-wrap"><a class="link-card" href="https://go.dev/dl/" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">All releases - The Go Programming Language</span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-01-favicon.webp" alt=""><span class="link-card-domain">go.dev</span></span></span><img class="link-card-image" src="./linkcard-01-image.webp" alt=""></a></div>

<div class="link-card-wrap"><a class="link-card" href="https://formulae.brew.sh/formula/go" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">go</span><span class="link-card-description">Homebrew’s package index</span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-02-favicon.ico" alt=""><span class="link-card-domain">formulae.brew.sh</span></span></span><img class="link-card-image" src="./linkcard-02-image.webp" alt=""></a></div>

brew経由でGoをインストールします。

```shell
% brew install go

....

go 1.21.6 is already installed but outdated (so it will be upgraded).
==> Fetching downloads for: go
✔︎ Bottle Manifest go (1.25.4)                                                                                             [Downloaded    7.5KB/  7.5KB]
✔︎ Bottle go (1.25.4)                                                                                                      [Downloaded   57.5MB/ 57.5MB]
==> Upgrading go
  1.21.6 -> 1.25.4 
==> Pouring go--1.25.4.arm64_sequoia.bottle.tar.gz
🍺  /opt/homebrew/Cellar/go/1.25.4: 14,439 files, 203.1MB
==> Running `brew cleanup go`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
Removing: /opt/homebrew/Cellar/go/1.21.5... (12,543 files, 253.0MB)
Removing: /opt/homebrew/Cellar/go/1.21.6... (12,545 files, 253.0MB)
```

### Goをインストールできているか確認

Goのバージョンを確認できたらインストールOKです。

```shell
% go version
go version go1.25.4 darwin/arm64
```

## go modについて

次に、`go mod init`を使ってモジュールを初期化する必要があります。

このコマンドは、モジュールのディレクトリに新しい`go.mod`ファイルを作成し、モジュールの依存関係やバージョン情報をトラッキングします。

```go
% go mod init hello
```

例えば、`hello`パッケージを`example.com/hello`モジュールとして公開した場合には、`https://example.com/hello`からダウンロードして、`hello`パッケージとしてインポートするイメージです。

つまり、モジュールは`https://module-name`となる場所からダウンロードされることが前提なので、モジュール名にはGitHubのリポジトリを指定する場合が多いようです。

今回は、特別公開などしないので、適当にhelloとしています。

## VSCode・Cursorで使うGoの拡張機能

[https://marketplace.visualstudio.com/items?itemName=golang.Go](https://marketplace.visualstudio.com/items?itemName=golang.Go)

Goの拡張機能をCursorに入れておきます。

これを入れておくことでコードジャンプや構文のサポートなどを受けることができます。

## **Golangci-lintを使ったリンター設定の紹介**

今回は、あくまで紹介・導入方法くらいになりますが、知っておくと良さそうだったのでまとめました。

<div class="link-card-wrap"><a class="link-card" href="https://golangci-lint.run/" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">Golangci-lint</span><span class="link-card-description"> Golangci-lint is a fast linters runner for Go. </span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-03-favicon.svg" alt=""><span class="link-card-domain">golangci-lint.run</span></span></span></a></div>

### brew経由でGolangci-lintをインストール

brew経由でgolangciをインストールして、バージョン確認ができていればインストールOKです。

```shell
% brew install golangci-lint

% golangci-lint --version
golangci-lint has version 2.6.2 built with go1.25.4 from dc16cf4 on 2025-11-14T02:47:46Z
```

### エディターの設定ファイルに反映

Cursorの`settings.json`で以下のように拡張機能とlintの設定を記載してください。

```json
{
  "editor.formatOnSave": true,
  "[go]": {
    "editor.defaultFormatter": "golang.go"
  },
  "go.useLanguageServer": true,
  "gopls": {
    "formatting.gofumpt": true
  },
  "go.lintTool": "golangci-lint",
  "go.lintFlags": ["--config=${workspaceFolder}/.golangci.yaml", "--fast"]
}
```

### yamlファイルでlintルールの作成

`.golangci.yaml`を作成してlintルールなどを作ります。

今回はざっくり全て有効にしてみます。

```yaml
version: 2

linters:
  enable-all: true
```

### 有効化されているlintの確認方法

ちなみに、どのリンターが有効になっているかは、`golangci-lint help linters`で確認できました。

-   `Enabled by default linters`：現在有効になっているリンターが出力
-   `Disabled by default linters`：無効なリンターが出力

```shell
% golangci-lint help linters
```

こんな感じで有効と無効がわかります。

<figure><img src="./image-001.webp" alt="golangci-lintで有効化されているlintを確認" width="2218" height="872"></figure>

各lintの意味と説明に関しては、以下のドキュメントに全て記載されているのでチェックしてみてください。

<div class="link-card-wrap"><a class="link-card" href="https://golangci-lint.run/docs/linters/" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">Linters</span><span class="link-card-description"> Golangci-lint is a fast linters runner for Go. </span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-04-favicon.svg" alt=""><span class="link-card-domain">golangci-lint.run</span></span></span></a></div>

他にも、`golangci-lint formatters`などもあります。

### lintの実行

それでは、`golangci-lint run`を使用してlintを実行してみましょう。（`-v`オプションで詳細な情報が出力可能）

出力結果にエラーがなく、issue0になっていれば問題ないです。

```shell
% golangci-lint run -v
```

### 【実務向け】Github ActionsにGolangci-lintを組み込む

<div class="link-card-wrap"><a class="link-card" href="https://github.com/golangci/golangci-lint-action" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">GitHub - golangci/golangci-lint-action: Official GitHub Action for golangci-lint from its authors</span><span class="link-card-description">Official GitHub Action for golangci-lint from its authors - golangci/golangci-lint-action</span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-05-favicon.svg" alt=""><span class="link-card-domain">github.com</span></span></span><img class="link-card-image" src="./linkcard-05-image.webp" alt=""></a></div>

`golangci/golangci-lint-action`を導入することでGithub上で自動チェックしてくれます。

細かい設定は省略しますが、必要に応じて導入した方が良さそうですね。

## Hello World!してみる

`hello.go`というファイルを作成して以下コードを記述します。

```go
package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}
```

`go run`を実行してprintされていればOKです。

```shell
% go run .
Hello, World!
```

## まとめ

この記事では、Goの開発環境構築とlintツールの活用方法について解説しました。

golangci-lintを導入することで、コード品質を保ちながら効率的な開発が可能になります。

利用可能なlinterを確認してプロジェクトに適したものを選択できるのも良いポイントですね。

また、Github Actionsと組み合わせることで、自動的なコード品質チェックを実現できます。

Goでの開発を始める際の参考になれば幸いです。
