牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Emacs で Golang の開発環境を整えるメモ 2019年版

EmacsGolang 環境まわりがいろいろ変わってて gocode が動かなかったり Flycheckの設定が変わってたりしてた。
会社の人から VScode を勧められたけど Emacs 使いはそんなことでは負けないんだからね!😢

前提

  • macOS mojave
  • anyenv で goenv 入れて go13.x インストール済み
  • 要望として下記ができればいいかな
    • 構文チェック
    • 補完
    • 保存時に gofmt

go のパッケージをインストール

$ go get -u golang.org/x/tools/cmd/gopls
$ go get golang.org/x/tools/cmd/goimports

M-x package-list-packages でインストール

  • go-mode
  • lsp-mode
  • company
  • company-lsp
  • lsp-ui
  • exec-path-from-shell

.zshrc

export PATH="$HOME/.anyenv/bin:$PATH"
eval "$(anyenv init - zsh)"

export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"

init.el

なぜか exec-path-from-shell-copy-envs を2回読み込まないと goimports 動かない。

  • 追記2019-11-22
    • goimports がコード書いてるとなんかうざくなるので使うのやめた。
    • helm-occur で関数とか絞り込むと便利なことに気づいた。行数増えると探すのツラくなるので。
(exec-path-from-shell-initialize)

;; golang
(progn
  (let ((envs '("GOROOT" "GOPATH" "PATH")))
    (exec-path-from-shell-copy-envs envs))

  (add-hook 'go-mode-hook
            (lambda ()
              (setq indent-tabs-mode t)
              ;; (setq gofmt-command "goimports")
              (add-hook 'before-save-hook 'gofmt-before-save)

              (define-key go-mode-map (kbd "M-.") 'xref-find-definitions)

              (let ((govet (flycheck-checker-get 'go-vet 'command)))
                (when (equal (cadr govet) "tool")
                  (setf (cdr govet) (cddr govet))))
              ))
  (add-hook 'go-mode-hook #'lsp-deferred)

  (eval-after-load 'go-mode
    '(progn
       (exec-path-from-shell-copy-envs '("GOROOT" "GOPATH" "PATH"))
       (define-key go-mode-map (kbd "C-c C-j") 'helm-occur)
    ))
  )

感想

いろいろ挙動おかしくなったりするから go を書くなら VScode をオススメしたい(使ったことないけど)

参考

macOS Mojave の日本語入力でshift+スペースが全角・半角交互に入力されるやつ回避のメモ

plist いじるのなんか嫌だったんでやってなかったんだけど、結局あまりにもうざいのでやってしまった。

変更後の挙動は日本語入力時に space キー押下で全角スペース、shift+space キーで全角。(常に半角スペースでもいいんだけど長年の癖で...)

やり方

こちらの記事だいたいそのまま

⌘R 押しながら再起動 > ユーティリティ > ターミナル

$ csrutil disable
$ reboot

再起動後ファイルの内容を書き換え

sudo vim /System/Library/Input Methods/JapaneseIM.app/Contents/PlugIns/JapaneseIM.appex/Contents/Resources/KeySetting_Default.plist

修正前

         <dict>
                <key>command</key>
                <string>contextual_space</string>
            </dict>
            <key>shift+&apos; &apos;</key>
            <dict>
                <key>command</key>
                <string>contextual_space_reverse</string>
            </dict>

修正後

                     <dict>
                                <key>command</key>
                                <string>direct_input</string>
                                <key>character</key>
                                <string> </string>
                        </dict>
                        <key>shift+&apos; &apos;</key>
                        <dict>
                                <key>command</key>
                                <string>direct_input</string>
                                <key>character</key>
                                <string> </string>
                        </dict>

ファイルを修正したら

sudo killall -HUP JapaneseIM

動作確認しておく

また ⌘R 押しながら再起動 > ユーティリティ > ターミナル

$ csrutil disable
$ reboot

DONE!