Go 開発用に Visual Studio Code を構成する

このクイックスタートでは、Go for Visual Studio Code 拡張機能をインストールして構成します。

Go 開発者調査 2020 の結果では、回答者の41% が Go での最も好ましいエディター として Visual Studio Code を選びました。 これは、Visual Studio Code が Go 開発者の間で最も人気のあるエディターであるということです。

Visual Studio Code および Go 拡張機能では、IntelliSense、コード ナビゲーション、高度なデバッグが提供されます。 このクイックスタートでは、Visual Studio Code を構成します。 その後、Go プログラムのサンプルを記述、実行、デバッグします。

A screenshot showing a Go program within Visual Studio Code

1. Go をインストールする

次の手順に従って Go をインストールしてください。

  1. Web ブラウザーで、go.dev/doc/install に移動します。
  2. お使いのオペレーティング システムに応じたバージョンをダウンロードします。
  3. ダウンロードが完了したら、インストーラーを実行します。
  4. コマンド プロンプトを開き、go version を実行して Go がインストールされていることを確認します。

2. Visual Studio Code をインストールする

次の手順に従って、Visual Studio Code をインストールします。

  1. Web ブラウザーで、code.visualstudio.com に移動します。
  2. お使いのオペレーティング システムに応じたバージョンをダウンロードします。Windows、Linux、macOS がサポートされています。
  3. ダウンロードが完了したら、インストーラーを実行します。 これはすぐに完了します。

3. Go 拡張機能のインストール

手順 Screenshot
Visual Studio Code で、アクティビティ バーの [拡張機能] アイコンをクリックして[拡張機能] ビューを表示します。 または、キーボード ショートカット (Ctrl + Shift + X) を使用します。 A screenshot showing how search for the Go extension.
Go 拡張機能を検索し、[Install]\(インストール\) を選択します。 A screenshot showing how to use the search box in the top tool bar to find App Services in Azure.

4. Go ツールを更新する

手順 Screenshot
Visual Studio Code で、コマンド パレット[Help]\(ヘルプ\)>[Show All Commands]\(すべてのコマンドを表示\) を開きます。 または、キーボード ショートカット (Ctrl + Shift + P) を使用します。 A screenshot showing how search the Command Palette.
Go: Install/Update tools を検索し、パレットからコマンドを実行します。 A screenshot showing how to run the Go: install/update tool from the command pallet.
メッセージが表示されたら、使用可能なすべての Go ツールを選択し、[OK] をクリックします。 A screenshot showing how to update all the available Go tools.
Go ツールの更新が完了するまで待ちます。 A screenshot showing all the Go tools that were updated.

5. Go プログラムのサンプルを記述する

手順 Screenshot
Visual Studio Code で、Go アプリケーションのルート ディレクトリを作成するフォルダーを開きます。 フォルダーを開くには、アクティビティ バーのエクスプローラー アイコンをクリックし、[Open Folder]\(フォルダーを開く\) をクリックします。 A screenshot showing how to create a new folder.
エクスプローラー パネルで [New Folder]\(新しいフォルダー\) をクリックし、sample-appという名前の Go の サンプル アプリケーションのルート ディレクターを作成します。 A screenshot showing how to create a folder in vs code.
エクスプローラー パネルで[New File]\(新しいファイル\)をクリックし、ファイルに main.go という名前を付けます。 A screenshot showing how to create a file in vs code.
[Terminal]\(ターミナル\) > [New Terminal]\(新しいターミナル\) からターミナルを開き、コマンド go mod init sample-app を実行して Go のサンプル アプリケーションを初期化します。 A screenshot running the go mod init command.
次のコードを main.go ファイルにコピーします。 A screenshot displaying a sample Go program.

サンプル コード:

package main

import "fmt"

func main() {
    name := "Go Developers"
    fmt.Println("Azure for", name)
}

6. デバッガーを実行する

手順 Screenshot
番号付が付いている行の左側をクリックして、7 行目にブレークポイントを作成します。 または、7 行目にカーソルを置き、F9 キーを押します。 A screenshot showing how to set a breakpoint.
[デバッグ] ビューを表示するには、Visual Studio Code の横にあるアクティビティ バーのデバッグ アイコンをクリックします。 または、キーボード ショートカット (Ctrl + Shift + D) を使用します。 A screenshot showing how to navigate to the debug panel.
[Run and Debug]\(実行とデバッグ\) をクリックするか、F5 キーを押してデバッガーを実行します。 次に、7 行目の name 変数にカーソルを合わせると、その値が表示されます。 デバッガー バーの [Continue]\(続行) をクリックするか、F5 キーを押してデバッガーを終了します。 A screenshot showing running the debugger in VS code.

次のステップ