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.2k
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
生成AI活用の組織格差を解消する 〜ビジネス職のCursor導入が開発効率に与えた好循環〜 / Closing the Organizational Gap in AI Adoption
upamune
7
5.3k
バクラクのモノレポにおける AI Coding のための環境整備と {Roo,Claude} Code活用事例 / AI Coding in Bakuraku's Monorepo: Environment Setup & Case Studies with {Roo, Claude} Code
upamune
10
7.6k
AI Coding in Bakuraku's Monorepo: Environment Setup & Case Studies with {Roo, Claude} Code [EN]
upamune
0
51
来週から実践できる! LayerXのバクラク事業部で行われている Go 関連の読書会のご紹介 / Bakuraku Go Reading Group
upamune
7
1.8k
冗長なエラーログを削減し、スタックトレースを手に入れる / Reducing Verbose Error Logs and Obtaining Stack Traces
upamune
2
2.2k
バクラク事業部でのGoのユースケースとチームを超えたナレッジ共有 | hatena.go
upamune
9
3.6k
未卒 🔜 新卒 / Misotsu soon New Grads
upamune
2
730
Other Decks in Programming
See All in Programming
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
950
スタートアップの急成長を支えるプラットフォームエンジニアリングと組織戦略
sutochin26
1
6.1k
0626 Findy Product Manager LT Night_高田スライド_speaker deck用
mana_takada
0
180
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
260
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
2
860
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
21
4k
Systèmes distribués, pour le meilleur et pour le pire - BreizhCamp 2025 - Conférence
slecache
0
120
What's new in AppKit on macOS 26
1024jp
0
100
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
190
AIエージェントはこう育てる - GitHub Copilot Agentとチームの共進化サイクル
koboriakira
0
600
AI時代の『改訂新版 良いコード/悪いコードで学ぶ設計入門』 / ai-good-code-bad-code
minodriven
20
8.1k
Result型で“失敗”を型にするPHPコードの書き方
kajitack
5
930
Featured
See All Featured
Fireside Chat
paigeccino
37
3.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
Building an army of robots
kneath
306
45k
Balancing Empowerment & Direction
lara
1
430
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
What's in a price? How to price your products and services
michaelherold
246
12k
4 Signs Your Business is Dying
shpigford
184
22k
Adopting Sorbet at Scale
ufuk
77
9.5k
Visualization
eitanlees
146
16k
Six Lessons from altMBA
skipperchong
28
3.9k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
960
Embracing the Ebb and Flow
colly
86
4.7k
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