はじめに
Visual Studio Code を使った Go 環境が快適だと聞いたので、自分の Mac Pro にインストールし、デバッグ環境構築までの備忘録です。中身は、公式サイトに書かれていることを順にやっているだけです。
環境
Mac のバージョン
Go のバージョン
$ go version
$ go version go1.8 darwin/amd64
環境構築
自動補完の設定
Code -> 基本設定 -> 設定 で setting.json を開くか、⌘, で setting.json を開きます。以下の設定を追加して保存します。
setting.json
{
"go.autocompleteUnimportedPackages": true
}
デバッグ環境の設定
Homebrew を使っているのであれば、brew install go-delve/delve/delve
でデバッグ環境が作れるようですが、僕の環境では、MacPorts を使っていたので、こちらの Manual install の手順で環境を作ります。
toolchain のインストール
xcode-select --install
自己署名証明書の作成
- キーチェーンを起動して、 キーチェーンアクセス->証明書のアシスタント->証明書を作成... を選択
- 名前を好きなもので設定(例えば、dlv-cert)
- 証明書のタイプは、 コード署名 を選択
- デフォルトを無効化にチェック
- 生成 をクリックし、そのあと表示される確認ダイアログで 続ける をクリック
- 有効期間を 3650 に設定して、 続ける をクリック
- 続ける をクリック(これを証明書の場所を指定画面まで続けます)
- キーチェーンで システム を選択し、 作成 をクリック
- キーチェーンで作成した自己署名証明書の情報ダイアログを表示
- 信頼を展開して、この証明書を使用するときを 常に信頼 に変更
devel のインストール
$ mkdir $GOPATH/src/github.com/derekparker
$ cd $GOPATH/src/github.com/derekparker
$ git clone https://github.com/derekparker/delve.git
$ cd delve
$ CERT=dlv-cert make install
確認
$ dlv --help
Delve is a source level debugger for Go programs.
Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register state and more.
The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.
Pass flags to the program you are debugging using `--`, for example:
`dlv exec ./hello -- server --config conf/config.toml`
Usage:
dlv [command]
Available Commands:
attach Attach to running process and begin debugging.
connect Connect to a headless debug server.
debug Compile and begin debugging main package in current directory, or the package specified.
exec Execute a precompiled binary, and begin a debug session.
run Deprecated command. Use 'debug' instead.
test Compile test binary and begin debugging program.
trace Compile and begin tracing program.
version Prints version.
Flags:
--accept-multiclient[=false]: Allows a headless server to accept multiple client connections. Note that the server API is not reentrant and clients will have to coordinate.
--api-version=1: Selects API version when headless.
--build-flags="": Build flags, to be passed to the compiler.
--headless[=false]: Run debug server only, in headless mode.
--init="": Init file, executed by the terminal client.
-l, --listen="localhost:0": Debugging server listen address.
--log[=false]: Enable debugging server logging.
--wd=".": Working directory for running the program.
Use "dlv [command] --help" for more information about a command.