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
printf-args-num-ok?
Search
decafe09
February 24, 2018
Programming
160
0
Share
printf-args-num-ok?
goでprintfの引数の数をチェックするときの諸々について。
decafe09
February 24, 2018
More Decks by decafe09
See All by decafe09
GoならわかるGIF
decafe09
0
360
よくあるWebサービスのAPIを作ったらいろいろ勉強になった話/WebApplicationAPITips
decafe09
1
380
goaを使ったAPI開発/api-development-with-goa
decafe09
0
2.1k
Other Decks in Programming
See All in Programming
AI時代になぜ書くのか
mutsumix
0
380
tRPCの概要と少しだけパフォーマンス
misoton665
2
270
なぜあなたのコードには「コシ」がないのか?〜AI時代に問う、最後まで美味しい設計と戦略〜 #phpconkagawa / phpconkagawa2026
shogogg
0
160
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
350
ローカルLLMでどこまでコードが書けるか / How much code can be written on a local LLM
kishida
2
350
KMP × Kotlin 2.3 - How Android Got Slower While iOS Builds Improved by 47%
rio432
0
170
2026年のソフトウェア開発を考える(2026/05版) / Software Engineering Scrum Fest Niigata 2026 Edition
twada
PRO
23
12k
実用!Hono RPC2026
yodaka
2
310
継続的な負荷検証を目指して
pyama86
3
1.1k
Are We Really Coding 10× Faster with AI?
kohzas
0
150
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
490
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
800
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
55
8.1k
Are puppies a ranking factor?
jonoalderson
1
3.4k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
190
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.5k
4 Signs Your Business is Dying
shpigford
187
22k
Scaling GitHub
holman
464
140k
Designing Powerful Visuals for Engaging Learning
tmiket
1
360
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
290
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.3k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
180
Transcript
printf - args num ok? Umeda.go #3 2018 - 02
- 24 @decafe09
fmt.Printf fmt.Printf("%s", "moji")
fmt.Printf fmt.Printf("%s", "moji", "yobun")
fmt.Printf $ go build # ここでは気づけない $ go run main.go
# ここで気づける moji%!(EXTRA string=yobun)
fmt.Printf $ go build # ここでは気づけない $ go run main.go
# ここで気づける moji%!(EXTRA string=yobun) func main() { http.HandleFunc( "/xxx", func(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s", "moji", "yobun") return }, ) http.ListenAndServe(":8080", nil) }
fmt.Printf $ go build # ここでは気づけない $ go run main.go
# ここでは気づけない $ curl localhost:8080/xxx # ここで気づける moji%!(EXTRA string=yobun)
go vet $ go vet ./main.go:6: Printf call needs 1
arg but has 2 args go vet ! curl " go vet CI go vet
go vet –printfuncs $ go vet –printfuncs=Infof ./main.go:6: Infof call
needs 1 arg but has 2 args func Infof(format string, a ...interface{}) (n int, err error) { return fmt.Printf(format, a...) }
go vet @ Go 1.10 $ go test ./main.go:6: Printf
call needs 1 arg but has 2 args go1.9.3 main.go:6: wrong number of args for format in Printf call: 1 needed but 2 args go1.10 main.go:6: Printf call needs 1 arg but has 2 args go test go vet go test -printfuncs
Humm... format := "%s" fmt.Printf(format, "moji", ”yobun")
Humm... $ go build # ここでは気づけない $ go vet #
ここでは気づけない $ go run main.go # ここで気づける moji%!(EXTRA string=yobun) http ! % # &$"%
Background API6=3.;?>/2,BW!:20/)O APIVK" C %!8>?4+?5JSAPIRFP XXNTL@YYNTLIEH XXYYEH
XX, YY O(&'!format!O(& %D 71? +<:20/GQ&72-/M%A format#xx,yy*9=-.;?U$
Thinking... "0(' ! 0(',1#%$ /0( $ !
+.)*format !format !&-...
Result func check(format string) error 1. format %! 2. format
message 3. format %! 4. %! func main() { format := "%s" err := check(format) if err != nil { panic(err) } fmt.Println(message(format)) } func message(format string) string { return fmt.Sprintf(format, "moji", "yobun") } func check(format string) error { pre := strings.Count(format, "%!") msg := message(format) post := strings.Count(msg, "%!") if post > pre { return errors.New("format error: " + msg) } return nil }
OK All errors begin with the string "%!" followed sometimes
by a single character (the verb) and end with a parenthesized description. https://golang.org/pkg/fmt/
Check $ go run main.go panic: format error: moji%!(EXTRA string=yobun)
Profile decafe09