Mac で Go をサクッとはじめるための手引書

Go のインストール

1
2
3
4
5
6
7
8
$ mkdir $HOME/.go

# Add this line to ~/.zshrc
export GOPATH=$HOME/.go
export PATH=$PATH:$GOPATH/bin

# Install
brew install go

バージョン確認

1
2
$ go version
go version go1.13.4 darwin/amd64

REPL のインストール

1
2
3
4
5
6
7
8
$ GO111MODULE=off go get -u github.com/motemen/gore/cmd/gore

$ gore # 確認
gore version 0.4.1  :help for help
gore> fmt.Println("hello")
hello
6
<nil>

Go Package のインストール

1
2
3
4
5
6
7
8
# gopkgs is a tool that provides list of available Go packages that can be imported.
$ GO111MODULE=on go get github.com/uudashr/gopkgs/cmd/gopkgs@latest

# Golint is a linter for Go source code.
$ go get -u golang.org/x/lint/golint

# errcheck is a program for checking for unchecked errors in go programs.
$ go get -u github.com/kisielk/errcheck

補完ツールのインストール

補完については gocodego get してくるという記事がよく見られるが、これは以下のような事情でちょっと使わない方がいい。

またこの記事では bingo と golsp について言及しているが、これもまた事情が変わって今は gopls に一本化する方向に向かっている。 こちらの記事が詳しい。

それらを踏まえて設定していく。

gopls をインストール

Language Server の gopls をインストールする。

1
$ GO111MODULE=on go get golang.org/x/tools/gopls@latest

(cf. https://github.com/golang/tools/blob/master/gopls/doc/user.md)

VSCode に Go のプラグインをインストール

こちらの Go のプラグインをインストールする。

settings.json の設定

こんな感じで settings.json に追記して VSCode を再起動する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
    "go.useLanguageServer": true,
    "go.alternateTools": {
        "go-langserver": "gopls"
    },
    "go.languageServerExperimentalFeatures": {
        "format": true,
        "autoComplete": true,
        "rename": true,
        "goToDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "goToTypeDefinition": true,
        "goToImplementation": true,
        "documentSymbols": true,
        "workspaceSymbols": true,
        "findReferences": true,
        "diagnostics": true
    },
    "[go]": {
        "editor.snippetSuggestions": "none",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
    },
    "gopls": {
        "usePlaceholders": false
    }
}

以上で補完が効くようになった。