今更ながら、Ubnutu20.04.3にGoをインストールします。
インストールするマシンについて
os情報
user@machine:~/gotrain$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
マシン情報
user@machine:~/gotrain$ uname -a
Linux hs-Inspiron-3250 5.13.0-28-generic #31~20.04.1-Ubuntu SMP Wed Jan 19 14:08:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Goをインストールする
今回は、Ubuntuにgo langをインストールしたいと思います。
インストールは、公式サイトを参考にインストールをすすめていきます。
まず、Goのパッケージをインストールします。
user@machine:~$ wget https://go.dev/dl/go1.17.7.linux-amd64.tar.gz
実行結果
--2022-02-19 10:50:36-- https://go.dev/dl/go1.17.7.linux-amd64.tar.gz
go.dev (go.dev) をDNSに問いあわせています... 2001:4860:4802:34::15, 2001:4860:4802:36::15, 2001:4860:4802:32::15, ...
go.dev (go.dev)|2001:4860:4802:34::15|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: https://dl.google.com/go/go1.17.7.linux-amd64.tar.gz [続く]
--2022-02-19 10:50:36-- https://dl.google.com/go/go1.17.7.linux-amd64.tar.gz
dl.google.com (dl.google.com) をDNSに問いあわせています... 2404:6800:4004:80e::200e, 216.58.197.206
dl.google.com (dl.google.com)|2404:6800:4004:80e::200e|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 134835336 (129M) [application/x-gzip]
`go1.17.7.linux-amd64.tar.gz' に保存中
go1.17.7.linux-amd6 100%[===================>] 128.59M 810KB/s in 6m 25s
2022-02-19 10:57:01 (342 KB/s) - `go1.17.7.linux-amd64.tar.gz' へ保存完了 [134835336/134835336]
Goパッケージを展開する
圧縮ファイルを/usr/local
に展開します。
user@machine:~$ sudo tar -C /usr/local -xzf go1.17.7.linux-amd64.tar.gz
解凍できた確認します。
user@machine:~$ ls -l
-rw-rw-r-- 1 user user 134835336 2月 11 07:41 go1.17.7.linux-amd64.tar.gz
PATHを追加
更に、PATHを追加します。
user@machine:~$ export PATH=$PATH:/usr/local/go/bin
user@machine:~$ source ~/.profile
Goバージョンを確認
user@machine:~$ ~/gotrain$ go version
go version go1.17.7 linux/amd64
バージョンが表示されたらOKです。
VSCodeにGo開発環境を構築する
続いて、VSCodeの環境を整えていきます。
拡張機能をインストール
VSCodeの拡張機能から、Go
もしくは、@id:golang.go
と検索欄に入力すると、以下のパッケージが表示されるので、インストールを勧めてください。
必要なパッケージのインストールとアップデート
拡張機能を追加したら、ctrl
+ shift
+ p
を入力してください。
検索欄が表示されたら、Go:Install/Update Pakage
表示されるので、選択してください。
すべての項目のインストールが完了したら、終了です。
補足:ツールのアップデートでエラーが発生
エラー内容
エラー内容
cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH
Tools environment: GOPATH=/home/users/go
Installing 9 tools at /home/users/go/bin in module mode.
gopkgs
go-outline
gotests
gomodifytags
impl
goplay
dlv
staticcheck
gopls
Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest (/home/users/go/bin/gopkgs) SUCCEEDED
Installing github.com/ramya-rao-a/go-outline@latest (/home/users/go/bin/go-outline) SUCCEEDED
Installing github.com/cweill/gotests/gotests@latest (/home/users/go/bin/gotests) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (/home/users/go/bin/gomodifytags) SUCCEEDED
Installing github.com/josharian/impl@latest (/home/users/go/bin/impl) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (/home/users/go/bin/goplay) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest FAILED
{
"killed": false,
"code": 2,
"signal": null,
"cmd": "/usr/local/go/bin/go install -v github.com/go-delve/delve/cmd/dlv@latest",
"stdout": "",
"stderr": "github.com/go-delve/delve/pkg/proc/internal/ebpf\n# github.com/go-delve/delve/pkg/proc/internal/ebpf\ncgo: C compiler \"gcc\" not found: exec: \"gcc\": executable file not found in $PATH\n"
}
Installing honnef.co/go/tools/cmd/staticcheck@latest (/home/users/go/bin/staticcheck) SUCCEEDED
Installing golang.org/x/tools/gopls@latest (/home/users/go/bin/gopls) SUCCEEDED
1 tools failed to install.
dlv: failed to install dlv(github.com/go-delve/delve/cmd/dlv@latest): Error: Command failed: /usr/local/go/bin/go install -v github.com/go-delve/delve/cmd/dlv@latest
github.com/go-delve/delve/pkg/proc/internal/ebpf
# github.com/go-delve/delve/pkg/proc/internal/ebpf
cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH
GCCをインストールする
gccがなかったので、インストールしました。
sudo apt install gcc
この状態でもう一度インストールすると、正常にインストールできます。
実行結果
Tools environment: GOPATH=/home/users/go
Installing 8 tools at /home/users/go/bin in module mode.
gopkgs
go-outline
gotests
gomodifytags
impl
goplay
dlv
staticcheck
Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest (/home/users/go/bin/gopkgs) SUCCEEDED
Installing github.com/ramya-rao-a/go-outline@latest (/home/users/go/bin/go-outline) SUCCEEDED
Installing github.com/cweill/gotests/gotests@latest (/home/users/go/bin/gotests) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (/home/users/go/bin/gomodifytags) SUCCEEDED
Installing github.com/josharian/impl@latest (/home/users/go/bin/impl) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (/home/users/go/bin/goplay) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest (/home/users/go/bin/dlv) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck@latest (/home/users/go/bin/staticcheck) SUCCEEDED
補足:VSCodeでGoのデバッグに失敗
エラー内容
エラー内容
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Starting: /home/users/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:38427 --log-dest=3 from /home/users/gotrain
DAP server listening at: 127.0.0.1:38427
Build Error: go build -o /home/users/gotrain/__debug_bin -gcflags all=-N -l .
go: go.mod file not found in current directory or any parent directory; see 'go help modules' (exit status 1)
以下の記事を参考に、問題を解決。
https://qiita.com/toshihirock/items/c660fd77f5f3cc47b560
ターミナルより、以下のコマンドを実行
[command] go mod init <モジュール名>
実行結果
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
go.modが生成されたか確認してみましょう。
users@machine:~/gotrain$ ls
go.mod hello hello.go
go.modを確認してみましょう。
users@machine:~/gotrain$ cat go.mod
module hello
go 1.17
これで、もう一度、VSCodeからデバッグを実行してみましょう。
※launch.jsonをあらかじめ作成しておきましょう。
Starting: /home/users/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:36385 --log-dest=3 from /home/users/gotrain
DAP server listening at: 127.0.0.1:36385
2022-02-19T17:27:41+09:00 error layer=debugger can't find build-id note on binary
Type 'dlv help' for list of commands.
Hello World!
Process 8615 has exited with status 0
Detaching
dlv dap (8546) exited with code: 0
以上、自分用のメモとして、かきとめました。
誰かのお力になれれば幸いです。