LoginSignup
67
71

More than 5 years have passed since last update.

10分で終わるGo言語の開発環境構築

Last updated at Posted at 2016-02-28

環境構築に時間を取られるのはもったい。
凝るのは後々!最低限のものを準備して早々に書きたいという人へ

実行環境

HomeBrewのセットアップ

  • HomeBrewのアップデート
    • $ brew updateで完了

HomeBrewがない場合

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

GoLangのセットアップ

  • インストール
$ brew install go
  • 確認
$ go version
go version go1.4.2 darwin/amd64
  • Goのワークスペースを作成
    • GoのソースコードやGoの実行可能ファイル、並びにコンパイル済みのパッケージファイルを保存する為のディレクトリを持つワークスペースが必要なので作成する
# Goのワークスペース構成
# src/ ソースコードを保存する(例えば:.go .c .h .s等) 
# bin/ コンパイル後に生成される実行可能ファイル
# pkg/ コンパイル後に生成されるファイル(例えば:.a)
$ mkdir ~/code/golang/{src,bin,pkg}

  • PATH設定を~/.zprofileもしくは~/.bash_profileに追加して反映で完了
$ echo export GOROOT=/usr/local/opt/go/libexec >> ~/.zprofile
$ echo export GOPATH=$HOME/code/golang >> ~/.zprofile
$ echo export PATH=$PATH:$GOROOT/bin:$GOPATH/bin >> ~/.zprofile
$ source ~/.zprofile

外部ライブラリの追加

goimports

過不足のimportを自動で良きに計らってくれる

  • インスール
$ go get golang.org/x/tools/cmd/goimports
  • 確認
$ goimports -help
usage: goimports [flags] [path ...]
  -d=false: display diffs instead of rewriting files
  -e=false: report all errors (not just the first 10 on different lines)
  -l=false: list files whose formatting differs from goimport's
  -srcdir="": choose imports as if source code is from `dir`
  -w=false: write result to (source) file instead of stdout

goCodeのインストール

Goのヘルパー機能を提供してくれる

  • インストール
$ go get -u -v github.com/nsf/gocode
  • 確認
gocode -help
Usage: gocode [-s] [-f=<format>] [-in=<path>] [-sock=<type>] [-addr=<addr>]
       <command> [<args>]

Flags:
  -addr="127.0.0.1:37373": address for tcp socket
  -debug=false: enable server-side debug mode
  -f="nice": output format (vim | emacs | nice | csv | json)
  -in="": use this file instead of stdin input
  -profile=0: port on which to expose profiling information for pprof; 0 to disable profiling
  -s=false: run a server instead of a client
  -sock="unix": socket type (unix | tcp)

Commands:
  autocomplete [<path>] <offset>     main autocompletion command
  close                              close the gocode daemon
  status                             gocode daemon status report
  drop-cache                         drop gocode daemon's cache
  set [<name> [<value>]]             list or set config options

godefの追加

呼び出し先関数へのジャンプ機能などを強化してくれる

  • インストール
$ go get -v github.com/rogpeppe/godef
  • 確認
godef -help 
usage: godef [flags] [expr]
  -A=false: print all type and members information
  -a=false: print public type and member information
  -acme=false: use current acme window
  -debug=false: debug mode
  -f="": Go source filename
  -i=false: read file from stdin
  -o=-1: file offset of identifier in stdin
  -t=false: print type information

Go Oracleの追加

静的解析ツールを提供してくれる

  • インストール
go get -u -v golang.org/x/tools/cmd/oracle
  • 確認
 oracle -help
Run 'oracle -help' for more information.
Go source code oracle.
Usage: oracle [<flag> ...] <mode> <args> ...

Golintの追加

Go言語用のlintを提供

  • インストール
$ go get -u github.com/golang/lint/golint
  • 確認
$ golint -help
Usage of golint:
  golint [flags] # runs on package in current directory
  golint [flags] package
  golint [flags] directory
  golint [flags] files... # must be a single package
Flags:
  -min_confidence=0.8: minimum confidence of a problem to print it

Sublime Text3のプラグインを追加

すべてPackegeControlで追加します。Command+Shift+pでコマンドパレッドを開いてinstallです。

GoSublimeの追加

  1. PackegeControlでGoSublimeをインストール
    • gosublimeと入力してGoSublimeを選択
  2. インストール後以下のアナウンスが出るのでSublimeを再起動する
GoSublime appears to have been updated.
New ANNOUNCE: `a16.01.09-1`, current ANNOUNCE: `a16.01.09-1`
You may need to restart Sublime Text.

SublimeLinter-contrib-golintの追加

Golintをsublimeで使えるようにするプラグイン

【SublimeLinterが入っていない場合】
PackegeControlでsublimelinterと入力してSublimeLinterを追加

  1. PackegeControlでgolintと入力。SublimeLinter-contrib-golintを選択してインストール
  2. インストール後、再起動する
  3. 設定の変更
    • Preference > Package Settings > SublimeLinter > Settings - Userで設定を開いてpathsの対応するOSのkey名に対して$GOPATH/binをフルPATHで記載する
        "mark_style": "solid underline",
        "no_column_highlights_line": false,
        "passive_warnings": false,
        "paths": {
            "linux": [],
            "osx": [
                "/hoge/hoge/code/golang/bin"
            ],
            "windows": []
        },

完了後はSublimeTextを再起動して完了

Golintが動かない場合

デバッグモードにしてコンソールに出力されるデバッグログを見るのが早いです

  1. Tools > SublimeLinter > Setting - Debug Mode を選択
  2. Control +` でコンソールを開く
  3. 出力ログを確認して適宜修正

参考

67
71
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
67
71