Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
楽してコードを書くためのエディタ活用術/golang on emacs
Search
NAGATA Hiroaki
January 20, 2018
Programming
0
2.8k
楽してコードを書くためのエディタ活用術/golang on emacs
できるだけ手を動かさず、頭も使わずにコードを書くための機能の紹介です。
題材としてEmacsを使っていますが、他のエディタでも設定次第で同じような機能が利用できるはず。
NAGATA Hiroaki
January 20, 2018
Tweet
Share
More Decks by NAGATA Hiroaki
See All by NAGATA Hiroaki
生成AIとの上手な付き合い方【公開版】/ How to Get Along Well with Generative AI (Public Version)
handlename
0
570
Other Decks in Programming
See All in Programming
パスタの技術
yusukebe
1
360
管你要 trace 什麼、bpftrace 用下去就對了 — COSCUP 2025
shunghsiyu
0
390
「リーダーは意思決定する人」って本当?~ 学びを現場で活かす、リーダー4ヶ月目の試行錯誤 ~
marina1017
0
220
GitHub Copilotの全体像と活用のヒント AI駆動開発の最初の一歩
74th
7
2.6k
新しいモバイルアプリ勉強会(仮)について
uetyo
1
250
令和最新版手のひらコンピュータ
koba789
13
7.6k
AIのメモリー
watany
13
1.4k
#QiitaBash TDDで(自分の)開発がどう変わったか
ryosukedtomita
1
360
Bedrock AgentCore ObservabilityによるAIエージェントの運用
licux
9
630
GUI操作LLMの最新動向: UI-TARSと関連論文紹介
kfujikawa
0
860
Gemini CLIの"強み"を知る! Gemini CLIとClaude Codeを比較してみた!
kotahisafuru
3
990
Portapad紹介プレゼンテーション
gotoumakakeru
1
130
Featured
See All Featured
Designing for Performance
lara
610
69k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
110
20k
Six Lessons from altMBA
skipperchong
28
3.9k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.6k
Designing Experiences People Love
moore
142
24k
Gamification - CAS2011
davidbonilla
81
5.4k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Transcript
楽してコードを書くための テキストエディタ活用術 20 January 2018 NAGATA Hiroaki KAYAC Inc.
Presenter 長田洸明 (@handlename) KAYAC Inc. Lobi 事業部 サーバーサイドエンジニア Perl, Go
Emacs 歴は8 年くらい エディタは色々触っているけど結局Emacs に戻ってきました。 Emacs -> Vim -> Emacs -> SublimeText2 -> Emacs -> Atom -> Emacs
Text Editors
Text Editors 機能拡張 できるエディタが広く使われている。 Atom Emacs Vim Visual Studio Code
etc. ※アルファベット順です
Text Editors なぜエディタの機能を拡張するのかというと・・・ 楽をしたい できるだけ操作したくない できるだけ頭を使いたくない エディタに仕事をさせましょう。
Emacs
Emacs www.gnu.org/software/emacs/ (https://www.gnu.org/software/emacs/) 1972 年にリリースされたテキストエディタ 現在もバージョンアップが続いている Emacs Lisp で機能拡張
go-mode.el github.com/dominikh/go-mode.el (https://github.com/dominikh/go-mode.el) Emacs でGo を書くためのmajor mode Emacs でGo を書くならとりあえず入れておくべし
Spacemacs spacemacs.org/ (http://spacemacs.org/) A community-driven Emacs distribution - The best
editor is neither Emacs nor Vim, it's Emacs *and* Vim! Emacs のための設定集 ドキュメントが充実している 自分でEmacs Lisp を( ほとんど) 書かなくていい! 設定ファイルに一行追加すればgo-mode.el とその他便利機能が有効になる ;; .spacemacs (defun dotspacemacs/layers () (setq-default ;; ... dotspacemacs-configuration-layers '( ;; ... go ; 足 )))
Topics Code Writing Code Reading Build & Run Code
Code Writing
Code Writing Code Format Add/Delete Import Code Completion Syntax Check
Rename
Code Format M-x gofmt Go を書く上でのマナー インデントや空白文字の自動整形 gofmt (https://golang.org/cmd/gofmt) を利用している
ファイル保存時に自動実行するのが吉(spacemacs のデフォルト設定)
Add/Delete Import コード内で利用しているpackage を特定して import に自動追加 goimports (https://golang.org/x/tools/cmd/goimports) を利用している 同時にコードフォーマットしてくれるのでgofmt
代わりにも go-mode.el ではgofmt と入れ替える方法が紹介されている (go :variables gofmt-command "goimports"
Code Completion 定義済み関数・変数の入力補完 関数なら引数リストまで補完される struct はメンバまで補完される gocode (https://github.com/nsf/gocode) を利用している
Syntax Check 構文エラーをエディタ上に表示 go build する前に気付ける ycheck が各種linter( go vet
(https://golang.org/cmd/vet/) とか golint (https://github.com/golang/lint) とか) を呼び出し gometalinter (https://github.com/alecthomas/gometalinter) を利用することもできる (go :variables go-use-gometalinter t)
Rename M-x go-rename 変数名・関数名をリネーム 機械的な置換ではなく、同一定義のものだけをリネーム 別のファイルに書かれているものもリネーム gorename (https://golang.org/x/tools/cmd/gorename) を利用 構文エラーがあると実行できないので注意
Code Reading
Code Reading Reference De nition Reference GoDoc Jump to De
nition
Reference De nition 関数のインターフェイス・変数の型をさっと確認 いちいちgodoc を引くのはちょっと・・・ カーソルを合わせるとモードラインに自動で表示される コードリーディングの時にお世話になる
Reference GoDoc M-x go-doc-at-point 関数・変数のgodoc を引く さっと確認するには便利 Dash との使い分け
Jump to De nition M-x godef-jump 変数・関数の定義元にジャンプ godef (https://github.com/rogpeppe/godef) を利用
コードリーディングのお供
Build & Run Code
Build & Run Code Run Test Quickrun Post to Playground
Run Test M-x spacemacs/go-run-package-tests M-x spacemacs/go-run-test-current-function 開いているファイルに関連するテストを実行する プロジェクト全体/ ファイル内/ テストケース単体
を実行できる Spacemacs の独自関数 go test に -v オプションをを付けたいので手を加えている (advice-add 'spacemacs/go-run-tests :filter-args (lambda (args) (list (concat "-v " (car args)))))
Quickrun M-x quickrun 現在開いているファイルor 選択している範囲をその場で実行 quickrun.el (https://github.com/syohex/emacs-quickrun) を利用 quickrun.vim (https://github.com/thinca/vim-quickrun)
が元ネタ Go に限らず複数の言語に対応
Post to Playground M-x go-play-region M-x go-play-buffer 今書いているコードを paly.golang.org に投稿
投稿結果のURL が kill-ring にコピーされる
Conclusion Go はエディタから独立した開発支援ツールが充実している 好きなエディタ/IDE を使いましょう NO MORE WAR! エディタに仕事をさせて快適なコーディングライフを
Appendix
Environment OSX v10.13.2 Emacs v25.3.1 Spacemacs v0.200.10
Environment Variables Emacs 上ではPATH が通っていなくて外部コマンドが呼び出せない問題 .zshrc でEmacs 用の環境変数設定ファイルを生成している 環境変数の不一致でハマることがないので良い perl
$HOME/src/github.com/handlename/spacemacs-private/scripts/init_shellenv.pl \ > $HOME/src/github.com/handlename/spacemacs-private/ondemand/shellenv.el #!/usr/bin/env perl use strict; use warnings; for my $name (sort keys %ENV) { next if ! exists $ENV{$name}; my $value = $ENV{$name}; $value =~ s/\\/\\\\/g; printf qq{(setenv "%s" "%s")\n}, $name, $value; } (setenv "PATH" "/Users/nagata-hiroaki/.anyenv/envs/plenv/versions/5.26.1/bin:/Users/nagata-hiroaki/.anye (setenv "GOPATH" "/Users/nagata-hiroaki")
Guru godoc.org/golang.org/x/tools/cmd/guru (https://godoc.org/golang.org/x/tools/cmd/guru) よりIDE っぽいことができる 変数の呼び出し箇所を調べたり callstack を調べたり まだ使いこなせていない