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
GopherCon 2019 Report
Search
upamune / Yu SERIZAWA
August 20, 2019
Programming
0
2.1k
GopherCon 2019 Report
mercari.go #10で発表したGopherCon 2019 Reportの発表資料です.
upamune / Yu SERIZAWA
August 20, 2019
Tweet
Share
More Decks by upamune / Yu SERIZAWA
See All by upamune / Yu SERIZAWA
来週から実践できる! LayerXのバクラク事業部で行われている Go 関連の読書会のご紹介 / Bakuraku Go Reading Group
upamune
7
1.5k
冗長なエラーログを削減し、スタックトレースを手に入れる / Reducing Verbose Error Logs and Obtaining Stack Traces
upamune
1
2k
バクラク事業部でのGoのユースケースとチームを超えたナレッジ共有 | hatena.go
upamune
9
3.5k
未卒 🔜 新卒 / Misotsu soon New Grads
upamune
2
710
Other Decks in Programming
See All in Programming
MCP調べてみました! / Exploring MCP
uhzz
2
2.3k
メモリウォールを超えて:キャッシュメモリ技術の進歩
kawayu
0
1.9k
The Evolution of the CRuby Build System
kateinoigakukun
0
720
Strategic Design (DDD)for the Frontend @DDD Meetup Stuttgart
manfredsteyer
PRO
0
160
趣味全開のAITuber開発
kokushin
0
200
Youtube Lofier - Chrome拡張開発
ninikoko
0
2.4k
複雑なフォームの jotai 設計 / Designing jotai(state) for Complex Forms #layerx_frontend
izumin5210
4
1.1k
Chrome Extension Techniques from Hell
moznion
1
160
状態と共に暮らす:ステートフルへの挑戦
ypresto
2
710
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
0
110
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
3
3.7k
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
0
870
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
41
2.6k
Code Review Best Practice
trishagee
67
18k
We Have a Design System, Now What?
morganepeng
52
7.5k
Designing for humans not robots
tammielis
252
25k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.2k
Fireside Chat
paigeccino
37
3.4k
RailsConf 2023
tenderlove
30
1.1k
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
A Modern Web Designer's Workflow
chriscoyier
693
190k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Transcript
GopherCon 2019 Report mercari.go #10 @upamune
About Me @upamune (うぱみゅん) Merpay, Inc. / CodePayment Team Backend
Engineer
発表するトーク
How I Write HTTP Web Services After Eight Years Mat
Ryer 出典: https://medium.com/@matryer
Who is Mat Ryer?
Mat Ryer Blog medium.com/@matryer Podcast Go Time Books Go ⾔語による
Web アプリケーション開発 Go Programming Blueprints OSS BitBar Testify Gopherize.me etc...
出典: GoTime https://cdn.changelog.com/uploads/covers/go-time-medium.png Go⾔語によるWebアプリケーション開発 https://www.amazon.co.jp/dp/4873117526 Go Programming Blueprints https://www.amazon.co.jp/dp/B01GQCQ8OW
トーク概要
トーク概要 Go で HTTP service をどう書いているか HTTP service を書く時に重要な要素を紹介 いくつかのパターンを紹介
重要な要素
重要な要素 Maintainability Glaceability Code should be boring Self Similar code
Maintainability 最初から Maintainability を考慮して書く 考慮しないと、メンテナンスコストがツールを作る時よりも⼤きくなる可能性
Glaceability 視認性 コードを読んでどれくらい早くコードを理解できるか コードだけではなく、プロジェクト構造なども含まれる 関数、名前空間、変数名、コードの構造、etc...
Code should be boring 他の⼈が理解できるように書く 経験のほとんどない⼈がコードを利⽤する可能性があることを理解するのが重要
Self Similar code コードベース内の他のコードに似たコードがあると、 他の⼈がコードに親しみやすくなる
Design Patterns/Decisions
全部紹介しきれないのでいくつかピックアップ
Creating a server struct & a constructor for the server
type server struct { db *someDatabase router *someRouter email EmailSender } func newServer() *server { s := &server{} s.routes() return s } グローバル変数は利⽤しない それを避けるために server 構造体が持つ newServer では依存をセットアップしない テストのため 多くなければ引数でとっても良い ルーティングだけセットアップする
Routing // routes.go func (s *server) routes() { s.router.Get("/api/", s.handleAPI())
s.router.Get("/about", s.handleAbout()) s.router.Get("/", s.handleIndex()) } ⼀箇所でルーティングを管理する 視認性 ⤴ URL からどのハンドラーを利⽤しているか容易に特定できる
Dealing with data // Respond helper func (s *server) respond(w
http.ResponseWriter, r *http.Request, data interface{}, status int) { w.WriteHeader(status) if data != nil { err := json.NewEncoder(w).Encode(data) // TODO: handle error } } // Decoding helper func (s *server) decode(w http.ResponseWriter, r *http.Request, v interface{}) error { return json.NewDecoder(r.Body).Decode(v) } 抽象化する 後から Accept ヘッダーや Content-Type ヘッダーに対応することが容易に ヘルパーは http.ResponseWriter と *http.Request を引数で受け取る
Request and response func (s *server) handleGreet() http.HanlderFunc { type
request struct { Name string `json:"name"` } type response struct { Greeting string `json:"greeting"` } return func(w http.ResponseWriter, r *http.Request) { // do something... } } Handler に関連する Request/Response が⾒つけやすい 関数の中で定義しているので request , response という短い構造体名にできる
Request and response func TestGreet(t *testing.T) { is := is.New(t)
p := struct{ Name string `json:"name"` }{ Name: "Yu SERIZAWA", } // ... test code } テストの際は request 構造体を参照できないので、リクエストの struct を定義する Name フィールドだけこのテストでは関係するとわかる
Lazy setup func (s *server) handleTemplate(file string...) http.HandleFunc { var
( init sync.Once tpl *template.Template tplerr error ) return func(w http.ResposeWriter, r *http.Request) { init.Do(func() { tpl, tplerr = template.ParseFiles(files...) }) if tplerr != nil { // return error } // use template } } 重い処理を呼ばれるまで sync.Once で遅らせる GAE を利⽤する場合などで起動時間を速くしたい場合に有効
残りは元スライド、ライブブログで
matryer/is is ... ? I call it “Testify off steroids”
:) https://gophers.slack.com/archives/C0528UE9X/p1564348834304100?thread_ts=1564339225.294700&cid=C0528UE9X
matryer/is func Test(t *testing.T) { is := is.New(t) signedin, err
:= isSignedIn(ctx) is.NoErr(err) // isSignedIn error is.Equal(signedin, true) // must be signed in body := readBody(r) is.True(strings.Contains(body, "Hi there")) } https://github.com/matryer/is#usage
トークの感想 だいたい似たよう感じに作っているので安⼼できた 業務で HTTP sevice を作ることはほとんどないが、 HTTP service 以外を書く時でも 参考にできることがあった
最初に挙げていた重要な要素など testify を置き換えたい
参考リンク Slide https://gophers.slack.com/archives/C0528UE9X/p1564339225294700 このレスに is の説明もあります 元になったブログ記事 https://medium.com/statuscode/how-i-write-go-http-services-after-seven- years-37c208122831 LiveBlog
https://about.sourcegraph.com/go/gophercon-2019-how-i-write-http-web- services-after-eight-years