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
710
Other Decks in Programming
See All in Programming
CSC305 Lecture 08
javiergs
PRO
0
270
Catch Up: Go Style Guide Update
andpad
0
240
ソフトウェア設計の実践的な考え方
masuda220
PRO
4
630
Android16 Migration Stories ~Building a Pattern for Android OS upgrades~
reoandroider
0
130
Reactive Thinking with Signals and the Resource API
manfredsteyer
PRO
0
100
オープンソースソフトウェアへの解像度🔬
utam0k
17
3.1k
Things You Thought You Didn’t Need To Care About That Have a Big Impact On Your Job
hollycummins
0
250
AI 駆動開発におけるコミュニティと AWS CDK の価値
konokenj
4
100
When Dependencies Fail: Building Antifragile Applications in a Fragile World
selcukusta
0
110
タスクの特性や不確実性に応じた最適な作業スタイルの選択(ペアプロ・モブプロ・ソロプロ)と実践 / Optimal Work Style Selection: Pair, Mob, or Solo Programming.
honyanya
3
190
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
190
iOSでSVG画像を扱う
kishikawakatsumi
0
150
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
For a Future-Friendly Web
brad_frost
180
10k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
jQuery: Nuts, Bolts and Bling
dougneiner
65
7.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
Product Roadmaps are Hard
iamctodd
PRO
55
11k
Fireside Chat
paigeccino
40
3.7k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
610
Making the Leap to Tech Lead
cromwellryan
135
9.6k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
990
The World Runs on Bad Software
bkeepers
PRO
72
11k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
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 を調べたり まだ使いこなせていない