LoginSignup
0
0

More than 3 years have passed since last update.

Mac VSCode dlvでGoのデバッグをする方法

Last updated at Posted at 2021-05-09

goをVSCodeでデバッグするには、dlvというものが必要らしい。
dlvは、Goのデバッガのこと。静的型付け言語?のため、強者はlog.Printf("%+v")でも問題箇所に気づくことはできるらしいが、delveを使ったデバッグは便利とのこと。

dlvをインストールするのに若干ハマったので備忘録として残しておく。

環境

VS Code
Mac Os
brew

goのインストール

brew install go
go version
go version go1.16.3 darwin/amd64kkk

GOPATHを通しておく。

$echo 'export PATH=~/go/bin:$PATH'>>~/.bash_profile

dlvのインストール

試したこと

brew install go-delve/delve/delve

brew でdlvをインストールしようとしたら、↓で怒られた。

Error: 
  homebrew-core is a shallow clone.
To `brew update`, first run:
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
==> Tapping go-delve/delve
Cloning into '/usr/local/Homebrew/Library/Taps/go-delve/homebrew-delve'...
remote: Enumerating objects: 48, done.
remote: Total 48 (delta 0), reused 0 (delta 0), pack-reused 48
Receiving objects: 100% (48/48), 12.44 KiB | 509.00 KiB/s, done.
Resolving deltas: 100% (19/19), done.
Tapped (29 files, 53.7KB).
==> Tapping homebrew/cask
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Enumerating objects: 570158, done.
remote: Counting objects: 100% (509/509), done.
remote: Compressing objects: 100% (322/322), done.
remote: Total 570158 (delta 259), reused 415 (delta 187), pack-reused 569649
Receiving objects: 100% (570158/570158), 250.97 MiB | 9.20 MiB/s, done.
Resolving deltas: 100% (402517/402517), done.
Tapped 3899 casks (3,995 files, 269.2MB).
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
Error: No available formula or cask with the name "go-delve/delve/delve".
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.

formulaeがないとのこと。
formulaeとは、「調理法、手順」という意味で、Homebrewでは「ビルド方法・手順が書かれたスクリプト」のこと。

https://qiita.com/b4b4r07/items/6efebc2f3d1cbbd393fc#1-3

少し調べたが、あまり解決策っぽいのは出てこなかったので、
brewでインストールするのは諦めて、↓のようにすると無事にインストールできた。

go get -u github.com/go-delve/delve/cmd/dlv

go getでは、コマンドラインツールの実行ファイルを勝手にインストールしてくれるっぽい。

①go getが叩かれるとGoで書かれたコマンドラインツールの実行ファイルをGOPATH/bin配下に自動的に置いてくれます。
②①と同じタイミングでGOPATH/srcにパッケージのソースコードが置かれます。ちなみに、go getでは取得したパッケージの依存パッケージ(importの中に書いてあるパッケージ)も一緒にダウンロードしてくれます。
https://qiita.com/yu19991013/items/ae4f6e0ebd59dfb9bc3d

dlv version
Delve Debugger
Version: 1.6.0
Build: $Id: 8cc9751909843dd55a46e8ea2a561544f70db34d

プロジェクトの作成

Goは$GOPATHにリポジトリとかgo getで取得してビルドされた実行ファイルが置かれる。
~/goのフォルダの下にsrcを作ってそこにプロジェクトを作成するのがお作法らしい?

~/go
├── bin
├── pkg
│   ├── mod
│   └── sumdb
└── src
    └── hello //helloというフォルダを作成してこの中で、.goファイルの作成

VSCodeでデバッグしようとすると、開かれたディレクトリのmain()から実行される。
最初は、複数ファイルにmain()を使用していて、うまくデバッグできなかった。
よって、一つのmain.goファイルの中で関数を分けるか、パッケージを作成してimportする。

パッケージの作成方法とimportに関しては、改めて勉強し記事にする。

0
0
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
0
0