LoginSignup
1
1

Go 言語の開発環境構築(Mac)

Last updated at Posted at 2024-01-03

Go 開発ツール

Mac に Go 開発ツールをインストールする場合は、Homebrew を使い、下記のコマンドで実行できます。

$ brew install go

下記のコマンドでバージョンが表示されれば、完了です。

$ go version
go version go1.21.5 darwin/amd64

注意点

brew install コマンドを実行してもターミナルに何も表示されない、もしくは下記が表示される場合は、Homebrew の更新に時間がかかっているため、しばらく待つ必要があります。

Updating Homebrew...

また、下記のエラーが出る場合は、Frameworks フォルダに書き込み権限が無いため、ターミナルに表示された sudo chown -R [user name] /usr/local/Frameworks コマンドで、フォルダのオーナーを変更する必要があります。

Error: The following directories are not writable by your user:
/usr/local/Frameworks

You should change the ownership of these directories to your user.
  sudo chown -R [user name] /usr/local/Frameworks

And make sure that your user has write permission.
  chmod u+w /usr/local/Frameworks

VS Code

VS Code を下記からダウンロードしインストール
https://code.visualstudio.com/
※ バージョン: 1.85.1

マーケットプレイス(Code -> 基本設定 -> 拡張機能)で Go(Go Team at Google)を検索し、インストール

注意点

Go 拡張機能をインストールしただけでは、定義へのジャンプ等が行えないので、コマンドパレット(表示 -> コマンドパレット)で「Go:Install/Update Tools」を選択し、gopls をインストールします

依存関係の解消

GitHub のプライベートリポジトリのライブラリに依存している場合は、下記の設定が必要です。

GitHub の認証

パーソナルアクセストークンを使用する場合は、~/.gitconfig に下記を追加

[url "https://<アカウント名>:<アクセストークン>@github.com/"]
        insteadOf = https://github.com/

sshを使用する場合は、~/.gitconfig に下記を追加

[url "git@github.com:"]
        insteadOf = https://github.com/

GOPRIVATE の設定

go get などでライブラリを取得した場合に、チャックサムが行われます。
sum.golang.org のデータと検証が行われ、下記の様なエラーが出ます。

go: github.com/xxx-xx/xx-sample@latest: github.com/xxx-xx/xx-sample@v0.0.0-20240118131021-7a2bd4d910f8: verifying module: github.com/xxx-xx/xx-sample@v0.0.0-20240118131021-7a2bd4d910f8: reading https://sum.golang.org/lookup/github.com/xxx-xx/xx-sample@v0.0.0-20240118131021-7a2bd4d910f8: 404 Not Found

プライベートなライブラリは、環境変数 GOPRIVATE に登録する必要があります。
GOPRIVATE=github.com/<アカウント名>/<リポジトリ名>

export GOPRIVATE=github.com/<アカウント名>/<リポジトリ名>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1