Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
How do you design Web API in Golang?
Search
itosho
May 28, 2018
Programming
780
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
How do you design Web API in Golang?
Gopher道場#1 LT大会での発表資料です。
itosho
May 28, 2018
More Decks by itosho
See All by itosho
【2025年版】インターネットサービスの育て方 / How to Grow and Scale Internet Services 2025
itosho
0
290
Windsurf Rulesでどう自分のクローンをつくるか / How can I make a clone of myself in Windsurf Rules
itosho
0
150
Text EditorとしてのWindsurf / Windsurf as a Text Editor
itosho
0
390
【2024年版】インターネットサービスの育て方 / How to Grow and Scale Internet Services
itosho
0
140
打線組という個人サービスを Goで開発している話 / Indie Service Development by Go
itosho
1
210
Components Reconsidered
itosho
1
2.4k
打線組を支える技術 / The Technology Behind Dasengumi
itosho
0
79
組織をスケールさせるためのTech Vision / Connehito Tech Vision for Growing Our Team
itosho
2
720
生きのびるためのインディー開発 / Indie Development to Survive
itosho
0
74
Other Decks in Programming
See All in Programming
パーソナライズPodcastを作った話
repunit11
0
140
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
570
AWSに止められる覚悟してますか?
morizo_1984
2
530
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
34
16k
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
480
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
150
golang-bob.pdf
iwatsukayura
0
130
Domain-Driven Transformation
hschwentner
2
2.3k
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
1
280
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.1k
Featured
See All Featured
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
580
HDC tutorial
michielstock
2
960
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
Evolving SEO for Evolving Search Engines
ryanjones
0
320
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
59k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
430
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
700
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Transcript
How do you design Web API in Golang? Gopher Dojo#1,
MAY 28 2018 Sho Ito Connehito Inc. @itosho
自己紹介 How do you design Web API in Golang? •
伊藤 翔, @itosho ◦ 所属: コネヒト株式会社 / Supership株式会社 • 普段はバックエンドエンジニア ◦ よく書く言語: PHP / Ruby • 個人でいくつかサービスつくっています ◦ https://itosho.github.io/ • 野球とアイドルが好きです⚾
Gopher道場で学べてよかったこと How do you design Web API in Golang? •
CLIツールの基礎 • Goにおけるユニットテストの考え方 • 実践的なgoroutineの知識 • contextパッケージの使い方 • Goっぽい書き方 / Goの学び方
控えめに言って最高! ありがとうございました! 感想
もっと知りたい! 欲張りですいません! でも…
今日話すこと How do you design Web API in Golang? •
GoにおけるWeb API開発で3大よく分かっていないこと ◦ 雰囲気で現状はこうしてるという話をします • 懇親会等でみなさんの知見や意見を聞きたい ◦ スライド公開するのでソーシャル上でも意見もらえると嬉しいです
前提 How do you design Web API in Golang? •
インディー開発 • 規模は小さい ◦ エンドポイント数:17 ◦ テーブル数:10 ◦ 単一リソースに対するCRUD処理がほとんど • インフラはVPS • データベースはMySQL
その1: ORMライブラリ
選択肢 How do you design Web API in Golang? •
GORM • genmai • 標準パッケージ(database/sql) ◦ 他にも色々あるけど触ったことがあるのは上記3つ
現状 How do you design Web API in Golang? •
GORMを使っている ◦ 初心者に database/sql はちょっと気が重い ◦ Rails上がりなのでとっつきやすい ◦ ドキュメントが充実している(情報もgenmaiより多い)
ここがよく分からない How do you design Web API in Golang? •
うまく言語化出来てないけど、あんまりGoっぽくない ◦ 既にスキーマ管理は別でやってる • 複雑なことをすると辛そう • datetime型の扱いが面倒 ◦ 参考: 「Golangのgormで、カスタマイズしたtime.Timeに差し変える」 ▪ https://qiita.com/roothybrid7/items/52623bedb45ff0c26a8a • genmaiのほうがバランスいいかも? ◦ ただ、genmaiはタグの使い方が他と違うような気がする • 最終的には database/sql or GAE/Datastore?
その2: パッケージ構成
現状 How do you design Web API in Golang? MVCライクな構成
server: →Railsで言うコントローラ model: →構造体定義, 1テーブル1ファイル database: →db接続(後述) / スキーマ管理 assets: →静的ファイルを格納 main.go: →ルーティングとHTTPハンドラ定義 ※go-json-restを利用
database/database.go How do you design Web API in Golang? var
DB *gorm.DB // main.goでInitDBを呼び出す // database.DB.First(&user, id).Error のような形で利用 func InitDB() { var err error user := os.Getenv("MYSQL_USER") pass := os.Getenv("MYSQL_PASSWORD") parameter := "charset=utf8&parseTime=True&loc=Asia%2FTokyo" DB, err = gorm.Open("mysql", user+":"+pass+"@/hoge?"+parameter) // 省略 }
ここがよく分からない How do you design Web API in Golang? •
database接続変数がグローバル変数なのはしょうがない? • 今後server / modelパッケージが肥大化していった時はどうすれば? ◦ ググるとDDDライクな例をよくみるけど…? ▪ 自分のサービスだと若干オーバースペックに感じる
その3: インフラ環境
現状 How do you design Web API in Golang? Nginx
MySQL Golang Supervisor client • nginxを前段に置いている • Supervisorを利用して、デーモン化している • 静的ファイルは http.FileServer を利用して配信 • デプロイはバイナリをアップロードするだけ static files
main.go How do you design Web API in Golang? func
main() { // 省略 l, err := net.Listen("tcp", ":8080") if err != nil { return } http.Handle("/v1/", http.StripPrefix("/v1", api.MakeHandler())) http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets")))) fcgi.Serve(l, nil) }
ここがよく分からない How do you design Web API in Golang? •
静的ファイルの配信方法 ◦ ぶっちゃけS3を利用したほうが楽そう • デーモン化はSupervisorを利用する以外に便利な方法があるのか? • nginx + goのベストプラクティス ◦ 参考: 「Nginx with Goのパフォーマンスを検証した」 ▪ http://tsuyoshi-nakamura.hatenablog.com/entry/2016/12/25/182946 • GAEにすればORM含めて色々解決する説
みなさんの意見を聞かせてください! まとめ
ご清聴ありがとうございました